I see this information on the site,
During a negation (NEG), the carry flag is set unless the operand is zero, in which case it is cleared.
But, it doesn't help me reason about the carry flag. Why does it behave this way and how does NEG trigger a flag that supposed to represent a carry-out.
In this answer they quote the manual,
The CF flag set to 0 if the source operand is 0; otherwise it is set to 1.
In my head, and the way I see this explained I'm doing a bitwise-negation, and adding one. Why is the carry flag set? Is there any process that will help me grok this without understanding rules like the one above.
The carry flag, on a subtraction, represents a borrow. If you negate x
, you (virtually) subtract x
from 0, which needs a borrow, unless x
is 0.
So it makes sense that the carry flag is set unless you subtract 0.
3 bit 2-s compliment
cf
[0]000 value = 0
[0]011 SUB 3
---
Requires borrow.
cf
[1]111 0 after carry (cf=1)
011 SUB 3
------
[1]100
+1 2cp
101
You can think of mentally as bitwise-negation.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments