在x86/x64体系结构中设置EFLAGS标志的条件。

huangapple go评论67阅读模式
英文:

Conditions under which EFLAGS flags are set in x86/x64

问题

我想知道设置基本的 EFLAGS 标志(CF、ZF、OF、SF 等)的条件是什么。我查阅了英特尔 x86 指令手册和这个网站,但没有成功。我成功找到了条件跳转的条件(例如,如果 (SF != OF) 或 (ZF == 1),则执行 JLE),但没有找到有关标志本身的条件。

关于 ZF,它是最简单的,只需检查结果是否为零。关于 SF,我假设需要检查结果的最高有效位是零还是一。但对于其他标志,我不确定。此外,这些条件是否适用于操纵这些标志的所有 x86 指令,还是 ADD 和 CMP 会根据不同的方程设置它们的标志?

英文:

I would like to know what are the conditions under which the basic EFLAGS flags (CF, ZF, OF, SF...) are set. I have looked into the Intel x86 instruction manual, and this website that is well done, but without success. I managed to find the conditions for the conditional jumps (for example, a JLE is taken if (SF != OF) or (ZF == 1)), but not regarding the flags themselves.

Regarding ZF, it is the easiest one, as it is only needed to check if the result is zero. For SF, I assume that one have to check if the most significant bit of the result is zero or one. But for the others, I am unsure. Additionally, are those conditions the same across all x86 instructions manipulating those flags, or do an ADD and a CMP will set their flags under different equations?

答案1

得分: 2

英特尔SDM在第1卷的3.4.3 EFLAGS寄存器部分回答了您关于的问题。

部分引用文档:

3.4.3.1 状态标志

EFLAGS寄存器的状态标志(位0、2、4、6、7和11)指示算术指令(如ADD、SUB、MUL和DIV指令)的结果。状态标志的功能如下:

  • CF(位0)进位标志 — 如果算术操作生成结果的最高位处的进位或借位,则设置;否则清除。对于无符号整数算术,此标志指示溢出条件。它还用于多精度算术。
  • PF(位2)奇偶标志 — 如果结果的最低字节包含偶数个1位,则设置;否则清除。
  • AF(位4)辅助进位标志 — 如果算术操作生成结果的第3位处的进位或借位,则设置;否则清除。此标志用于二进制编码的十进制(BCD)算术。
  • ZF(位6)零标志 — 如果结果为零,则设置;否则清除。

[...]

这并不是关于这个主题最深入的资源,因为它不会涵盖各种x86实现的勘误表(而这些有很多...),但它肯定是帮助您入门的最佳资源。

愉快的编程 在x86/x64体系结构中设置EFLAGS标志的条件。

英文:

The intel SDM does answer your question on Volume 1 - 3.4.3 EFLAGS Register.

Partly quoting the doc:

> 3.4.3.1 Status Flags
>
> The status flags (bits 0, 2, 4, 6, 7, and 11) of the EFLAGS register indicate the results of arithmetic instructions,
such as the ADD, SUB, MUL, and DIV instructions. The status flag functions are:
> * CF (bit 0) Carry flag — Set if an arithmetic operation generates a carry or a borrow out of the most-
significant bit of the result; cleared otherwise. This flag indicates an overflow condition for
unsigned-integer arithmetic. It is also used in multiple-precision arithmetic.
> * PF (bit 2) Parity flag — Set if the least-significant byte of the result contains an even number of 1 bits;
cleared otherwise.
> * AF (bit 4) Auxiliary Carry flag — Set if an arithmetic operation generates a carry or a borrow out of bit
3 of the result; cleared otherwise. This flag is used in binary-coded decimal (BCD) arithmetic.
> * ZF (bit 6) Zero flag — Set if the result is zero; cleared otherwise.
>
> [...]

That is not the most in-depth source you'll find on the subject, as it won't cover errata for various x86 implementations (and there's a ton of those...) but it is certainly the best to get you started.

Happy hacking 在x86/x64体系结构中设置EFLAGS标志的条件。

huangapple
  • 本文由 发表于 2023年1月8日 22:33:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定