Quiz Entry - updated: 2026.07.06
How do you recognize a switch statement / jump table in assembly?
Look for an unsigned bounds check (cmp + ja) followed by an indirect jump through a table, jmp *table(,%reg,8).
* Bounds check with ja, then an indirect jump through a table of 8-byte .quad addresses. Each table entry is one case; *8 because addresses are 8 bytes on x86-64. *
cmp $6, %rdi
ja .L_default
jmp *.L4(,%rdi,8)
The pattern:
- Bounds check:
cmp $N, %reg+ja default— ensures index is 0..N - Indirect jump:
jmp *table(,%reg,8)— jumps through a table of addresses - Jump table in
.rodata: array of.quadaddresses, one per case
How to read the jump table in GDB:
(gdb) x/8a 0x402500
0x402500: 0x400e73 (case 0)
0x402508: 0x400e90 (case 1)
...
Why *8? Each entry is a 64-bit address (8 bytes) on x86_64.
Key insight: Compilers use jump tables when switch cases are dense (few gaps). Sparse cases get compiled as if-else chains instead.
Go deeper:
Branch table (Wikipedia) — Explains why compilers emit jump tables for dense switch cases, the exact construct in the diagram.
JMP incl. indirect jumps (felixcloutier) — Documents the indirect jmp through a memory operand that dispatches through the table.