Why does the NEG instruction interfere with the Carry Flag?

Evan Carroll

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.

Rudy Velthuis

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.

edited at
0

Comments

0 comments
Login to comment

Related

Why does "#define A" interfere with "namespace A{}"?

Why does socket interfere with selenium?

How is the x86 JAE instruction related to the carry flag?

Why does my macro stop at an .Unprotect instruction and then carry on unhindered when I continue it without changing anything?

Why does "position: relative" interfere with "transform: scale"?

Why does remapping key interfere with FormatTime?

Why does DispatchGroup interfere with main queue?

How does the NEG instruction affect the flags on x86?

Why is the Carry Flag set during a subtraction when zero is the minuend?

How does the carry and overflow flag behave when result is to big for register

CMP and carry flag

Why does enabling undefined behaviour sanitization interfere with optimizations?

Why does a nested self-capturing function interfere with isKnownUniquelyReferenced(_:)?

why does a local PrintWriter interfere with another local PrintWriter?

Why does my Navbar interfere with my Stripe token creation?

Why does optimizing with the -O(1/2/3) option interfere with scanf()?

Implementing a Web Api Controller - why does use of IDisposable interfere with routing?

Why does my firewall (iptables) interfere in my bridge (brctl)?

Why does the Zero Flag exist?

Setting carry flag in Armsim#

Carry flag set on signed arithmetic

Assembly language carry flag counter

will carry flag be set after CMP?

how does CF(Carry flag) get set according to the computation t = a-b where a and b are unsigned integers

Does Git interfere with SVN?

Why does `difftime` carry label attribute from other variable in R?

Why does my structure element carry padding bytes?

Move a partition out from under scp: why does it carry on?

gcc compile instruction contain flag -03. What does it do?