CSF 2009 The MIPS Assembly Language Procedure Calls
CSF 2009 The MIPS Assembly Language: Procedure Calls and Examples Chapter 2
Procedure Calling • Steps required 1. 2. 3. 4. 5. 6. Place parameters in registers Transfer control to procedure Acquire storage for procedure Perform procedure’s operations Place result in register for caller Return to place of call Chapter 2 — Instructions: Language of the Computer — 2
Register Usage • $a 0 – $a 3: arguments (reg’s 4 – 7) • $v 0, $v 1: result values (reg’s 2 and 3) • $t 0 – $t 9: temporaries – Can be overwritten by callee • $s 0 – $s 7: saved – Must be saved/restored by callee • $gp: global pointer for static data (reg 28) • $sp: stack pointer (reg 29) • $fp: frame pointer (reg 30) • $ra: return address (reg 31) Chapter 2 — Instructions: Language of the Computer — 3
Procedure Call Instructions • Procedure call: jump and link jal Procedure. Label – Address of following instruction put in $ra – Jumps to target address • Procedure return: jump register jr $ra – Copies $ra to program counter – Can also be used for computed jumps • e. g. , for case/switch statements Chapter 2 — Instructions: Language of the Computer — 4
Procedure Example I (One time call) • C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } – Arguments g, …, j in $a 0, …, $a 3 – f in $s 0 (hence, need to save $s 0 on stack) – Result in $v 0 Chapter 2 — Instructions: Language of the Computer — 5
Procedure Example I (One time call) • MIPS code: proc_example: addi $sp, -4 sw $s 0, 0($sp) add $t 0, $a 1 add $t 1, $a 2, $a 3 sub $s 0, $t 1 add $v 0, $s 0, $zero lw $s 0, 0($sp) addi $sp, 4 jr $ra Chapter 2 — Instructions: Language of the Computer — 6 Save $s 0 on stack Procedure body Result Restore $s 0 Return
Procedure Example II (Multiple calls or Recursion) • Procedures that call other procedures • For nested call, caller needs to save on the stack: – Its return address – Any arguments and temporaries needed after the call • Restore from the stack after the call Chapter 2 — Instructions: Language of the Computer — 7
Recursive Procedure Example • C code: int fact (int n) { if (n < 1) return f; else return n * fact(n - 1); } – Argument n in $a 0 – Result in $v 0 Chapter 2 — Instructions: Language of the Computer — 8
Recursive Procedure Example • MIPS code: fact: addi sw sw slti beq addi jr L 1: addi jal lw lw addi mul jr $sp, $ra, $a 0, $t 0, $v 0, $sp, $ra $a 0, fact $a 0, $ra, $sp, $v 0, $ra $sp, -8 4($sp) 0($sp) $a 0, 1 $zero, L 1 $zero, 1 $sp, 8 $a 0, -1 0($sp) 4($sp) $sp, 8 $a 0, $v 0 # # adjust stack for 2 items save return address save argument test for n < 1 # # # # # if so, result is 1 pop 2 items from stack and return else decrement n recursive call restore original n and return address pop 2 items from stack multiply to get result and return
Local Data on the Stack • Local data allocated by callee – e. g. , C automatic variables • Procedure frame (activation record) – Used by some compilers to manage stack storage Chapter 2 — Instructions: Language of the Computer — 10
Memory Layout • Text: program code • Static data: global variables – e. g. , static variables in C, constant arrays and strings – $gp initialized to address allowing ±offsets into this segment • Dynamic data: heap – E. g. , malloc in C, new in Java • Stack: automatic storage Chapter 2 — Instructions: Language of the Computer — 11
String Copy Example • C code (naïve): – Null-terminated string void strcpy (char x[], char y[]) { int i; i = 0; while ((x[i]=y[i])!='