Professional Documents
Culture Documents
05 - MIPS Procedures PDF
05 - MIPS Procedures PDF
CS270
Max Luttrell, Fall 2016
procedures
• high level language: procedure, function,
method, subroutine
procedure needs
1. put parameters in a place where procedure can
access them
• Instructions
instru name purpose example
ction jump jump to location and store jal ProcedureAddress
jal
and link return address in $ra
jump jump to address in
jr jr $ra
register register
simple procedure
example
main
- put parameter(s) in $a0-$a3
- jal ProcedureAddress # puts PC+4 in $ra and
# jumps to ProcedureAddress
ProcedureAddress:
- use parameters in $a0-$a3 to compute result
- put result in $v0, $v1
- jr $ra # jumps back to $ra, origin point in main
stack
• to save variables, we
use a stack
$sp high memory
• the $sp register points address
to the top of stack
• push -- subtract 4
from $sp, then add a
word to the stack
value addr
$sp
$sp 0x10 0x10
0xC
0x8
0x4
stack example
• push (7) - done
$sp 7 0xC
0x8
0x4
stack example
• push (7) - done
7 0xC
$sp 3 0x8
0x4
stack example
• push (7) - done
$sp 7 0xC
0x8
0x4
MIPS procedure
• previously, we wrote MIPS code for the following
C statement, assuming the f,g,h,i,j variables are
assigned to registers $s0, $s1, $s2, $s3, and
$s4:
f = (g+h) - (i+j);
add $t0, $s1, $s2 # put g+h in $t0
add $t1, $s3, $s4 # put i+j in $t1
sub $s0, $t0, $t1 # put $t0-$t1 in f
# note $t0 is now g+h; $t1 is i+j
leaf_example:
addi $sp, $sp, -12 # adjust stack to make room for 3 words
sw $t1, 8($sp) # save $t1 to stack
sw $t0, 4($sp) # save $t0 to stack
sw $s0, 0($sp) # save $s0 to stack
...
• first, let's save all the registers we will use to the stack
MIPS procedure
int leaf_example(int g, int h, int i, int j)
{
int f;
f = (g+h) - (i+j);
return f;
}
...
add $t0, $a0, $a1 # $t0 gets g+h
add $t1, $a2, $a3 # $t1 gets i+j
sub $s0, $t0, $t1 # $s0 gets (g+h) - (i+j)
...
...
add $v0, $s0, $zero # put result in $v0 for return
...
...
lw $s0, 0($sp) # restore $s0 for caller
lw $t0, 4($sp) # restore $t0 for caller
lw $t1, 8($sp) # restore $t1 for caller
addi $sp, $sp, 12 # adjust stack to pop 3 items
...
...
jr $ra
• MIPS convention:
int proc2(int);
int proc1(void) {
int a=10, b=40;
a = a + proc2(b);
return (a);
}
order of items on stack
int proc2(int);
int proc1(void) {
• Inside of a call to proc1, looking int a=10, b=40;
"up" the stack, here is what you a = a + proc2(b);
return (a);
should see, in order: }
int proc2(int);
int proc1(int a) {
int b=40;
a = a + proc2(b);
return (a);
}
• returns to caller
$ra 20($sp)
• main() calls f1(), which m
a
calls f2() $s0 16($sp) i
n
• main() $sp arg area 0($sp)
• uses $s0
$ra 56($sp)
m
• is a leaf (does not call a
$s0 52($sp) i
any other procedures) n
arg area 36($sp)
• only uses t-registers $ra 32($sp)
$s3 28($sp)
$s1 24($sp) f
1
$t0 20($sp)
e 16($sp)
arg area
$sp 0($sp)
(a,b,c,d)
nmatches
• Let's look at a slightly more complicated
example
int equiv (char, char);
• Code:
/pub/cs/mluttrel/cs270/examples/mips/nmatches1.s
nmatches variables
stored in s-registers
• Alternate pproach: save variables in s-registers
• Code:
/pub/cs/mluttrel/cs270/examples/mips/nmatches1_sregs.s
global variables
• In addition to the stack, which holds arguments, registers, and
locals, we need memory for global/static variables and dynamically
allocated memory (i.e. "malloc" in C, "new" in C++/java)
dynamically
allocated
pre-allocated lower memory
(globals) address
Exercise 5A
• In this exercise, we will call a procedure, but we will not use the
stack to save and restore registers this time. This is not correct
behavior, but is a good starting point to introduce us to
procedures.
• You can get a starting file from hills by using the following scp
command (all one line; replace uname with your username):
scp uname@hills.ccsf.edu:/pub/cs/mluttrel/cs270/exercises/
procedures/first_proc.s .
fog.ccsf.edu/~mluttrel/cs270/exercises/ex5c.html
Exercise 5D
fog.ccsf.edu/~mluttrel/cs270/exercises/ex5d.html
Exercise 5E
fog.ccsf.edu/~mluttrel/cs270/exercises/ex5e.html