Flags, cmp & test
Assembly has no if. Decisions are made in two steps: an instruction sets status flags in the rflags register, and a later instruction reads them (a conditional jump, setcc or cmov). This lesson is about the first step.
The status flags
| Flag | Name | Set to 1 when the result… |
|---|---|---|
| ZF | zero flag | is zero |
| SF | sign flag | has its top bit set (is negative when read as signed) |
| CF | carry flag | produced a carry out of the top bit (unsigned overflow), or needed a borrow in subtraction |
| OF | overflow flag | is wrong when read as signed (signed overflow) |
| PF | parity flag | has an even number of 1 bits in its lowest byte (rarely used) |
Arithmetic and logic instructions (add, sub, and, or, xor, neg, shifts…) update the flags as a side effect. mov, lea, push, pop and jumps do not change them.
Seeing the flags
To make the flags visible, the examples in this lesson use a helper, print_flags. It copies rflags into rax with pushfq/pop, extracts the four interesting bits (CF is bit 0, ZF bit 6, SF bit 7, OF bit 11) with bt ("bit test", which copies one bit into CF) and setc (which stores CF as 0 or 1), and patches them into a line of text.
section .text
global _start
_start:
mov eax, 5
sub eax, 5 ; 0: ZF=1
call print_flags
mov eax, 5
sub eax, 7 ; -2: SF=1, and a borrow: CF=1
call print_flags
mov al, 200
add al, 100 ; 300 doesn't fit in a byte: CF=1
call print_flags
mov al, 100
add al, 100 ; 200 > 127: wrong as signed, OF=1 (and SF=1)
call print_flags
mov eax, 60
xor edi, edi
syscall
; ---- print_flags: print ZF, SF, CF and OF ----
section .data
flags_text: db "ZF=0 SF=0 CF=0 OF=0", 10
section .text
print_flags:
pushfq
pop rax ; rax = rflags
bt eax, 6 ; ZF
setc dl
add dl, '0'
mov [flags_text + 3], dl
bt eax, 7 ; SF
setc dl
add dl, '0'
mov [flags_text + 8], dl
bt eax, 0 ; CF
setc dl
add dl, '0'
mov [flags_text + 13], dl
bt eax, 11 ; OF
setc dl
add dl, '0'
mov [flags_text + 18], dl
mov eax, 1
mov edi, 1
mov rsi, flags_text
mov edx, 20
syscall
retZF=1 SF=0 CF=0 OF=0
ZF=0 SF=1 CF=1 OF=0
ZF=0 SF=0 CF=1 OF=0
ZF=0 SF=1 CF=0 OF=1
Look at the last two lines carefully: CF is about unsigned numbers, OF is about signed numbers. 200 + 100 overflows a byte when the values are unsigned (0–255), but as signed bytes 200 is really -56, and -56 + 100 = 44 is fine, so OF=0. 100 + 100 = 200 is fine unsigned, but not in the signed range (−128…127), so OF=1. The CPU computes both and lets you pick the interpretation.
cmp: compare by subtracting
cmp a, b computes a - b, sets the flags exactly like sub, and throws the result away. Both operands are unchanged. After a cmp, the flags tell you how a relates to b:
| Relation | Flags after cmp a, b |
|---|---|
a == b |
ZF = 1 |
a < b unsigned |
CF = 1 |
a < b signed |
SF ≠ OF |
section .text
global _start
_start:
mov eax, 3
cmp eax, 3 ; equal
call print_flags
mov eax, 3
cmp eax, 10 ; 3 < 10 in every interpretation
call print_flags
mov eax, -1
cmp eax, 10 ; signed: -1 < 10, but unsigned: 0xFFFFFFFF > 10
call print_flags
mov eax, 60
xor edi, edi
syscall
; ---- print_flags: print ZF, SF, CF and OF ----
section .data
flags_text: db "ZF=0 SF=0 CF=0 OF=0", 10
section .text
print_flags:
pushfq
pop rax ; rax = rflags
bt eax, 6 ; ZF
setc dl
add dl, '0'
mov [flags_text + 3], dl
bt eax, 7 ; SF
setc dl
add dl, '0'
mov [flags_text + 8], dl
bt eax, 0 ; CF
setc dl
add dl, '0'
mov [flags_text + 13], dl
bt eax, 11 ; OF
setc dl
add dl, '0'
mov [flags_text + 18], dl
mov eax, 1
mov edi, 1
mov rsi, flags_text
mov edx, 20
syscall
retZF=1 SF=0 CF=0 OF=0
ZF=0 SF=1 CF=1 OF=0
ZF=0 SF=1 CF=0 OF=0
In the last case SF=1 and OF=0, so SF ≠ OF: "less than" as signed numbers. But CF=0: "not below" as unsigned numbers. The same comparison gives different answers depending on which flags you look at — the next lesson's conditional jumps come in signed and unsigned versions for exactly this reason.
test: check bits by AND-ing
test a, b computes a AND b, sets ZF and SF from the result, clears CF and OF, and discards the result. Two classic uses:
test rax, rax— israxzero (ZF) or negative (SF)? Shorter thancmp rax, 0.test al, 1— is the lowest bit set, i.e. is the number odd?
setcc: a flag into a register
The setcc family stores a condition as a byte, 1 if it is true and 0 otherwise. The condition names are the same as for jumps: sete/setz (equal), setne, setl (signed less), setb (unsigned below), setg, seta, sets, setc, …
section .text
global _start
_start:
mov ebx, 37
test bl, 1 ; odd?
setnz al ; al = 1 if the AND result is not zero
movzx edi, al
call print_uint ; 37 is odd: 1
mov ebx, -5
cmp ebx, 2
setl al ; signed: -5 < 2
movzx edi, al
call print_uint ; 1
cmp ebx, 2
setb al ; unsigned: 0xFFFFFFFB < 2 ?
movzx edi, al
call print_uint ; 0
mov eax, 60
xor edi, edi
syscall
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret1
1
0
Note:
setccwrites only an 8-bit register, so the rest of the register keeps its old contents. Zero-extend withmovzxbefore using the result as a number — or zero the full register withxorbefore the comparison (never between the comparison and thesetcc, becausexorchanges the flags).
Test yourself
What does cmp rax, rbx do with its result?
- Stores it in rax
- Stores it in rbx
- Discards it, keeping only the flags
- Pushes it on the stack
After mov al, 255 / add al, 1, which flags are set?
- ZF and CF
- SF and OF
- Only OF
- None
Which flag indicates overflow for SIGNED arithmetic?
- CF
- ZF
- OF
- PF