0

I know that jump tables are mainly used to create switch statements in assembly:

int a = 5;
switch (a){
  case 5:
  ...
   break;
  ...
} 

In this case, jump is just a pointer to a memory address which has instructions to do case 5 work.

If i'm not mistaking, a lookup table has pre calculated results in an array? so instead of writing code to calculate them you just return the array index? Sort of like a HashMap.

Above two sound very similar to me, are they basically the same thing? One points to instructions and the other returns pre calculated results?

Anthon
  • 3
  • 3
  • 1
    I would see a jump table as a special case of a lookup table; a lookup table that contains branch instructions. – Ctx Jun 22 '20 at 14:30
  • Rather branch addresses – 0___________ Jun 22 '20 at 14:42
  • Nothing like jump table exists in the C language so why did you tag it as `C`? – 0___________ Jun 22 '20 at 14:44
  • @P__J__ An array of function pointers is a jump table. In fact many embedded systems implement their interrupt vector table as an array of function pointers written in C - functions to be called by the hardware. – Lundin Jun 22 '20 at 14:46
  • @P__J__ No, it can really contain the branch instructions. – Ctx Jun 22 '20 at 14:52
  • @P__J__ Maybe because OP does now know that? :) – klutt Jun 22 '20 at 14:56
  • `switch` instructions of packed (ie sequential, or at least linear) values are commonly translated into jump tables if you have a good optimizing compiler in C. – Blindy Jun 22 '20 at 14:57

3 Answers3

0

If i'm not mistaking, a lookup table has pre calculated results in an array? so instead of writing code to calculate them you just return the array index? Sort of like a HashMap.

Correct. What's stored at that index could be data, or a pointer to data, or a pointer to a function etc etc.

A jump table is simply a look-up table where each index corresponds to a function, most commonly implemented in C as an array of function pointers.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • So then in case of `lookup tables`, what's the point of having a table to begin with? can't a code just return a `constant` instead of looking it up in a table? or is there an algorithm which takes and input and outputs a value from table? similar to a jump table. – Anthon Jun 22 '20 at 14:51
  • @Anthon How could the code know which one to return without a table? You can't store that information in thin air. The table may be part of the application data, or it may be part of the machine code, but it must still be allocated somewhere. – Lundin Jun 22 '20 at 15:01
0

Jump tables are, as you mention, native code that's arranged in a way that's easy to jump to depending on integer values. If the jump table is comprised of just short jump instructions, you can multiply your switch value by 3 and add the offset of the first entry in the table and jump (JMP or CALL) to it.

Lookup tables on the other hand are just raw data. It's still stored in a packed format so you can access it through a linear operation on your index (size * index + offset), but to make use of it you use an indirect move (MOV dest, [expression]) instead of physically jumping to it.

Keep in mind that lookup tables are just an optimization, albeit a huge one, you can load values into registers with a jump table as well. This is an is-a relationship.

Blindy
  • 65,249
  • 10
  • 91
  • 131
0

What happens under the hood is up the compiler. But you're right on your observations. Here is a snippet demonstrating what compilers often do to switch statements:

#include <stdio.h>

void foo(void) { printf("foo\n"); }
void bar(void) { printf("bar\n"); }

int main(void)
{
    // Array of size 2 of pointer to function without arguments returning void
    // Yes, declaring function pointers is not intuitive...
    void (*f[2])(void);

    f[0] = foo;
    f[1] = bar;

    int x;
    printf("Enter a number (0 or 1): ");
    scanf("%d", &x);

    printf("Using switch\n");
    switch(x) {
    case 0: foo(); break;
    case 1: bar(); break;
    }

    printf("Using array of function pointers\n");
    f[x]();
}
klutt
  • 30,332
  • 17
  • 55
  • 95