Daniel Pop
Daniel Pop

Reputation: 550

Carry flag set on signed arithmetic

mov ah, -128 
mov bh, 80h 
add ah,bh 

Why does this code set CF, taking into consideration the fact that the computation doesn't need a carry? Or better, what's the behavior of CF in signed arithmetic and of OF in unsigned?

Upvotes: 0

Views: 1169

Answers (2)

Micha Wiedenmann
Micha Wiedenmann

Reputation: 20823

The addition is done as Two's Complement, and the result is larger than 8 bit. 0x80 + 0x80 = 0x100 or in binary:

   0b10000000
+  0b10000000
-------------
= 0b100000000

and thus the result is 0 and the Carry Flag is set.

Don't think of -128 as a negative number, rather think of it as the positive number (128) which has the same bit pattern as the two's compliment of your negative number, then carry out the unsigned addition. (And therefore the carry flag is set.)

Upvotes: 5

Mike
Mike

Reputation: 4288

You could not represent 128 with an signed 8 Bit value:

-128 = 0x80

So you are calculate:

(-128)
+ (-128)
-------
-256   (not a 8 Bit value and overflow to the next Byte -> C is set)

And the carry is set.

A 8 Bit signed value is good for the range: -128 ... 127

Upvotes: 0

Related Questions