Code: Select all
; a = index (0..42)
; b = spare
ld b, a ; Multiply index by 3
add a, b
add a, b
ld ($+4), a ; Overwrite the jump destination
jr $ ; Jump to the table
jp Routine1
jp Routine2
jp Routine3
...
I'm multiplying the index by 3 since that's how long a JP instruction is (I'm assuming that the targets may be anywhere). If you can really ensure that all target addresses are close enough (within around 127 bytes*) then it'd look more like this:
Code: Select all
; a = index (0..63)
add a, a ; Multiply index by 2
ld ($+4), a ; Overwrite the jump destination
jr $ ; Jump to the table
jr Routine1
jr Routine2
jr Routine3
...
*I say "around" since it needs to be 127 bytes away from the specific jump, so the actual maximum distance from the start of the code depends on which entry is being jumped to. Still should give a good idea of how far away it can afford to be.