I have some places in my game's code where I do a series of cmp and then beq to decide where to branch based on some state (usually a simple enumeration counting from 0). I've seen a couple of hints on here about how to implement a simple branch table, but I was wondering what might be the best way of doing this. My current idea:
My very first thought was to have a table of words with the absolute addresses of the branch labels, but then I thought you'd have to shift your state left by 1 bit (unless you make sure your states are already 0, 2, 4, 6, etc.) before finding the entry in the table.
Code:
locationlowbyte:
.byte <branch1 ;state 0
.byte <branch2 ;state 1
locationhibyte:
.byte >branch1 ;state 0
.byte >branch2 ;state 1
routine:
ldx state
lda locationlowbyte,x
sta w0
lda locationhibyte,x
sta w0+1
jmp (w0)
branch1:
;do stuff
jmp switchbreak
branch2:
;do different stuff
switchbreak:
.byte <branch1 ;state 0
.byte <branch2 ;state 1
locationhibyte:
.byte >branch1 ;state 0
.byte >branch2 ;state 1
routine:
ldx state
lda locationlowbyte,x
sta w0
lda locationhibyte,x
sta w0+1
jmp (w0)
branch1:
;do stuff
jmp switchbreak
branch2:
;do different stuff
switchbreak:
My very first thought was to have a table of words with the absolute addresses of the branch labels, but then I thought you'd have to shift your state left by 1 bit (unless you make sure your states are already 0, 2, 4, 6, etc.) before finding the entry in the table.