CSCI-365 Computer Organization
CSCI-365 Computer Organization
Computer Organization
Lecture 5
Note: Some slides and/or pictures in the following are adapted from:
Computer Organization and Design, Patterson & Hennessy, ©2005
Some slides and/or pictures in the following are adapted from:
slides ©2008 UCB
Function Call Bookkeeping
sp c
b b
a sp a x = mem[sp]
sp = sp – 4
sp = sp + 4
mem[sp] = c
call Proc B
Proc B’s values …
call Proc C
Proc C’s values …
… return
Stack grows return
this way return
Low address
Using the Stack
Restore
jr $ra
jal xyz
Restore
Text version jr $ra jr $ra
is incorrect
10000000
Addressable Static data
with 16-bit 10008000
signed offset Data segment
1000ffff
Dynamic data
7ffffffc
80000000
Second half of address
space reserved for
memory-mapped I/O
Overview of the memory address space in MIPS.
Basic Structure of a Function
Prologue
entry_label: ra
addi $sp,$sp,-framesize
sw $ra, framesize-4($sp) # save $ra
save other regs if need be
...
memory
Body (call other functions…)
Epilogue
restore other regs if need be
lw $ra, framesize-4($sp) # restore $ra
addi $sp,$sp,framesize
jr $ra
Example
• $ra: Can Change. The jal call itself will change this
register. Caller needs to save on stack if nested call
• $v0-$v1: Can Change. These will contain the new
returned values
• $a0-$a3: Can Change. These are volatile argument
registers. Caller needs to save if they’ll need them after
the call
• $t0-$t9: Can Change. That’s why they’re called
temporary: any procedure may change them at any time.
Caller needs to save if they’ll need them afterwards
Register Conventions
$sp z
Local y Frame for
..
variables . current
Saved procedure
registers
Old ($fp)
$sp c $fp c
b Frame for b Frame for
a current a previous
.. procedure .. procedure
. .
$fp
Saving $fp, $ra, and $s0 onto the stack and restoring
them at the end of the procedure