I am trying to do a jump table in C like this
cmp eax, dword 3 ;max number
ja invalid ;default
jmp [eax*4+jumptable] ;jump to handler
invalid:
retn
jumptable:
dd handle0, handle1, handle2, handle3
handle0:
....
and so forth
I started with this
void dohandler(int a, int b, void (*jumptable[4]) (int x))
if (a > 3) return
else jumptable[3 * 4](b);
}
int main() {
void (*jumptable[4]) (int x);
jumptable[0] = &handler1;
... etc
dohandler(1,2,jumptable);
}
void handler1(int x) { ... }
.. etc
but its not working well..
Not sure, but I think you have a couple of problems here:
First, you don't use
a
in referencing your jump table, and I think that corresponds to theeax
register in the assembly version. Second, don't multiply that by 4. I think that's intended to be the size of a pointer, and the array reference does that for you. So you end up with this: