Skip to main content
Be Bitwise
x86 AssemblyData Movement & Arithmetic

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:

FlagBitFull NameSet when...
CF0Carry FlagAn unsigned operation overflowed (carry out of MSB)
PF2Parity FlagThe lowest byte of the result has an even number of 1-bits
AF4Auxiliary CarryCarry from bit 3 to bit 4 (BCD arithmetic)
ZF6Zero FlagThe result is exactly zero
SF7Sign FlagThe most significant bit of the result is 1 (negative in signed)
OF11Overflow FlagA signed operation overflowed

A peek at the lower 16 bits of the flags register:

215
214
213
212
211
210
29
28
27
26
25
24
23
22
21
20
Decimal0
Hex0x0000
Binary0000000000000000
Key Concept: CF vs OF

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, NEG
  • AND, OR, XOR
  • SHL, SHR, SAR, ROL, ROR
  • MUL, IMUL (only CF and OF are defined)
  • CMP, TEST

Instructions that do NOT set flags:

  • MOV, LEA, XCHG
  • PUSH, POP
  • CALL, RET
  • NOT
  • NOP

CMP - Compare Without Storing

CMP runs a subtraction and then bins the result, keeping only the flag side-effects:

nasm
cmp rax, rbx ; compute rax - rbx, set flags, throw away the result

It's the canonical move before a conditional jump:

nasm
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:

nasm
test rax, rax ; AND rax with itself -- tests if rax is zero
; ZF = 1 if rax is 0, SF = 1 if rax is negative

TEST beats CMP for two everyday checks:

nasm
; Check if a register is zero
test rax, rax ; better than "cmp rax, 0" (smaller encoding)
jz .is_zero
; Check if specific bits are set
test 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, rax
js .error ; jump if sign flag set (negative result)

TEST always clears CF and OF; logical operations can't overflow.

Why TEST over CMP for Zero Checks

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:

ConditionMeaning (after CMP a, b)Flags tested
E / ZEqual / ZeroZF = 1
NE / NZNot equal / Not zeroZF = 0
G / NLEGreater (signed)ZF=0 AND SF=OF
GE / NLGreater or equal (signed)SF = OF
L / NGELess (signed)SF != OF
LE / NGLess or equal (signed)ZF=1 OR SF!=OF
A / NBEAbove (unsigned)CF=0 AND ZF=0
AE / NBAbove or equal (unsigned)CF = 0
B / NAEBelow (unsigned)CF = 1
BE / NABelow 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.

albl
10x050x03
20xFF0x01
30x800x7F
40x010xFF
50x7F0x80
60x400x40
Answers
  1. jg and ja. 5 beats 3 either way.
  2. jl and ja. Signed it's -1 against 1, unsigned it's 255 against 1.
  3. jl and ja. Signed it's -128 against 127. The subtraction overflows (OF = 1, SF = 0), and SF ≠ OF is exactly what jl tests.
  4. jg and jb. Signed 1 beats -1, unsigned 1 loses to 255.
  5. jg and jb. Signed 127 beats -128, unsigned 127 loses to 128.
  6. 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:

nasm
; Set the carry flag
stc ; CF = 1
; Clear the carry flag
clc ; CF = 0
; Complement the carry flag
cmc ; CF = !CF
; Push flags onto the stack and pop them back
pushfq ; push RFLAGS onto the stack
popfq ; pop stack into RFLAGS
; Read flags into AH (legacy, rarely used)
lahf ; AH = SF:ZF:0:AF:0:PF:1:CF

Practical Example: Safe Unsigned Addition with Overflow Check

nasm
; 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
ret
Security Note: Flag-Based Side Channels

Setting 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:

nasm
; Pattern: null pointer check
test rdi, rdi
jz .handle_null
; Pattern: error return check (syscall or C function)
test eax, eax
js .error ; negative return = error
; Pattern: loop counter reaching zero
dec rcx
jnz .loop_top ; continue while rcx != 0
; Pattern: checking bit flags
test dword [rdi + FLAGS_OFFSET], FLAG_READABLE
jz .not_readable

What 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.