Download Procedures - Computer Systems - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
CSE 410Computer Systems
L^
t^
P
d
Lecture 6 & 7 – Procedures
(aka functions, subroutines, methods…)
An Example Function: FactorialAn
Example Function: Factorial
fact:
li
$t
1
f
1
int
fact(int
n)
li
$t0,
1
f =
1
move
$t1, $a
i
= n
loop:
blez
$t1, exit
exit if i<=
int
i, f; f^
=^
for
(i
n;
i >
i--)
f^
=^
f^
mul
$t0, $t0, $t
f *= I
addi
$t1, $t1, -
i—
j^
loop
exit
f =
f
} return f; }
move
$v0, $t
result in $v
jr
$ra
return
3
Register CorrespondencesRegister
Correspondences
$zero
Zero (always)
$at
Assembler temp
$v0-$v
Value (return from function)
$a0-$a
Argument (to function)
$a0 $a
Argument (to function)
$t0-$t
Temporaries
$s0-$s
Saved Temporaries
$t8 $t
T^
i
$t8-$t
Temporaries
$k0-$k
Kernel (OS) Registers
$gp
Global Pointer Saved
$sp
Stack Pointer Saved
$fp
Frame Pointer Saved
$ra
Return Address Saved
$ra
Return Address Saved
5
Control flow in CControl
flow in C
-^
Invoking a function changes the
t^
l fl
f^
t^
i
int main(){
control flow of a program twice.1. Calling the function2. Returning from the functionI^
thi
l^
th
i^
f^
ti
...t
fact(8); t
fact(3); t
t
t2;
-^
In this example the main functioncalls
fact
twice, and fact returns
twice—but to
different
locations in
main.
t
t
t2;
-^
Each time fact is called, the CPUhas to remember the appropriatereturn address.
int fact(int n){
int i,
f
for (i =
n; i > 1; i--)
-^
Notice that main itself is also afunction! It is called by theoperating system when you runthe program
for (
i^
=^
n;
i
i
f^
=^
f^
*^
i;
return
f;
6
the
program.
Data flow in CData
flow in C
•^
Functions accept
int main(){
arguments and producereturn values.
•^
The blue parts of the
...t
= fact(8); t
= fact(3); t
t
t
•^
The
blue parts of the
program show theparameters and
f h
f
t
= t1 + t2; ... } i t f
t(i t
arguments of the factfunction.
•^
The purple parts of the
int fact(int n){
int i,
f
for (i = n; i >
1; i--)
f^
f * i
The
purple parts of the
code deal with returningand using a result.
f^
= f * i; return
f;
8
Data flow in MIPSData
flow in MIPS
•^
MIPS uses the following conventions for function
g
arguments and results.– Up to four function arguments can be “passed” by
placing them in argument registers $a0-$a3 beforeplacing them in argument registers $a0 $a3 beforecalling the function with jal.
– A function can “return” up to two values by placing them
in registers $ 0 $ 1 before ret rning
ia jr
in registers $v0-$v1, before returning via jr.
•^
These conventions are not enforced by the hardware orassembler, but programmers agree to them so functionswritten by different people can interface with each other.
•^
We may have time later to talk about dealing with longerargument lists or more complex return values
9
a gu
e t
sts o
o e co
p e
etu
a ues
The big problem so farThe
big problem so far
•^
There is a big problem here!
g p
– The main code uses $t1 to store the result of
fact(8).
– But $t1 is also used within the fact function!
•^
The subsequent call to fact(3) will overwrite the valueof fact(8) that was stored in $t1of fact(8) that was stored in $t
11
Nested functions
A:
...# P t
B’
i^
Nested
functions
-^
A similar situation happensh
ll^
f^
ti^
th t
#^
Put
B’s args in $a0-$a
jal
B^
$ra =
A
A2:
when you call a function thatthen calls another function.
-^
Let’s say A calls B, which callsC
B:
...# Put C’s args in $a0-$a3,#^
i^
B’
C. – The arguments for the call
to C would be placed in$ 0 $ 3 th
iti
#^
erasing B’s args! jal
C^
$ra =
B
B2:
...jr
$ra
#^
Where does
thi
$a0-$a3, thus
overwriting
the original arguments forB. Si
il^
l^
j l C
it^
thi
s go???
C
-^
Si
milarly, jal C overwrites the return address that wassaved in $ra by the earlierjal B
12
C:
...jr
$ra
jal
B
.
Who saves the registers?Who
saves the registers?
•^
Who is responsible for saving important registers acrossf^
ti^
ll?
function calls?– The caller knows which registers are important to it and
should be saved.The callee knows exactly which registers it will use and
– The callee knows exactly which registers it will use and
potentially overwrite.
•^
However, in the typical “black box” programmingapproach, the caller and callee do not know anythingapproach, the caller and callee do not know anythingabout each other’s implementation.– Different functions may be written by different people
or companies.
– A function should be able to interface with any client,
and different implementations of the same functionshould be substitutable.
So how can two functions cooperate and share registers
•^
So how can two functions cooperate and share registerswhen they don’t know anything about each other?
14
The caller could save the registersThe
caller could save the registers…
•^
One possibility is for the
ll^
t^
i^
t^
t^
frodo
:^
li
$a
caller
to save any important
registers that it needs beforemaking a function call, and torestore them after
frodo
:^
li
$a
li
$a1,
li
$s0,
li
$s1,
restore
them after.
•^
But the caller does not knowwhat registers are actuallywritten by the function so it
#^
Save
registers
#^
$a0,
$a1,
$s0,
$s
jal
gollum
written
by the function, so it
may save more registersthan necessary.
•^
In the example on the right
jal
gollum
Restore registers#^
$a0,
$a1,
$s0,
$s
•^
In
the example on the right,
frodo wants to preserve $a0,$a1, $s0 and $s1 fromgollum, but gollum may not
add
$v0, $a0, $a
add
$v1, $s0, $s
jr
$ra
15
gollum
,^ but gollum may not
even use those registers.
or they could work together
…or they could work together•^
MIPS uses conventions again to split the register spilling chores.
-^
The
caller
is responsible for saving and restoring any of the
The
caller
is responsible for saving and restoring any of the
following caller-saved registers that it cares about.
$t0-$t
$a0-$a
$v0-$v
In other words, the callee may freely modify these registers, underthe assumption that the caller already saved them if necessary.
-^
The
callee
is responsible for saving and restoring any of the
The
callee
is responsible for saving and restoring any of the
following callee-saved registers that it uses. (Remember that $ra is“used” by jal.)
$s0-$s
$ra
Thus the caller may assume these registers are not changed by thecallee.
17
- $ra is tricky; it is saved by a callee who is also a caller.
Register spilling exampleRegister
spilling example
-^
This convention ensures that the caller and callee together save allf th
i^
t^
t^
i t
f^
d^
l^
d^
t^
i t
$ 0
of the important registers—frodo only needs to save registers $a0and $a1, while gollum only has to save registers $s0 and $s2.
f^
d^
li
$ 0
3
gollum
:
frodo:
li
$a0, 3
li
$a1, 1
li
$s0, 4
li
$s1, 1
gollum:
Save registers# $s0 and $s2li
$a0, 2
Save registers# $a0 and $a1jal
gollum
li
$a2, 7
li
$s0, 1
li
$s2, 8
...
Restore registers# $a0 and $a1add
$v0, $a0, $a
Restore registers# $s0 and $s2jr
$ra
18
add
$v1, $s0, $s
jr
$ra
Where are the registers saved?Where
are the registers saved?
•^
Now we know who is responsible for saving whichregisters but we still need to discuss where thoseregisters, but we still need to discuss where thoseregisters are saved.
•^
It would be nice if each function call had its own privatememory areamemory area.– This would prevent other function calls from overwriting
our saved registers—otherwise using memory is nobetter than using registersbetter than using registers.
– We could use this private memory for other purposes
too, like storing local variables.
20
Function calls and stacksFunction
calls and stacks
-^
Notice function calls and returns occur in at^
k lik
d^
th^
t^
tl^
ll d
A:
stack-like order: the most recently calledfunction is the first one to return.^1
Someone calls A
A:
...jal B A2: ...
jr $ra
Someone calls A
A calls B
B calls C
C returns to B
j^
B:
C returns to B
B returns to A
A returns
jal C B2: ...
jr
$ra
-^
Here, for example, C must return to B before
B can return to A.
C:
...j^
21
jr
$ra