Jump table

from Wikipedia, the free encyclopedia

A jump table (also branch table or jump bar called; English jump table ) is in the programming , a method in which a branch the program flow by a table of jump instructions is realized.

Uses

Use for function calls

A jump table can be used to be able to call up a certain selection of functions (from an operating system or from a function library) comfortably and with a guarantee of compatibility. For this purpose, the calls (or sometimes just the bare function addresses) are arranged one after the other with a constant length as in a table in the memory. They typically consist of simple jump instructions to the point in the ROM or within the function library where the actual program code is located. In this way several purposes are fulfilled at the same time:

  • This achieves compatibility between successive software versions: Even if the actual code of one or more functions changes and the address position of the function entries changes as a result, only the address in the jump instructions in the jump table is changed, whereas the application software that uses this jump table does not needs to be adjusted.
  • You can practically address these functions via a number (their index within the table), which simplifies programming in certain areas.

Some microprocessors also support such constructs with special instructions that implement such indexed jumps.

Switch statement

Jump tables can also be created by compilers , mainly in the optimized implementation of switch instructions where the values ​​are close together.

implementation

Implementation principle of a program library in 6502 - assembler syntax:

Bibliotheksbasis:
  JMP Funktion0  ; Sprungleiste
  JMP Funktion1  ; Jede dieser Sprunganweisungen
  JMP Funktion2  ; belegt genau 3 Bytes im ROM
  ...
  JMP Funktionn
  ...
Funktion0:       ; Implementierung Funktion 0
  ...
  RTS             ; Rücksprung aus Funktionsroutine
Funktion1:       ; Implementierung Funktion 1
  ...
  RTS             ; Rücksprung aus Funktionsroutine
  ...
Funktionn:       ; Implementierung Funktion n
  ...
  RTS             ; Rücksprung aus Funktionsroutine

Calling a function n from such a library using its number:

  JSR Bibliotheksbasis+3*n

With a 68k processor, a jump instruction occupies 6 instead of 3 bytes, so the call must then be multiplied by 6.

At the high-level language level, the respective runtime library converts a function name or number into such a jump command.

Here you can also see the advantage of the method: If, for example, the implementation of Function1 is expanded when the function library is revised so that it occupies more space in the ROM, the entry points of the higher function numbers move backwards. However, the jump bar at the very beginning does not change its structure, so that the call from the application program does not have to be changed.

Examples

Jump table of the CBM kernal
  • The operating system of the Commodore - PET 2001 and its successors up to the C64 had a jump table for the most important operating system calls at the end of its ROM. For example, in all the operating systems mentioned, there was $FFD2a jump to the routine for outputting a single character (which was to be transferred in the accumulator ) on the screen at address .
  • The shared libraries (function libraries) of the Amiga OS all have such a jump table at the beginning, via which all calls to their functions are made by default.