Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a jump table?

Can someone explain the mechanics of a jump table and why is would be needed in embedded systems?

like image 371
JeffV Avatar asked Sep 07 '08 01:09

JeffV


People also ask

What does a jump table do?

In computer programming, a branch table (sometimes known as a jump table) is a term used to describe an efficient method of transferring program control (branching) to another part of a program (or a different program that may have been dynamically loaded) using a table of branch instructions.

What is a jump table in C?

A jump table is a special array of pointer-to-functions. Since, this is an array, and as we know that all array elements must be of same type, therefore, it requires that all functions must be of same type and all take same number and same type of parameters.

What is jump table in Python?

Jump tables, sometimes called dispatch or branch tables, are indexed lists of functions. They are typically used when the function to be called is dependent upon a variable.


2 Answers

A jump table can be either an array of pointers to functions or an array of machine code jump instructions. If you have a relatively static set of functions (such as system calls or virtual functions for a class) then you can create this table once and call the functions using a simple index into the array. This would mean retrieving the pointer and calling a function or jumping to the machine code depending on the type of table used.

The benefits of doing this in embedded programming are:

  1. Indexes are more memory efficient than machine code or pointers, so there is a potential for memory savings in constrained environments.
  2. For any particular function the index will remain stable and changing the function merely requires swapping out the function pointer.

If does cost you a tiny bit of performance for accessing the table, but this is no worse than any other virtual function call.

like image 194
Josh Segall Avatar answered Oct 02 '22 19:10

Josh Segall


A jump table, also known as a branch table, is a series of instructions, all unconditionally branching to another point in code.

You can think of them as a switch (or select) statement where all the cases are filled:

MyJump(int c) {    switch(state)    {       case 0:          goto func0label;       case 1:          goto func1label;       case 2:          goto func2label;    } } 

Note that there's no return - the code that it jumps to will execute the return, and it will jump back to wherever myjump was called.

This is useful for state machines where you execute certain code based on the state variable. There are many, many other uses, but this is one of the main uses.

It's used where you don't want to waste time fiddling with the stack, and want to save code space. It is especially of use in interrupt handlers where speed is extremely important, and the peripheral that caused the interrupt is only known by a single variable. This is similar to the vector table in processors with interrupt controllers.

One use would be taking a $0.60 microcontroller and generating a composite (TV) signal for video applications. the micro isn't powerful - in fact it's just barely fast enough to write each scan line. A jump table would be used to draw characters, because it would take too long to load a bitmap from memory, and use a for() loop to shove the bitmap out. Instead there's a separate jump to the letter and scan line, and then 8 or so instructions that actually write the data directly to the port.

-Adam

like image 36
Adam Davis Avatar answered Oct 02 '22 19:10

Adam Davis