What is meant by "Applying the NEG instruction to a nonzero operand always sets the Carry flag."
Why does substracting 2 from 1 set the carry flag?
00000001 (1)
+ 11111110 (-2) [in 2-complement form]
---------------------
CF:1 11111111 (-1) [ why is the carry flag set here???]
You could view NEG a
as equivalent to SUB 0, a
. If a
is non-zero, then this will set the carry flag (as this will always result in an unsigned overflow).
First, we are know when a < b
, a - b
can always produce a carry(Borrow).
Second, let's understand why x86 inverted the carry flag when subtracting.
The first point for is so obvious a human being, but not so for a computer. (Suppose computer use a + (~b+1)
to replace the a - b
to do calculation.) How computer figure out there is a borrow ? We can take 2's complements as a clock.(Clockwise means the original number -- a
, anticlockwise means inversion --~b+1
)
So for a computer, it can tell that (for subtraction) if a > b
, a + ~b + 1
will have carry (overlap in the picture); if a < b
, a + ~b + 1
will not have carry (gap in the picture).
Conclusion:
So for subtraction, if there is no carry means there is borrow;
if there is carry means no borrow, ie invert the carry bit.
By the way, as far as I have tried, there is no overflow (ie, OF
is not set when 1 - 2
.) I don't think @Oliver is right in this point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With