Lecture 2 6 Instructions Procedure Call Learning Objectives
Lecture 2. 6 Instructions: Procedure Call
Learning Objectives n n n Describe the 6 steps in procedure call Identify the registers whose contents are preserved by the callees Adjust the stack pointer register properly in procedure call Specify the behaviors of callers and callees following the calling conventions Convert functions in high-level languages to assembly statements Chapter 2 — Instructions: Language of the Computer — 2
Coverage n Textbook Chapter 2. 8 Chapter 2 — Instructions: Language of the Computer — 3
Six Steps in Execution of a Procedure Caller places parameters in a place where the procedure (callee) can access them 1. § $a 0 -$a 3: four argument registers Caller transfers control to the callee 2. n Use jal instruction Callee acquires the storage resources needed Callee performs the desired task Callee places the result value in a place where the caller can access it 3. 4. 5. § $v 0 -$v 1: two registers for result values Callee returns control to the caller 6. § $ra: return address register to return to the point of origin Chapter 2 — Instructions: Language of the Computer — 4
Instructions for Accessing Procedures n MIPS procedure call instruction n Saves PC+8 in register $ra to have a link to the second instruction following the jump instruction for the procedure to return jal n Procedure. Address Instruction format: J format 0 x 03 n #jump and link 26 bit address Procedure can do a jump on register jr n $ra #return Instruction format: R format 0 rs 0 x 08 Chapter 2 — Instructions: Language of the Computer — 5
Delay slot n Delay slot is an instruction slot that gets executed without the effects of a preceding instruction n n When an instruction is executed, the following instruction is put into the delay slot In hardware and some simulators, the instruction following jump and branch instructions is always executed n Add ‘nop’ after the jump and branch instructions in your lab to simply coding n Including j, jal, beq, bne Chapter 2 — Instructions: Language of the Computer — 6
Calling Convention n A set of rules for defining n n Where data are placed during function calls Who saves/restores data What data should be stored All calling conventions have a bit in common n All store data in registers and on a stack Chapter 2 — Instructions: Language of the Computer — 7
32 Registers Revisited Name Register Number $zero 0 $at 1 $v 0 - $v 1 2 -3 $a 0 - $a 3 4 -7 $t 0 - $t 7 8 -15 $s 0 - $s 7 16 -23 $t 8 - $t 9 24 -25 $gp 28 $sp 29 $fp 30 $ra 31 Usage Preserved by callee? constant 0 (hardware) n. a. reserved for assembler n. a. returned values no arguments no temporaries no saved values yes temporaries no global pointer yes stack pointer yes frame pointer yes return address no Chapter 2 — Instructions: Language of the Computer — 8
MIPS Memory Allocation n n Text: program code Static data: global variables n n n e. g. , static variables in C, $sp constant arrays and strings $gp initialized to address allowing ±offsets into this segment $gp e. g. , malloc in C, new in Java Stack: automatic storage 0 x f f f f c Stack Dynamic data: heap n n Memory Dynamic data (heap) Static data 0 x 1000 8000 0 x 1000 0000 Text (Your code) PC Reserved 0 x 0040 0000 0 x 0000 Chapter 2 — Instructions: Language of the Computer — 9
Stack n A stack is a LIFO data structure n n Push: n n n LIFO = Last-In, First-Out Basic stack operations: push, pop Stack pointer: point to the “top” element in the stack Place an element on the top of the stack Update the stack pointer Pop: n n Remove an element from the top of the stack Update the stack pointer Chapter 2 — Instructions: Language of the Computer — 10
Stack Example Chapter 2 — Instructions: Language of the Computer — 11
Stack in Memory n A stack is a last-in first-out (LIFO) data structure n high addr top of stack $sp One of the general registers, $sp, is used to address the stack, which “grows” from high address to low address n Push – add data onto the stack $sp = $sp – 4 data on stack at new $sp n low addr Pop – remove data from the stack data from stack at $sp = $sp + 4 Chapter 2 — Instructions: Language of the Computer — 12
Procedures n Root n n n Non-leaf procedure n n Entry function of a program Always a caller Both a caller and a callee Leaf procedure n Always a callee Chapter 2 — Instructions: Language of the Computer — 13
Calling Conventions regarding Root n n Root is always a caller As a caller it needs to save on the stack: n Any arguments and temporaries needed after calling the callee n n Arguments are given as the program is lauched Restore from the stack after calling the callee If caller calls multiple callees, then the caller may need to save the return values to the stack Root has no return address to save Chapter 2 — Instructions: Language of the Computer — 14
Calling Conventions regarding Leaf Procedures n n A leaf procedure is a callee A callee needs to save on the stack: n Any saved registers it needs to use for computation n n Restore from the stack before returning the control back to the caller A callee needs to restore the following registers before returning the control back to the caller n n n Global pointer register Frame pointer register Stack pointer register Chapter 2 — Instructions: Language of the Computer — 15
Leaf Procedure Example n C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } n Arguments g, …, j in $a 0, …, $a 3 n f in $s 0 n n The procedure needs to save $s 0 on stack before using it Result in $v 0 Chapter 2 — Instructions: Language of the Computer — 16
Leaf Procedure Example n MIPS code: leaf_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 Save $s 0 on stack Procedure body Result Restore $s 0 Return Chapter 2 — Instructions: Language of the Computer — 17
Stack Contents Before the call During the call After the call Chapter 2 — Instructions: Language of the Computer — 18
String Copy Example n C code (naïve): Null-terminated string void strcpy (char x[], char y[]) { int i; i = 0; while ((x[i]=y[i])!='