0

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Katoptriss
  • 107
  • 6

1 Answers1

2

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 ;)

Léo Germond
  • 720
  • 8
  • 18
  • Very few errata are related to basic functionality like setting FLAGS. Some instructions (like MUL and DIV) don't set most FLAGS this way, though (e.g. [ZF not set as a result of MUL instruction in assembly language](https://stackoverflow.com/q/25905651)). Very strange of Intel to mention those as instructions that set flags in a normal way like ADD/SUB, / XOR/OR/AND, or shifts. (Set "according to the result"). http://ref.x86asm.net/coder32.html lists flags modified and well-defined vs. undefined for every form (opcode) of every instruction. – Peter Cordes Jan 08 '23 at 15:25
  • The complete version of this bullet list quote appears in [What does "set according to the result" mean in Intel's SDM manuals?](https://stackoverflow.com/q/40478386) (where it's a good answer, because it does define what it means when instructions like `adc` say "The OF, SF, ZF, AF, CF, and PF flags are set according to the result." (https://www.felixcloutier.com/x86/adc#flags-affected - every instruction has a Flags Affected section in the manual.) – Peter Cordes Jan 08 '23 at 15:26