user3175173
user3175173

Reputation: 103

Mips subtraction negative overflow

Hexadecimal in mips: If I have 0x80000000 and I subtract it by 0xD00000000 my answer is 0X -50000000 is this possible in mips having a negative in or is there another way to write this? That is correct?

Upvotes: 1

Views: 4860

Answers (1)

RobertB
RobertB

Reputation: 1076

Remember, if you look at the most significant bit, you'll know if you are working with a negative number. For simplicity, let's just look at the four most significant bits.

0x80 = 1000 0000
0xD0 = 1101 0000

So those values are already negative. You want to subtract them, that is, 0x80 - 0xD0. Well, subtraction is addition (a-b = a + -b), and the whole point of 2's compliment is that you can add signed numbers and get the result you expect. So let's negate 0xD0:

1101 0000 # original value
0010 1111 # flip the bits
0011 0000 # add 1

If that didn't make sense, ask the all-knowing Wikipedia.

Now, we can add the values:

1000 0000
0011 0000
---------
1011 0000

So if I've done it right, 0x80 - 0xD0 = 0xB0. It's still a negative number (but you don't put a minus sign in front of it, it's implied by the MSBit). And that makes sense, because 0x80 is a very negative number, and 0xD0 is a less negative number than 0x80. (Really, it is... 0xFF is -1, the least negative number.)

Upvotes: 2

Related Questions