0 Members and 1 Guest are viewing this topic.
;before hand ld hl, tableStart ld de, $8000 ld bc, tableEnd-tableStart ldir;super fast access ld a, (toJump) add a, a add a, a ld l, a ld h, $80 jp (hl);the nops are there to make them 4 bytes;but you could remove them and multiply by 3tableStart: jp lbl0 nop jp lbl1 nop jp lbl2 nop jp lbl3 nop jp lbl4 noptableEnd:
;A holds number of routine to jump to ld hl,jumpTable add a,a add a,l ld l,a jr nc,$+3 inc h ld a,(hl) inc hl ld h,(hl) ld l,a jp (hl)jumpTable: .dw Routine0 .dw Routine1 .dw Routine2 ;... ;...
;I use these series of macros to make the code more readable and easier to type#define JTABLE call JumpTable#define JT(xx,xxxx) .db xx \ .dw xxxx#define JTEND .db 0;'a' holds a value and you want to jump to the label associated with the value.JTABLEJT(value1,label1)JT(value2,label2)JT(value3,label3);...;...JTEND;This is the jump table routineJumpTable: ex (sp),hl push bc ld b,aJumpTableLoop: ld a,(hl) inc hl or a jr z,hJumpTableEnd cp b jr z,JumpTableEnd inc hl inc hl jr JumpTableLoopJumpTableEnd: ld a,(hl) inc hl ld h,(hl) ld l,ahJumpTableEnd: ld a,b pop bc ex (sp),hl ret