Understanding RFLAGS: Carry, Zero, Sign and Overflow Flags
How the x86-64 carry, zero, sign and overflow flags are set by add, sub and cmp, with worked 8-bit examples and the conditional jumps that read them.

Every conditional branch on x86-64 is a two-step dance. One instruction does some arithmetic and leaves facts behind in the rflags register, and a later jcc instruction reads those facts. Nothing else carries the comparison between them, so if you understand four of the flags, CF, ZF, SF and OF, you understand why every if, while and for compiles the way it does. This post sets out what each one means, shows 8-bit worked examples you can check by hand, and maps them to the conditional jumps.
The flags register
rflags is a 64-bit register, but only a handful of its bits matter for everyday code. The arithmetic ones are:
| Flag | Bit | Name | Set when |
|---|---|---|---|
CF | 0 | Carry | an unsigned operation carried out of, or borrowed into, the top bit |
PF | 2 | Parity | the low byte of the result has an even number of 1 bits |
AF | 4 | Adjust | a carry out of bit 3 (used only by old decimal-adjust instructions) |
ZF | 6 | Zero | the result is zero |
SF | 7 | Sign | the top bit of the result is 1 |
OF | 11 | Overflow | a signed result does not fit in the destination |
The Intel 64 and IA-32 Architectures Software Developer's Manual, volume 1, chapter 3, defines these as the "status flags". Other bits control behavior: DF (bit 10) is the direction flag for string instructions, and IF (bit 9) enables interrupts and cannot be changed by user code.
Two points to hold on to. First, only some instructions write flags: add, sub, cmp, and, or, xor, test, neg, shifts and a few others do, but mov, lea, push, pop and the jumps do not. Second, the CPU does not know whether your numbers are signed or unsigned. It sets CF as though they were unsigned and OF as though they were signed, every time, and it is your choice of jump that picks the interpretation.
Carry versus overflow with 8-bit examples
Eight bits keep the arithmetic small enough to do on paper. An 8-bit value is 0 to 255 unsigned, or -128 to 127 signed.
mov al, 0xFF
add al, 1 ; case A
mov al, 0x7F
add al, 1 ; case B
mov al, 0x80
add al, 0x80 ; case C
mov al, 5
sub al, 9 ; case DWork out each result and the four flags:
case A: 0xFF + 0x01 = 0x100 -> al = 0x00 CF=1 ZF=1 SF=0 OF=0
case B: 0x7F + 0x01 = 0x80 -> al = 0x80 CF=0 ZF=0 SF=1 OF=1
case C: 0x80 + 0x80 = 0x100 -> al = 0x00 CF=1 ZF=1 SF=0 OF=1
case D: 0x05 - 0x09 = -4 -> al = 0xFC CF=1 ZF=0 SF=1 OF=0Read them one at a time.
Case A is an unsigned wrap. 255 plus 1 does not fit in 8 bits, so the result is 0 and CF records that a carry fell off the top. As a signed operation, -1 plus 1 is 0 and fits perfectly, so OF is 0. The same bits are an overflow in one view and fine in the other.
Case B is the reverse. 127 plus 1 is fine unsigned (128), so CF is 0. Signed, it is 127 plus 1 giving 128, which does not fit in -128 to 127, so OF is 1, and the stored result 0x80 has its top bit set so SF is 1 as well. Note that SF alone is not the sign of the true answer when OF is set: the stored result looks negative but the real sum is positive. This is exactly why the signed jumps combine SF and OF.
Case C adds -128 and -128 as signed values. The true result, -256, does not fit, so OF is 1. Unsigned, 128 plus 128 is 256, which carries, so CF is 1.
Case D subtracts. For sub, CF means a borrow: unsigned 5 minus 9 needs to borrow, so CF is 1. The signed result, -4, fits, so OF is 0, and the result 0xFC has its top bit set, so SF is 1.
The rule for OF in addition: it is set when both operands have the same sign and the result's sign differs. Whenever you are unsure, redo the sum in a wider width and ask whether it still fits.
How compare and test use flags
cmp a, b performs a - b, sets the flags exactly as sub would, and throws away the result. test a, b does a & b the same way. Their only purpose is to leave flags for the next instruction.
cmp rdi, rsi ; flags from rdi - rsi
jb .smaller ; taken if rdi < rsi as unsigned numbers (CF=1)That pair is the whole implementation of if (a < b) for unsigned values. For signed values you use jl instead, which reads SF and OF.
The conditional jumps
Each jcc is a fixed expression over the flags. The ones to memorize are in this table. Several mnemonics are synonyms for the same condition.
| Jump | Meaning | Condition |
|---|---|---|
je, jz | equal, zero | ZF=1 |
jne, jnz | not equal | ZF=0 |
jb, jc, jnae | below (unsigned <) | CF=1 |
jae, jnc, jnb | above or equal (unsigned >=) | CF=0 |
ja, jnbe | above (unsigned >) | CF=0 and ZF=0 |
jbe, jna | below or equal (unsigned <=) | CF=1 or ZF=1 |
jl, jnge | less (signed <) | SF differs from OF |
jge, jnl | greater or equal (signed >=) | SF equals OF |
jg, jnle | greater (signed >) | ZF=0 and SF equals OF |
jle, jng | less or equal (signed <=) | ZF=1 or SF differs from OF |
js / jns | negative / non-negative | SF=1 / SF=0 |
jo / jno | overflow / no overflow | OF=1 / OF=0 |
The signed condition SF != OF is clever: it means "the sign bit of the stored result is wrong exactly when the overflow flag says so". Case B above, 127 + 1, has SF=1 and OF=1, which are equal, so jge is taken, and the true answer (128) is indeed non-negative.
Read the flags yourself
You can copy rflags into a register with pushfq and pop. This helps when you are checking your own hand calculation in a debugger:
mov al, 0xFF
add al, 1
pushfq ; push rflags on the stack
pop rax ; rax = rflagsrax = 0x257Decode 0x257 in binary: bits 0 (CF), 1 (reserved, always 1), 2 (PF), 4 (AF), 6 (ZF) and 9 (IF). That matches case A: a carry, a zero result, even parity of 0x00, and a carry out of bit 3. The IF bit reads as 1 in a normal user-space process. Treat the exact upper bits as machine-dependent.
Flag pitfalls
**
incanddecdo not touchCF.** They update the other flags only, soinc alon0xFFgivesZF=1but leaves the previous carry. This is the reason multi-precision loops useleaordecto count while anadcchain carries.**
movdoes not set flags.** Amovbetweencmpandjccis fine, because it leaves the flags alone, but anaddin between destroys them.Shifts by zero leave flags unchanged, and shifts by more than one bit leave
OFundefined. Do not branch onOFafter a shift bycl.A compiler may combine the test with the previous arithmetic. After
sub rax, rdx, a followingjzneeds notest, becausesubalready setZF.


