What are the four x86 condition-code flags, and when is each one set?
CF (carry/unsigned overflow), ZF (zero), SF (sign/negative), OF (signed overflow) — set as a side effect of arithmetic, or deliberately by cmp/test.
These are single-bit flags in a status register. Arithmetic instructions update them automatically; later conditional jumps, sets, and moves read them. They're the bridge between "doing math" and "making a decision."
| Flag | Name | Set when… |
|---|---|---|
| CF | Carry | Unsigned overflow — a carry out of the most-significant bit |
| ZF | Zero | The result is exactly 0 |
| SF | Sign | The result is negative (its MSB is 1) |
| OF | Overflow | Signed (two's-complement) overflow occurred |
For addq src, dest computing t = a + b:
- ZF = (
t == 0), SF = (t < 0as signed), CF = unsigned overflow. - OF = signed overflow:
(a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)— i.e. two same-signed operands produced a wrong-signed result.
Two flavors of overflow: CF is for unsigned interpretation, OF for signed. The same bits, the same addition — the CPU just sets both so either interpretation has its overflow indicator ready.
Note: lea does not touch the flags (it's just an address calculation), which is part of why compilers love it for arithmetic.
Go deeper:
FLAGS register (Wikipedia) — the FLAGS register: CF, ZF, SF and OF.
CMP — x86 instruction reference — CMP as the deliberate flag-setter.