(ii) By placing it in a register designated for this purpose.
register that is used for linkage is register
which is called
$ra by the extended assembler.
It holds the return address for a subroutine.
The instruction that puts the return address into
is (usually) the
$31 is one of the two "general purpose registers"
that behave differently from the others.
(The other one is register
jal instruction and register
the hardware support necessary to elegantly implement subroutines.
jal works, review the machine cycle.
The MIPS endlessly cycles through
three basic steps.
Each cycle executes one machine instruction.
(This is a somewhat simplified view, but sufficient for now).
jal instruction does the following
in the execute phase of the machine cycle:
jal sub # $ra <― PC+4 (the address 8 bytes away from the jal) # PC <― sub load the PC with the subroutine entry point # a branch delay slot follows this instruction
the middle step of the machine cycle has already incremented
the PC by four.
At this point the PC holds the address of the instruction
just after the
Now the execute phase of the
adds four to that address
and puts the result in
$ra holds the address of the second instruction
The correct return address is "address of the
jal plus eight".
This is because: (i) returning from the subroutine
jal instruction would be a disaster
(since it would execute again, sending control back to the subroutine),
(ii) the instruction following the
is a branch delay slot.
What instruction is usually placed in the branch delay slot?