NEG指令如何影响x86上的标志? [英] How does the NEG instruction affect the flags on x86?
问题描述
《英特尔软件开发手册》关于neg
指令的内容是这样的:
The Intel Software Development Manual says this about the neg
instruction:
如果源操作数为0,则CF标志设置为0;否则,将CF标志设置为0.否则设置为1.OF,SF,ZF,AF和PF标志根据结果设置.
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.
我认为将AF和CF设置为好像由neg %eax
替换为
I thought that AF and CF would be set as if neg %eax
were replaced by,
not %eax # bitwise negation
add $1, %eax
但是事实并非如此,在实际CPU上取反0x6ffffef5会设置AF和CF.
But that's not the case, negating 0x6ffffef5 on a real CPU sets AF and CF.
推荐答案
neg
将所有标志设置为与sub
从0开始获得的标志相同.
neg
sets all flags identically to what you'd get with a sub
from 0.
此指令序列将所有标志(包括AF和CF)设置为与neg %eax
相同:
This sequence of instructions sets all flags (including AF and CF) identically to neg %eax
:
xor %ecx, %ecx
sub %eax, %ecx # ecx = 0 - eax
英特尔文档确实确实指定了此内容,但没有在neg
本身的指令集参考(第2卷)条目的伪代码操作"部分或受标志影响的部分中指定.
Intel's documentation does actually specify this, but not in the pseudo-code Operation section or in the flags-affected section of the instruction-set reference (Volume 2) entry for neg
itself.
neg
的描述"部分的文本包括此块:
The text of the Description section for neg
includes this nugget:
此操作等效于从0减去操作数.
This operation is equivalent to subtracting the operand from 0.