Nested Subroutine Call
Nested Subroutine Call
sw
$a0, 0($sp)
$sp
a0
ra
$fp
OLD
NEW
calling program
procedure fact
4000
push ra
996
a0 = n (3)
4004
push a0
1000
1004
a0=n-1
jal fact (4000)
$sp
a0 = 1
n=3
a0
result
v0
ra = 4024
a0 = 2
ra= 4024
a0 = 3
ra = 1004
# if n 1 then goto L1
# return 1 to $v0
jr
# return
$ra
fact
# decrement n
# call fact with (n 1)
lw
$a0, 0($sp)
# restore argument n
lw
$ra, 4($sp)
# pop 2 items
# return n * fact(n-1)
jr
# return to caller
$ra
Stack pointer
Low address
Temporary local
variables
Return address
Saved argument
registers beyond
a0-a3
Growth of stack
Frame
pointer
High address
A translation hierarchy
HLL program
COMPILER
Assembly language program
ASSEMBLER
Machine language module
LINKER
Library routine
LOADER
Memory
. text
{Program instructions here}
. data
{Data begins here}
. byte 84, 104, 101
. asciiz The quick brown fox
. float f1,. . . , fn
. word w1, . . . . wn
. space n {reserve n bytes of space}