lecture 8. mips instructions #4 – branch instructions #2
DESCRIPTION
2010 R&E Computer System Education & Research. Lecture 8. MIPS Instructions #4 – Branch Instructions #2. Prof. Taeweon Suh Computer Science Education Korea University. Procedure (Function). Programmers use a procedure (or function) to structure programs - PowerPoint PPT PresentationTRANSCRIPT
Lecture 8. MIPS Instructions #4 – Branch Instructions #2
Prof. Taeweon SuhComputer Science Education
Korea University
2010 R&E Computer System Education & Research
Korea Univ
Procedure (Function)
• Programmers use a procedure (or function) to structure programs To make the program modular and easy to understand To allow code to be re-used
• Procedures allow the programmer to focus on just one portion of the task at a time
• Parameters (arguments) act as an interface between the procedure and the rest of the program
2
Korea Univ
Procedure Calls
Definitions• Caller: calling procedure (in this case, main)• Callee: called procedure (in this case, sum)
3
High-level code example
void main(){ int y; y = sum(42, 7); ...}
int sum(int a, int b){ return (a + b);}
Korea Univ
MIPS Instructions for Accessing Procedures
• Procedure call instruction:
jal ProcedureAddress #jump and link# $ra <- pc + 4
# pc <- jump target
Saves PC+4 in register $ra to have a link to the next instruction for the procedure return
Instruction format (J format):
4
26-bit address0x03
High-level code
int main() { simple(); a = b + c;}
void simple() { return;}
MIPS assembly code
0x00400200 main: jal simple
0x00400204 add $s0, $s1, $s2
...
0x00401020 simple: jr $ra
void means that simple doesn’t return a value.
compile
PC
PC+4
jal: jumps to simple and saves PC+4 in the return address register ($ra). In this case, $ra = 0x00400204 after jal executes
Korea Univ
MIPS Instructions for Accessing Procedures
• Return instruction:jr $ra #return // pc <- $ra
Instruction format (R format):
5
0 31 0x08
High-level code
int main() { simple(); a = b + c;}
void simple() { return;}
MIPS assembly code
0x00400200 main: jal simple
0x00400204 add $s0, $s1, $s2
...
0x00401020 simple: jr $ra
compile
$ra contains 0x00400204
jr $ra: jumps to address in $ra, in this case 0x00400204.
Korea Univ
Procedure Calls
Procedure calling conventions:• Caller:
passes arguments to callee. jumps to the callee
• Callee: performs the procedure returns the result to caller returns to the point of call Must not overwrite registers or memory needed by the
caller
MIPS conventions:• Call procedure: jump and link (jal) • Return from procedure: jump register (jr)• Argument values: $a0 - $a3• Return values: $v0, $v1
6
Korea Univ
Input Arguments and Return Values
7
High-level code
int main() { int y; ... y = diffofsums(2, 3, 4, 5); // 4 arguments ...}
int diffofsums(int f, int g, int h, int i){ int result; result = (f + g) - (h + i); return result; // return value}
Korea Univ
Input Arguments and Return Values
8
MIPS assembly code
# $s0 = y
main: ... addi $a0, $0, 2 # argument 0 = 2 addi $a1, $0, 3 # argument 1 = 3 addi $a2, $0, 4 # argument 2 = 4 addi $a3, $0, 5 # argument 3 = 5 jal diffofsums # call procedure add $s0, $v0, $0 # y = returned value ...
# $s0 = resultdiffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller
High-level code
int main() { int y; ... y = diffofsums(2, 3, 4, 5); // 4 arguments ...}
int diffofsums(int f, int g, int h, int i){ int result; result = (f + g) - (h + i); return result; // return value}
Korea Univ
Input Arguments and Return Values
9
MIPS assembly code
# $s0 = resultdiffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller
• We need a place (called stack) to temporarily store arguments and registers because … diffofsums overwrote 3 registers: $t0, $t1, and $s0 , which may
be being used in the main routine What if the callee (diffofsums) needs to use more registers than
allocated to argument ($a0 - $a3) and return values ($v0, $v1)
Korea Univ
The Stack
• Memory used to temporarily save variables
• Like a stack of dishes, stack is a data structure for spilling registers organized as a last-in-first-out (LIFO) queue
10
Korea Univ
The Stack - Spilling Registers
• Stack is a data structure for spilling registers organized as a last-in-first-out (LIFO) queue
• One of the general registers, $sp ($29), is used to point to the top of the stack The stack “grows” from high
address to low address in MIPS Push: add data onto the stack
• $sp = $sp – 4 • data on stack at new $sp
Pop: remove data from the stack• Data from stack at $sp• $sp = $sp + 4
11
low addr
high addr
$sptop of stack
Main Memory
Korea Univ
Stack Pointer (SP)
• Stack pointer ($sp) points to the top of the stack
12
Data
7FFFFFFC 12345678
7FFFFFF8
7FFFFFF4
7FFFFFF0
Address
$sp 7FFFFFFC
7FFFFFF8
7FFFFFF4
7FFFFFF0
Address Data
12345678
$sp
AABBCCDD
11223344
Main Memory
Main Memory
Korea Univ
How Procedures use the Stack
• Called procedures (callee) must not have any unintended side effects to the caller But, diffofsums overwrites 3 registers ($t0,
$t1, $s0) as duplicated below:
13
# MIPS assembly
# $s0 = resultdiffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller
Korea Univ
Storing Register Values on the Stack
14
# $s0 = resultdiffofsums: addi $sp, $sp, -12 # make space on stack # to store 3 registers sw $s0, 8($sp) # save $s0 on stack sw $t0, 4($sp) # save $t0 on stack sw $t1, 0($sp) # save $t1 on stack add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h +
i) add $v0, $s0, $0 # put return value in $v0 lw $t1, 0($sp) # restore $t1 from stack lw $t0, 4($sp) # restore $t0 from stack lw $s0, 8($sp) # restore $s0 from stack addi $sp, $sp, 12 # deallocate stack space jr $ra # return to caller
Data
FC
F8
F4
F0
Address
$sp
(a)
Data
FC
F8
F4
F0
Address
$sp
(b)
$s0
Data
$sp
(c)
$t0
FC
F8
F4
F0
Address
? ??
sta
ck fr
am
e$t1
“Push” (back up) the registers to be used in the callee to the stack
“Pop” (restore) the registers from the stack prior to returning to the caller
Korea Univ
Preserved and NonPreserved Registers
15
Preserved(Callee-Saved)
Nonpreserved(Caller-Saved)
$s0 - $s7 $t0 - $t9
$ra $a0 - $a3
$sp $v0 - $v1
stack above $sp
stack below $sp
• In the previous example, if the calling procedure does not use the temporary registers ($t0, $t1), the effort to save and restore them is wasted
• To avoid this waste, MIPS divides registers into preserved and nonpreserved categories• The preserved registers include $s0 ~ $s7 (saved)• The nonpreserved registers include $t0 ~ $t9 (temporary)• So, a procedure must save and restore any of the preserved registers it
wishes to use, but it can change the nonpreserved registers freely• The callee must save and restore any preserved registers it wishes
to use• The callee may change any of the nonpreserved registers
• But, if the caller is holding active data in a nonpreserved register, the caller needs to save and restore it
Korea Univ
Storing Saved Registers on the Stack
16
# $s0 = resultdiffofsums: addi $sp, $sp, -4 # make space on stack to
# store one register sw $s0, 0($sp) # save $s0 on stack # no need to save $t0 or $t1 add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 lw $s0, 0($sp) # restore $s0 from stack addi $sp, $sp, 4 # deallocate stack space jr $ra # return to caller
Korea Univ
Nested Procedure Calls
17
proc1: addi $sp, $sp, -4 # make space on stack sw $ra, 0($sp) # save $ra on stack jal proc2 ... lw $ra, 0($sp) # restore $s0 from stack addi $sp, $sp, 4 # deallocate stack space jr $ra # return to caller
• Procedures that do not call others are called leaf procedures• Life would be simple if all procedures were leaf procedures, but
they aren’t• For example,
• the main program calls procedure A with an argument of 3 (by placing the value 3 into register $a0 and then using jal A)
• Procedure A calls procedure B via jal B with an argument 7 (also placed in $a0)
• There is a conflict over the use of register $a0 and $ra• Use stack to preserve registers
• The caller pushes any argument registers ($a0~$a4) or temporary registers ($t0~$t9) that are needed after the call
Korea Univ
Recursive Procedure Call
18
MIPS assembly code
0x90 factorial: addi $sp, $sp, -8 # make room0x94 sw $a0, 4($sp) # store $a00x98 sw $ra, 0($sp) # store $ra0x9C addi $t0, $0, 2 0xA0 slt $t0, $a0, $t0 # a <= 1 ?0xA4 beq $t0, $0, else # no: go to else 0xA8 addi $v0, $0, 1 # yes: return 10xAC addi $sp, $sp, 8 # restore $sp0xB0 jr $ra # return0xB4 else: addi $a0, $a0, -1 # n = n - 10xB8 jal factorial # recursive call0xBC lw $ra, 0($sp) # restore $ra0xC0 lw $a0, 4($sp) # restore $a00xC4 addi $sp, $sp, 8 # restore $sp0xC8 mul $v0, $a0, $v0 # n * factorial(n-1)0xCC jr $ra # return
High-level code
int factorial(int n) { if (n <= 1) return 1; else return (n * factorial(n-1));}
• Recursive procedures invoke “clones” of themselves
Korea Univ
Stack during Recursive Call
19
$sp FC
F8
F4
F0
$ra
EC
E8
E4
E0
DC
FC
F8
F4
F0
EC
E8
E4
E0
DC
FC
F8
F4
F0
EC
E8
E4
E0
DC
$sp
$sp
$sp
$sp
$a0 = 1$v0 = 1 x 1
$a0 = 2$v0 = 2 x 1
$a0 = 3$v0 = 3 x 2
$v0 = 6
$sp
$sp
$sp
$sp
DataAddress DataAddress DataAddress
$a0 (0x3)
$ra (0xBC)
$a0 (0x2)
$ra (0xBC)
$a0 (0x1)
$ra
$a0 (0x3)
$ra (0xBC)
$a0 (0x2)
$ra (0xBC)
$a0 (0x1)