7.5. Jump Tables¶
A jump table is a structure that holds a list of function addresses. Each entry in the table corresponds to a different job that might need to be done and holds the address of the code that will handle that job.
Below is what a three entry jump table might look like. There are locations for three addresses at 0x100000, 0x100004, and 0x100008 that would correspond to three jobs we want to be able to do. (Job A, B, and C).
To do Job A, we would look at the address table + 0
get the address stored there (0x100014), then jump to that location, and
execute the instructions we find - the code for function1.
To do Job B, we will look at address table + 4
, find 0x100034,
jump to that location, and execute the code we find - that for function3.
Address | Contents | |
---|---|---|
0x000ffffc | ... | |
0x00100000 | 0x100014 | table[0] = Job A = function1 |
0x00100004 | 0x100034 | table[1] = Job B = function3 |
0x00100008 | 0x100020 | table[2] = Job C = function2 |
0x0010000c | ... | |
0x00100010 | ... | |
0x00100014 | Code for function1 | |
0x00100018 | ... | |
0x0010001c | ... | |
0x00100020 | Code for function2 | |
0x00100024 | ... | |
0x00100028 | ... | |
0x0010002c | ... | |
0x00100030 | ... | |
0x00100034 | Code for function3 | |
0x00100038 | ... | |
0x0010003c | ... | |
0x00100040 | ... |
The jump table allows us to say “I don’t know where I will be branching too, but I know the address I need will be stored in the jump table”. Our code can be written to go load that address and branch to it, even if we don’t know what it will be. Other code can modify the jump table to make sure that at run time the jump table if filled with the correct addresses.
This approach is used in many places where such flexibility is needed. Processors use this technique to handle exceptions - each particular kind of exception will automatically look at a different address to find the address of the exception handler to call. The operating system or program running on the hardware has the responsibility for providing code to handle such exceptions and making sure that the address of their functions are loaded into the correct locations in the exception jump table.
Jump tables are also used to implment virtual function calls.