How does a NEG instruction set the configuration flag (AF) to x86?

The Intel Software Development Guide says the instructions neg:

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

Obviously setting SF, ZF and PF according to the result means, and I can guess that OF is set when trying to negate the smallest negative number. But what about AF?

I thought that AF would be set as if neg %eaxreplaced by not %eax+ add $1, %eax. But no. Denying 0x6ffffef5 on the real CPU sets AF, even if adding 1 to 0x9000010a does not set AF.

+6
source share
1 answer

negsets all flags identical to what you would get with subfrom 0.

This sequence of instructions sets all flags (including AF) identically neg %eax:

xor  %ecx, %ecx
sub  %eax, %ecx

Intel's documentation actually indicates this, but not in the โ€œWorking with Pseudo-Codeโ€ section or in the flag-related section of the help record for the instruction set (Volume 2) for neg.

The text of the description section forneg this nugget includes:

This operation is equivalent to subtracting the operand from 0.

And in Volume 1 :

7.3.2.4 Instructions for changing and changing the sign

[paragraph on CMP]

The NEG command (negation) subtracts the signed integer operand.

, .

, vol.1 , . , , Intel , 2- .


, neg , sub Intel. (, neg [mem] ALU op, - . inc [mem] , 3 ).

+5

All Articles