Quiz Entry - updated: 2026.07.14
What does test %eax, %eax do and why is it used instead of cmp $0, %eax?
It ANDs %eax with itself to set the flags — a compact way to test whether the register is zero (or negative), instead of cmp $0, %eax.
test A, B computes A & B, sets flags, discards the result.
test %eax, %eax = %eax & %eax = %eax (unchanged) but sets:
- ZF = 1 if
%eax == 0 - SF = 1 if
%eax < 0(sign bit set)
Common patterns after test %eax, %eax:
| Jump | Meaning |
|---|---|
je / jz |
eax == 0 |
jne / jnz |
eax != 0 |
js |
eax < 0 (negative) |
jns |
eax >= 0 (non-negative) |
Why not cmp $0, %eax? Both work, but test is 1 byte shorter (no immediate operand) and the CPU can sometimes execute it faster.
Go deeper:
TEST instruction reference — confirms TEST ANDs the operands, sets SF/ZF/PF, and discards the result.
Bitwise operation (Wikipedia) — why x AND x leaves x unchanged, so the flags reveal zero/sign without a scratch value.