Quiz Entry - updated: 2026.07.10
What does the TEST instruction do and when is it useful?
TEST computes Src1 & Src2 (bitwise AND), sets ZF and SF, and discards the result — it's the AND counterpart to CMP's subtract.
test shines when you want to inspect bits rather than compare magnitudes, especially with a mask.
testq Src2, Src1 # compute Src1 & Src2, set ZF/SF, store nothing
- ZF set when
Src1 & Src2 == 0 - SF set when the AND's top bit is 1
Common patterns:
testq %rax, %rax # is %rax zero? (a value AND itself is 0 only if 0)
jz .is_zero
testq $0x1, %rax # is bit 0 set?
jnz .bit_set
testq %rax, %rax # is %rax negative?
js .is_negative
This is exactly the tool for C tests like if (a & 0xffff) where one operand is a bit mask.
Why testq %rax,%rax over cmpq $0,%rax? It's a shorter encoding with no immediate and no subtraction — the standard, compact zero-check that compilers emit everywhere.
Go deeper:
Felix Cloutier — TEST — TEST as the AND-based flag setter.