Conditions under which EFLAGS flags are set in x86/x64

827 Views Asked by At

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

There are 1 best solutions below

2
On

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