Professional Documents
Culture Documents
Anthony J Souza
San Francisco State University
April 2017
Readings and Topics
• Topics
• The Stack
• Function call and return
• Parameter handling
• Register User Conventions
• Readings
• Patterson and Hennessy
• 2.8 and 2.13
The Stack
• Is a last-in-first-out data structure. This is also known as LIFO
• This means when pushing items onto the stack, the last item pushed
is at the top. The first item pushed is the bottom.
• Push add an item to the top of the stack.
• Pop remove an item from the top of the stack.
• For example, add 13, 23, and 33 to the stack
top 33
top 23 23
top 13 13 13
0x7FFFFFFC Stack
Pushing 32-bit word onto the stack
Before:
$sp 0x7fffc6c0 0x7fff c6b8
addi $sp, $sp, -4
sw $23, ($sp) 0x7fff c6bc Empty slot
$23 0x12345678 0x7fff c6c0 Old top item
After:
$sp 0x7fffc6bc 0x7fff c6b8 Empty slot
$11 0x13
$12 0x23
$13 0x33 0x7fff c6bc
0x7fff c6c0 Old item
sw $11, -4($sp)
sw $12, -8($sp)
sw $13, -12($sp)
addi $sp, $sp, -12
To push $11, $12, $13 onto the stack, in order
After:
$sp 0x7fff c6c0 Empty slot
0x7fff c6b0
Empty slot
$11 0x13 0x7fff c6b4
Empty slot
$12 0x23 0x7fff c6b8
arg1
Basic Mechanics of Functions
• No need to see the code for pow()
• Caller code (main) and callee function (pow) can be written
independently
• Caller and callee communicate through arguments and return values
defined in prototype
• Arguments:
• Placeholders for copies of data
• Caller puts copies of its variables in argument placeholders
• Callee function puts return value in another placeholder
• Registers are used for arguments and return values.
Register Use Convention
• Set of rules on how to use registers so software modules can
communicate with each other properly
• Register use conventions are part of the call conventions of a system.
• In MIPS:
• First four arguments in $a0-$a3 ($4-$7)
• Return values in $v0, $v1 ($2, $3)
• Caller (main) puts correct values in $a0-$a3 before calling function
• Callee (pow) puts return value in $v0, $v1 before returning.
Control flow of main and pow()
int pow(int b, int e){
int res = 1;
for(int i = 0; i < e; i++){
res = res * b; Each function call must
} remember the return
return res; address(where to
} return to when
int main(){
function is done).
int x=2,p=3,y=2,q=4;
int result, result1;
result = pow(x,p);
result1 = pow(y,q);
}
To call function pow in MIPS
• To call a function in MIPS, a assembly programmer must do the
following:
1. Put arguments into $a0-$a3 registers.
2. Call function via jal instruction.
3. When pow returns, copy return value from v0 into result variable.
• When jal is executed, return address is saved to $ra($31) and then
jumps to label.
• Return address address after the jal instruction that we want to
return to when function returns. (PC + 4)
• In the case of multiple function calls, i.e. a function calls another
function, each function is responsible for saving its return address if it
calls other functions.
Function call Instruction
Jump and Link:
jal label
$31 (or $ra) = return address
go to label [or, PC = address of label]
Jump register:
jr R [R is any register]
PC = contents of R
To return from a function:
jr $ra
C++ prototype for pow()
int pow(int arg0, int arg1)
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra
Nested Functions
• When writing poly, we don’t see code from other functions.
• Compilers work in a similar way when they compile functions.
• From C++ code:
temp1 = pow(arg,4);
result = pow(arg,3);
result = temp1 + result + 1;
• What register should temp be in?
Nested Functions
• When writing poly, we don’t see code from other functions.
• Compilers work in a similar way when they compile functions.
• From C++ code:
temp1 = pow(arg,4);
result = pow(arg,3);
result = temp1 + result + 1;
• What register should temp be in?
• Temp needs to be in a safe register. Its value must be preserved
across functions calls. Meaning pow will not modify it.
• The $s? registers needs to used. (called saved registers).
Nested Functions
• Register use conventions: set of rules that determine how registers are used in a system.
• In general, a function is written/translated independently from other functions.
• Suppose we are writing a caller function; it calls a callee function.
• What happens to registers after the callee returns?
caller:
$s0 = 13
$a0 = 63
$v0 = -3
$t0 = 23
jal callee #callee()
$s0 = 13
$a0 = ??
$v0 = ??
$t0 = ??
Nested Functions
• MIPS register use conventions states:
• $a?, $v?, $t? registers not preserved across functions calls
• $ra is not preserved as well.
• $s? registers are preserved across function calls
• RULE:
• Suppose we are writing/translating function F().
• If $s? is used as a local var in F():
• The old value of $s? must first be saved on the stack
• Before the function returns it must restore all $s0? Registers that were
saved.
Nested Functions
• Looking back out our poly function, we will allocate temp1 to an $s? register, $s1
poly:
addi $sp, $sp, -8
sw $s1, ($sp)
sw $ra, 4($sp)
# do some work
lw $ra, 4($sp)
lw $s1, ($sp
addi $sp, $sp, 8
jr $ra
Nested Functions - Scopes
• There are various arguments floating around:
• poly’s argument arg,
• pow’s argument arg0, and arg1
• Note that all arguments share argument registers $a0-$a3
• MIPS prototype and comment header for poly:
# int poly(int arg)
# a0 arg
# v0 result
• Note from C++ code:
temp1 = pow(arg,4);
result = pow(arg,3);
• arg(in $a0 is used twice, once by poly and once by pow
• $a0 needs to be the same after the call to pow
• But register use conventions says $a0 must be assumed to contain garbage after call to pow(),
meaning not preserved across functions call!.
• Therefor, save a copy of arg in $s
Nested Function Calls poly:addi $sp, $sp, -12
• For nested function calls, sw $s1, ($sp)
often need to make a copy of sw $s0, 4($sp)
sw $ra, 8($sp)
each argument.
add $s0, $a0, $0
• In our example this is arg for li $a1, 4
the function poly. jal pow
• Then we can use a0 for pow’s move $s1, $v0
first argument. li $a1, 3
jal pow
• Now we can translate poly: add $t0, $v0, $s1
addi $v0, $t0, 1
lw $ra, 8($sp)
lw $s0, 4($sp)
lw $s1, ($sp)
addi $sp, $sp, 12
jr $ra
Nested Function Calls
• Complete source code at:
• http://unixlab.sfsu.edu/~whsu/csc256/PROGS/4.2.cpp
• http://unixlab.sfsu.edu/~whsu/csc256/PROGS/4.2.s
Summary MIPS Register Use Conventions
• Set of rules agreed on by software developers to make
sure software modules can communicate properly
• $a0-$a3 are arguments
• $v0-$v1 are return values
• $a?, $t?, $v? are not preserved across function calls.
• Must assume $a? $v? change contents after function
calls
• $t? are temporaries; these are also not preserved
across function calls.
Summary MIPS Register Use Conventions
• It is the responsibility of the caller to save $a?, $v?,
and $t?, if needed. [caller-saved]
• $s? are saved across function calls.
• It is the responsibility of the callee to save and restore
$s? registers that it uses [callee-saved]
• A compiler will choose the correct type of register to
use for each local variable.