go to previous page   go to home page   go to next page hear noise


jal address

The jalr Instruction

A specific jal instruction in a program always calls the same subroutine. For example, here is how main would usually call the first subroutine:

jal sub1

But what if you wanted the same instruction to call different subroutines depending on circumstances? This is where a jump table is useful. The table contains a list of subroutine entry points. To call a subroutine, copy its address from the table into a register. Now use the following instruction:

jalr r     # $ra <— PC+4  $ra <— return address 
           # PC  <— $r    load the PC with the address in $r

This works just like the jal instruction except the address of the subroutine now comes from a register.


Here is a section of main. Fill in the blanks so that control is passed to sub1:

          lw      $t0,     # get the first entry point in the Jump Table
          jalr             # pass control to sub1
          li      $v0,10          # return to OS

sub1add:  .word   sub1            # Jump Table
sub2add:  .word   sub2