Flags and the Status Register
The flags register (RFLAGS) is the unsung circuit behind every decision your code makes. Every arithmetic or logical instruction updates a few bits to characterise the result, and conditional jumps, conditional moves, and the rest of the branching cast read those bits to choose what happens next.
Skip the flags and you can't write a loop, an if-statement, or anything else with a branch in it. They're load-bearing.
The RFLAGS Register
RFLAGS is 64 bits wide, but in user-space code you'll only ever care about a fraction of them. The six worth memorising:
| Flag | Bit | Full Name | Set when... |
|---|---|---|---|
| CF | 0 | Carry Flag | An unsigned operation overflowed (carry out of MSB) |
| PF | 2 | Parity Flag | The lowest byte of the result has an even number of 1-bits |
| AF | 4 | Auxiliary Carry | Carry from bit 3 to bit 4 (BCD arithmetic) |
| ZF | 6 | Zero Flag | The result is exactly zero |
| SF | 7 | Sign Flag | The most significant bit of the result is 1 (negative in signed) |
| OF | 11 | Overflow Flag | A signed operation overflowed |
A peek at the lower 16 bits of the flags register:
The Carry Flag (CF) and Overflow Flag (OF) both indicate overflow, but for different number interpretations. CF detects unsigned overflow: the result wrapped around the unsigned range. OF detects signed overflow: the result crossed the boundary between positive and negative. For example, 0xFFFFFFFF + 1 (treating values as unsigned 32-bit) sets CF because the unsigned result exceeds 2^32. But 0x7FFFFFFF + 1 (treating the same bits as signed) sets OF because it wraps from the maximum positive integer to a negative number.
Which Instructions Set Flags?
Most arithmetic and logical instructions stir the flags:
Instructions that DO set flags:
ADD,SUB,INC,DEC,NEGAND,OR,XORSHL,SHR,SAR,ROL,RORMUL,IMUL(only CF and OF are defined)CMP,TEST
Instructions that do NOT set flags:
MOV,LEA,XCHGPUSH,POPCALL,RETNOTNOP
CMP - Compare Without Storing
CMP runs a subtraction and then bins the result, keeping only the flag side-effects:
cmp rax, rbx ; compute rax - rbx, set flags, throw away the resultIt's the canonical move before a conditional jump:
cmp rax, 10 je .is_ten ; jump if rax == 10 (ZF is set) jg .above_ten ; jump if rax > 10 (signed) jl .below_ten ; jump if rax < 10 (signed)CMP sets flags exactly as SUB would:
- If
rax == rbx, the subtraction yields zero, so ZF = 1 - If
rax < rbx(unsigned), the subtraction borrows, so CF = 1 - If
rax < rbx(signed), the result is negative, so SF != OF
TEST - Bitwise AND Without Storing
TEST runs a bitwise AND, sets flags, throws the result away:
test rax, rax ; AND rax with itself -- tests if rax is zero ; ZF = 1 if rax is 0, SF = 1 if rax is negativeTEST beats CMP for two everyday checks:
; Check if a register is zerotest rax, rax ; better than "cmp rax, 0" (smaller encoding)jz .is_zero
; Check if specific bits are settest rax, 0x01 ; is bit 0 set? (is rax odd?)jnz .bit_is_set
test rax, 0x80 ; is bit 7 set?jnz .high_bit_set
; Check if a syscall returned an error (negative value)test rax, raxjs .error ; jump if sign flag set (negative result)TEST always clears CF and OF; logical operations can't overflow.
test rax, rax and cmp rax, 0 both set ZF when rax is zero. But test rax, rax encodes in fewer bytes (no immediate operand to encode). Compilers almost always emit test for zero checks, so you will see this pattern everywhere in disassembled code. Recognizing it instantly is essential for reading real-world binaries.
Flag Combinations and Condition Codes
Conditional jumps fire on specific flag combinations, each with a condition code mnemonic:
| Condition | Meaning (after CMP a, b) | Flags tested |
|---|---|---|
| E / Z | Equal / Zero | ZF = 1 |
| NE / NZ | Not equal / Not zero | ZF = 0 |
| G / NLE | Greater (signed) | ZF=0 AND SF=OF |
| GE / NL | Greater or equal (signed) | SF = OF |
| L / NGE | Less (signed) | SF != OF |
| LE / NG | Less or equal (signed) | ZF=1 OR SF!=OF |
| A / NBE | Above (unsigned) | CF=0 AND ZF=0 |
| AE / NB | Above or equal (unsigned) | CF = 0 |
| B / NAE | Below (unsigned) | CF = 1 |
| BE / NA | Below or equal (unsigned) | CF=1 OR ZF=1 |
Signed conditions (G, GE, L, LE) read SF and OF. Unsigned ones (A, AE, B, BE) read CF and ZF. Mix them up and you've shipped a classic.
Put that to the test. Each row runs cmp al, bl with the byte values shown. For each one, decide which of jl, jb, jg and ja would jump. Read each byte both ways, as signed and as unsigned, before you answer.
al | bl | |
|---|---|---|
| 1 | 0x05 | 0x03 |
| 2 | 0xFF | 0x01 |
| 3 | 0x80 | 0x7F |
| 4 | 0x01 | 0xFF |
| 5 | 0x7F | 0x80 |
| 6 | 0x40 | 0x40 |
Answers
jgandja. 5 beats 3 either way.jlandja. Signed it's -1 against 1, unsigned it's 255 against 1.jlandja. Signed it's -128 against 127. The subtraction overflows (OF = 1, SF = 0), and SF ≠ OF is exactly whatjltests.jgandjb. Signed 1 beats -1, unsigned 1 loses to 255.jgandjb. Signed 127 beats -128, unsigned 127 loses to 128.- None of them. Equal values set ZF, and all four of these need ZF = 0 or a strict inequality.
Rows 2 to 5 are the dangerous ones: the signed and unsigned jumps disagree, so picking the wrong family flips the branch.
Reading Flags Directly
A few instructions let you poke flags directly:
; Set the carry flagstc ; CF = 1
; Clear the carry flagclc ; CF = 0
; Complement the carry flagcmc ; CF = !CF
; Push flags onto the stack and pop them backpushfq ; push RFLAGS onto the stackpopfq ; pop stack into RFLAGS
; Read flags into AH (legacy, rarely used)lahf ; AH = SF:ZF:0:AF:0:PF:1:CFPractical Example: Safe Unsigned Addition with Overflow Check
; Add two 64-bit unsigned values with overflow detection; Input: rdi = value a, rsi = value b; Output: rax = result, carry flag indicates overflow
safe_add: mov rax, rdi add rax, rsi ; rax = a + b, CF set if overflow jc .overflow ; jump if unsigned overflow occurred ret ; return sum in rax
.overflow: ; Handle overflow -- in a real program, return error or saturate mov rax, 0xFFFFFFFFFFFFFFFF ; saturate to max value retSetting a flag doesn't leak anything by itself. The leak comes from what you do with it: a conditional jump on a flag that depends on secret data makes the two paths take different amounts of time, and the branch predictor's state can reveal which way it went. An attacker who can measure those differences can infer the comparison result, sometimes across security boundaries. This is the basis of timing side-channel attacks. Cryptographic code is written to be constant-time: the same instructions run and the same memory is touched whatever the secret is. In practice that still uses cmp and test, but feeds the flags into cmovcc or setcc (or into arithmetic masks) instead of a jump, and avoids instructions whose latency varies with their operands, such as div. You'll build exactly this kind of branch-free comparison with CMOV and SETcc in the control-flow module.
Common Patterns in Real Code
Read enough disassembled code and these flag patterns become muscle memory:
; Pattern: null pointer checktest rdi, rdijz .handle_null
; Pattern: error return check (syscall or C function)test eax, eaxjs .error ; negative return = error
; Pattern: loop counter reaching zerodec rcxjnz .loop_top ; continue while rcx != 0
; Pattern: checking bit flagstest dword [rdi + FLAGS_OFFSET], FLAG_READABLEjz .not_readableWhat is Next
With flags decoded, the next lesson moves on to bitwise instructions: AND, OR, XOR, shifts, rotates. They're the underlying machinery for everything from data wrangling to cryptography.