6502 provides the following to control program flow, i.e. modify the PC register.
- JMP absolute
- JMP indirect
- Bxx relative instructions
- JSR absolute with a later RTS
- BRK or other IRQ with a later RTI (or RTS if you pull
.P
off the stack)
- Pushing two values to the stack and then RTS/RTI
- Hardware reset causes jump through the reset vector
That's it. If you want anything more complicated you need to create it using one or more of the above.
One way to implement a switch statement is to first create a table of pointers to all routines involved in the switch statement. Split them according to the low bytes of the routines and then the high bytes:
switchtab_lo .db >routine1, >routine2, >routine3
switchtab_hi .db <routine1, <routine2, <routine3
(I can never remember if > means low byte or high byte and different assemblers may have different syntax)
and then, assuming the value you want to switch upon is in .X, and that vector
is two bytes that don't start at the end of the page (to avoid the JMP indirect bug) and you've made sure it's a valid value:
lda switchtab_lo,X
sta vector
lda switchtab_hi,X
sta vector+1
jmp (vector)
It's tedious to do this each time you need to switch but that's why high level languages were invented.