Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Part F
Reading:
Name
Usage
$zero
$at
$v0 $v1
$a0 $a3
$t0 $t7
$s0 $s7
$t8 $t9
$k0$k1
$gp
$sp
$fp
$ra
constant 0
reserved for assembler
returned values (procedures)
arguments (procedures)
temporary values
saved values
Temporary values (contd)
reserved for operating system
pointer to global area (static data segment)
stack pointer
frame pointer (procedures)
return address (procedures)
3
The Basics
From your previous study (e.g., Java), you know the basics of
procedures (i.e., methods in Java):
Declaration: a procedure has a header, body, and local
variables
Arguments (parameters): a procedure receives argument
values to work with.
Return value: a procedure may return a value to the caller.
Nesting: a procedure may call other procedures.
Examples:
Example
Meaning
jal label
Jump register
jr
$ra
Comment
What is the
problem
here?
Practice
What is the problems with this MIPS code?
Register Usage
MIPS Convention #2:
$s0 to $s7 are callee-saved registers, i.e., $s0-$s7 should
have the same values before and after the procedure
The caller expects to find these registers unchanged after the call.
Therefore, the callee must save/restore them if they are altered.
Memory
(activation record)
LIFO (Last In First Out)
0x 7FFF FFFF
$fp
Stack
$sp
Dynamic data
(Heap)
0xFFFF FFFF
$gp
Static data
Size of memory
= 232 bytes
= 230 words
= 4 GB
0x1000 8000
0x1000 0000
PC
Text
(Your code)
Reserved
0x0040 0000
0x0000 0000
11
Memory
0x 7FFF FFFF
Registers
$sp
New Data
0x0000 0000
12
# 2.
# 3. PULL original values
from stack:
lw
$s0, 4($sp)
lw
$s1, 0($sp)
addi $sp,$sp,8
Memory
Registers
9524
0x 7FFF FFFF
1753
$sp
$s0 76
5
$s1 13
9
13
# 1. PUSH :
addi $sp,$sp,-8
sw
$s0, 4($sp)
sw
$s1, 0($sp)
0x0000 0000
13
14
Nested Procedures
A procedure may call another procedure
e.g., A calls B, then B calls C
What could go wrong?
Problems related to $ra
When A calls B, MIPS saves the return address in $ra
When B calls C, the value of $ra is overwritten by new a return address!
Problems related to $a0-$a3
If B calls C with arguments, values of $a0 to $a1 (arguments passed from A
to B) are overwritten by new argument set (arguments passed from B to C).
0x500000
A:
jal B
0x500014
0x500018
B:
jal C
C:
jr
$ra
$ra
0x500018
0x500004
$a0
0x12
0x5FA
$a1
0x43
0xC103
$a2
0x15
0xFAC5
$a3
0x30
0x945A
15
and
$a0-$a3 in the stack, and it should restore them after returning
from the procedure.
16
17
Local variables
Example 4: local variables
18
jal A
A:
jal B
B:
jal C
C:
Memory
$sp
High address
main frame
A frame
B frame
C frame
Low address
20
$fp
$sp
Local data
21
Prologue
(as a callee)
(as a callee)
(as a caller)
Body
(as a caller)
Do computation
After returning from a nested call, restore if changed and will be used
again: $a0 - $a3 and $t0 - $t9
// lw $?, ?($sp)
Epilogue
(as a callee)
(as a callee)
(as a caller)
(as a callee)
(as a callee)
// ...
// lw $s?, ?($sp)
// lw $ra, ?($sp)
// addi $sp, $sp, framesize
22
Recursive Procedures
A recursive procedure is the one that conditionally calls itself.
The procedure is thus a caller and callee.
fact(1) frame
fact(0) frame
23
Practice
Translate the function below into MIPS assembly language.
Write a test program for your function. Assume that initially
a = 10, b = 20, c = 5, d = 4
25
Practice
Translate the function below into MIPS code. Write a test
program to compute fact(10)
26