RosettaCodeData/Task/Conditional-structures/MIPS-Assembly/conditional-structures-5.mips

27 lines
633 B
Plaintext

switchExample:
;this implementation assumes that all destinations end in jr ra, so you'll need to arrive here with JAL switchExample.
;$t0 = index (must be a multiple of 4 or the program counter will jump to a location that's not guaranteed to properly return.)
la cases,$t1
addiu $t1,$t0 ;MIPS can't do variable indexed offsetting so we have to add the offset ourselves.
lw $t8,($t1) ;dereference the pointer, $t8 contains the address we wish to "call"
nop
jr $t8 ;jump to the selected destination.
nop
cases:
.word foo
.word bar
.word baz
foo:
;code goes here
jr ra
bar:
;code goes here
jr ra
baz:
;code goes here
jr ra