NEG 指令如何影响 x86 上的标志?

How does the NEG instruction affect the flags on x86?

英特尔软件开发手册对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 , %eax

但事实并非如此,在真实 CPU 上取反 0x6ffffef5 设置 AF 和 CF。

neg 设置 all 标志与从 0.

得到的 sub 相同

此指令序列设置所有标志(包括 AF 和 CF)与 neg %eax 相同:

xor  %ecx, %ecx
sub  %eax, %ecx     # ecx = 0 - eax

英特尔的文档确实指定了这一点,但没有在伪代码操作部分或 neg 本身的指令集参考(第 2 卷)条目的标志影响部分中。

the Description section for neg 的文本包含以下内容:

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.

指出了此文档的存在,但措辞并不直接。

我不知道第 1 卷有一整节说明说明。事实证明,第 2 卷 insn 集参考中并没有英特尔所说的关于单个指令的所有内容。


有一些证据表明 neg 在内部解码为与 Intel CPU 上的 sub 指令相同的 uop。 (例如 neg [mem] 可以将负载与 ALU op 微融合,以及将存储地址和存储数据微融合微融合。inc [mem] 只能对存储进行微融合,所以它是 3总融合域 uops)。