英特尔软件开发手册对 neg
指令是这样描述的:
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
被替换为,
not %eax # bitwise negation
add $1, %eax
但事实并非如此,在真实的 CPU 上对 0x6ffffef5 取反会设置 AF 和 CF。
最佳答案
neg
设置 all 标志,与使用 sub
从 0 获得的标志相同。
此指令序列将所有标志(包括 AF 和 CF)设置为与 neg %eax
相同:
xor %ecx, %ecx
sub %eax, %ecx # ecx = 0 - eax
<小时/>
英特尔的文档实际上确实指定了这一点,但没有在伪代码操作部分或 neg
本身的指令集引用(第 2 卷)条目的标志影响部分中指定。
the Description section for neg
的文字包括这个金 block :
This operation is equivalent to subtracting the operand from 0.
在 Volume 1 :
7.3.2.4 Comparison and Sign Change Instructions
[a paragraph about CMP]
The NEG (negate) instruction subtracts a signed integer operand from zero.
a comment on an earlier duplicate of this question 指出了此文档的存在这不是直接措辞。
我不知道第 1 卷有一整节解释说明。事实证明,英特尔关于各个指令的所有说明并不都在第 2 卷 insn 集引用中。
<小时/>有一些证据表明 neg
在内部解码为与 Intel CPU 上的 sub
指令相同的 uop。 (例如,neg [mem]
可以将负载与 ALU 操作微融合,以及存储地址和存储数据 uops 微融合。inc [mem]
只能对存储进行微融合,因此总共有 3 个融合域 uops)。
关于assembly - NEG 指令如何影响 x86 上的标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44837231/