jalr
Jump and link register
Saves the return address in $t1 and jumps to the address in $t2; with one register, the return address goes to $ra.
Forms
jalr ,jalrSaves the return address (PC + 4) in $ra, then jumps to the address in $t1. One-operand shorthand for 'jalr $ra,$t1'. Used to call a function via a pointer stored in a register.
Operands
$t1- Any general-purpose register,
$0to$31, using its numeric or named spelling, such as$8or$t0($zerois$0;$rais$31). $t2- A general-purpose register. It may be the same register as
$t1.
Allows calling a function whose address is only known at runtime (function pointer). Return with 'jr $t1'.
# jalr calls a local routine and writes the return address to $ra. Inspect $t2: 1 after return.
la $t0, routine
jalr $t0
li $t2, 1
b done # Skip the routine after returning.
routine: li $t2, 2
jr $ra # Return to the instruction after the call.
done: