.text
main:
. . . .
lw $t0,sub1add # get first address in Jump Table
jalr $t0 # pass control to sub1
. . . .
.data
sub1add: .word sub1 # Jump Table
sub2add: .word sub2
Confusion Alert: Notice that the
instruction that is used to load $t0 with
the entry point to sub1
is a lw sub1addsub1add,
which is what a lw
instruction does.
Here is a full program that includes a
mainjalr
.globl main
.text
main:
subu $sp,$sp,4 # push return address
sw $ra,($sp)
lw $t0,sub1add # get first address
jalr $t0 # pass control
lw $t0,sub2add # get second address
jalr $t0 # pass control
lw $ra,($sp) # pop return address
addu $sp,$sp,4
jr $ra
.data
sub1add: .word sub1 # Jump Table
sub2add: .word sub2
.text
sub1: li $v0,4
la $a0,messH
syscall
jr $ra
.data
messH: .asciiz "Hello "
.text
sub2: li $v0,4
la $a0,messW
syscall
jr $ra
.data
messW: .asciiz "World\n"