4
With PCSPIM your program is put into one big source file.
Entry points are labeled, and subroutines can be called with jal
.
With large programming projects it often happens
that the only way to call a subroutine is through
a jump table.
This might happen when several object modules are linked
together, for example
(see
Chapter One.)
Another place where this happens is with
dynamically loaded libraries.
Here, a subroutine is copied into memory only when it
is needed by another program.
The entry point is put into a jump table of available subroutines.
Here is our example program modified to give the feel of this:
.globl main .text main: subu $sp,$sp,4 # push return address sw $ra,($sp) # Assume that some initial processing determines # what subroutines are needed. Then the loader # gets those subroutines and fills in the jump table. lw $t0,jtable # get first address jalr $t0 # pass control lw $t0,jtable+4 # get second address jalr $t0 # pass control lw $ra,($sp) # pop return address addu $sp,$sp,4 jr $ra .data jtable: .word sub1 # Jump Table (pretend that the .word sub2 # addresses are filled in at run-time). .globl sub1 .text sub1: li $v0,4 la $a0,messH syscall jr $ra .data messH: .asciiz "Hello " .globl sub2 .text sub2: li $v0,4 la $a0,messW syscall jr $ra .data messW: .asciiz "World\n"
Pretend that at first only main
and its
jump table are loaded into memory,
and that the jump table is full of zeros.
Then,
later on,
the two subroutines are loaded and their addresses
are put into the jump table.
Now the only way to call the subroutines is through the jump table. The instruction:
lw $t0,jtable # get first address
... loads the first address of the jump table
into $t0.
Then the jalr
instruction calls it.
After returning from the first subroutine, the instruction:
lw $t0,jtable+4 # get second address
... loads the second address of the jump table
into $t0.
The jtable+4
means to use the
address that is four bytes away from
the symbolic address jtable
.