Step Into x64 Assembly · Lesson 14 · Speed & Dispatch
Switch Statements and Jump Tables
How compilers turn a switch into one indirect jump, tables of function pointers, and reading one in real compiler output.
- ~35 minreading and coding
- 4missions
- membersaccess
- x64 Assemblylanguage
Part of Step Into x64 Assembly. The full lesson, its missions and the real compiler and debugger are in the course. Try a free lesson first to see how it works.
A switch with 2 cases becomes 2 compares. With 50 cases that would be 50 compares, so compilers build a jump table instead: an array of addresses, one per case, indexed by the value. One bounds check and one indirect jump, however many cases there are.
In this lesson
- A chain of compares does not scale
- The unsigned bounds check
- Tables of function pointers
- In real compiler output
Missions
- A switch by hand
- Half the size: relative offsets
- A table of functions
- Read the compiler's switch