1

I want to know if the NEG instruction affects the overflow flag too. I know that it negates the value of a variable, but couldn't find out whether it affects the Overflow flag or not.

3
  • 4
    Why don't you check in the instruction set reference instead of asking here and waiting for an answer? Commented Oct 18, 2014 at 13:52
  • i viewed there but its really hard to got from there...need just a one to 2 line answer, i'm just new to this .. Commented Oct 18, 2014 at 13:56
  • How does the NEG instruction set the adjust flag (AF) on x86? also explains that neg x sets flags according to 0 - x, just like a sub. Commented Oct 25, 2018 at 23:29

2 Answers 2

5

If you want to know what instructions do, consult the reference manuals.

The essential reference, namely the Intel instruction set manual says this about the NEG instruction:

Flags Affected
The CF flag set to 0 if the source operand is 0; otherwise it is set to 1.
The OF, SF, ZF, AF, and PF flags are set according to the result. 

So it is clear that the NEG instruction sets the O flag; therefore it affects the O flag, which is the OP's original question. And it does so every time it is executed. (People should not confuse "didn't change" from "not set").

That particular reference manual doesn't provide a specific algorithm to indicate when O is set to zero or one. However, Intel CPUs are 2's complement machines. The Subtract instruction has the exact same verbiage. NEG X is equivalent to (0 SUBTRACT X). So NEG should set the O bit according to "overflow" for (0 SUBTRACT X); this will set O when X is 0x8000000.

Inspecting the Intel Basic Archiecture Manual, we find this description of the OF bit:

OF (bit 11) Overflow flag
— Set if the integer result is too large a positive number or too small a
  negative number (excluding the sign-bit) to fit in the destination operand;
  cleared otherwise. This flag indicates an overflow condition for signed-integer
 (two’s complement) arithmetic

confirming our understanding.

Sign up to request clarification or add additional context in comments.

Comments

0

If you negthe value 80h, the operand does not change, but the overflow flag is indeed set to 1.

6 Comments

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
@DaemonPainter It does provide an Answer to the Question. It was just not nicely formatted.
@Scratte: It only barely provide a new answer; Ira's answer already gave the example of neg overflowing (and setting OF) on 0x8000000 (with 32-bit operand-size). This answer just adds the resulting overflow value (unchanged) and reduces the operand-size to 8-bit operand-size (without mentioning the size; neg eax with eax=80h will produce 0xffffff80 without overflowing.) On the whole, I don't think this answer needs deletion, but I didn't upvote because its example is too minimal: you already need to understand it to know which operand-size it's talking about, for example.
@Scratte: right, agreed, it does answer the question. It's arguably "low quality", but not "very low quality", so neither flag delete reason is justified. And it's not just a copy/paste of part of Ira's answer (which would justify deletion), it's just a paraphrase or independent statement of the only special case.
I apologize for being wrong in my evaluation of the post. Please don't take this personally, but the barely provide a new answer could have been added as a comment under the already available answer. The amount of added information is so low that could have been a very low quality. This is why we have multiple reviewers :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.