What does the %rflags register contain?
%rflags is a register of single-bit status flags that arithmetic and comparison instructions set as a side effect, recording facts about the most recent result.
* Arithmetic sets ZF, SF, CF and OF at once — CF reads the result as unsigned, OF as signed. *
You rarely set these flags on purpose; instructions like add, sub, and cmp set them automatically, and then conditional jumps read them. The four that matter most for control flow:
| Flag | Name | Set when… |
|---|---|---|
| ZF | Zero | Result is zero |
| SF | Sign | Result is negative (top bit = 1) |
| CF | Carry | Unsigned overflow / borrow |
| OF | Overflow | Signed overflow |
The crucial mental model is that CF is the flag for unsigned comparisons and OF for signed ones — the same subtraction sets both, and the jump you choose decides which interpretation you mean:
cmp %rax, %rbx # compute %rbx - %rax, set flags
je equal # ZF=1 → equal
jl less # SF≠OF → signed less-than
jb below # CF=1 → unsigned less-than
Tip: cmp is just a subtraction that throws away the result and keeps only the flags — that's the whole trick behind conditional branches.
Go deeper:
FLAGS register (Wikipedia) — the x86 FLAGS register with the ZF/SF/CF/OF status bits.
Felix Cloutier — CMP — the subtract-and-set-flags instruction behind branches.