1

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.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • @RudyVelthuis can you make that an answer? – Evan Carroll Oct 25 '18 at 22:41
  • @PeterCordes I'm not saying that you couldn't fix that question and that answer, but you don't even mention the carry flag there. Making canonical answers too on badly formed questions is really confusing for search. – Evan Carroll Oct 25 '18 at 23:32
  • The first sentence of my answer there is *neg sets all flags identically to what you'd get with a sub from 0.* It's the same issue: the asker of that question was thinking of NEG in terms of `~x + 1` instead of `0 - x`. My answer shows where Intel documents the fact that it's really based on `0 - x`. Is it not obvious that `0-x` explains the behaviour? – Peter Cordes Oct 25 '18 at 23:48
  • Then fix the question itself. I don't actually object to concentrating questions on StackOverflow, I just think if you want to do that you have to abstract out the target question to cover the source questions. That question is specific to the AF flag. It so happens that your address provides the information I'm looking for, but when you a dupe a question you're saying the question is the same. StackOverflow gives you pretty liberal abilities to rewrite questions, you should take that those abilities and rewrite it to be about the NEG instruction and **all** x86 flags. – Evan Carroll Oct 25 '18 at 23:52
  • I don't think we need to make that question more generic just because the answer is generic and covers all flags. It's just something you have to get used to on SO. I know non-logged-in views forward you directly to the target duplicate, so there is a small case to be made that some searchers might not read the answer because the question title looks specific to a different problem. I hope most people will just read the answer, because the question is short. – Peter Cordes Oct 25 '18 at 23:56
  • @PeterCordes a *small* case? Seems like that's a pretty large case. I don't trust titles that don't line up with questions on Google. I simply look for ones that do. If there is none that do, I assume the question hasn't been asked (as in this case) if the questioned lined up, I wouldn't have asked it. Take a look at my change, I even asked the author to review my changes. I think for posterity that will make for a better question? With that said, I'm cool with duping this to that. – Evan Carroll Oct 26 '18 at 00:03
  • @PeterCordes also, if this gets duped, **and** it gets downvoted (which it is) it'll actually get deleted and purged. That just sets up the next guy to do it all again. – Evan Carroll Oct 26 '18 at 00:05

1 Answers1

2

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.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • I tried to show some code, but this helps a lot by giving me a way to think about it. It never occurred to me to think of 2cp like that, I always though bitwise-neg + 1. – Evan Carroll Oct 25 '18 at 22:51