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.

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
  • 4
    Why don't you check in the instruction set reference instead of asking here and waiting for an answer? – Jester Oct 18 '14 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 .. – k112275 Uzaif Umer Oct 18 '14 at 13:56
  • [How does the NEG instruction set the adjust flag (AF) on x86?](https://stackoverflow.com/q/44837231) also explains that `neg x` sets flags according to `0 - x`, just like a `sub`. – Peter Cordes Oct 25 '18 at 23:29

2 Answers2

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.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
0

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

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/28233977) – Daemon Painter Feb 03 '21 at 10:50
  • @DaemonPainter It does provide an Answer to the Question. It was just not nicely formatted. – Scratte Feb 03 '21 at 21:05
  • 1
    @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. – Peter Cordes Feb 03 '21 at 21:38
  • @PeterCordes That is all very reasonable. Deleting this as a subject matter expert is always an option. Deleting it via the Low Quality Post queue, that only serves to validate a "Not an Answer" flag, is an entirely different matter. That queue is not meant to be an early access to 20K privileges. – Scratte Feb 03 '21 at 21:48
  • 1
    @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. – Peter Cordes Feb 03 '21 at 21:51
  • 1
    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 :) – Daemon Painter Feb 04 '21 at 08:02