ECE232: Hardware Organization and Design Lecture 6: Procedures Adapted from Computer Organization and Design, Patterson & Hennessy, UCB
Overview Procedures have different names in different languages Java: methods C: procedures FORTRAN: subroutines Control flow passes to a new section of code, then returns to after the call MIPs includes registers to support data structures for procedures Goal: Understand what happens when a procedure is called/returns ECE232: Procedures 2
Software Conventions for MIPS Registers Register $0 $1 $2 - $3 $4 - $7 $8 - $15 $16 - $23 $24 - $25 $26 - $27 $28 $29 $30 $31 $hi $lo Names $zero $at $v0 - $v1 $a0 - $a3 $t0 - $t7 $s0 - $s7 $t8 - $t9 $k0 - $k1 $gp $sp $fp $ra $hi $lo Hardwired to zero Reserved by assembler Function return result registers Function passing argument value registers Temporary registers, caller saved Saved registers, callee saved Temporary registers, caller saved Reserved for OS kernel Global pointer Stack pointer Frame pointer Usage by Software Convention Return address (pushed by call instruction) High result register (remainder/div, high word/mult) Low result register (quotient/div, low word/mult) ECE232: Procedures 3
Leaf Procedure Example C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } MIPs assumes that arguments g,, j are in $a0,, $a3 f in $s0 (hence, need to save $s0 on stack) Result in $v0 ECE232: Procedures 4
Procedure Calling Steps required 1. Place parameters in registers 2. Transfer control to procedure 3. Acquire storage for procedure 4. Perform procedure s operations 5. Place result in register for caller 6. Return to place of call ECE232: Procedures 5
Leaf Procedure Example MIPS code: leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Save $s0 on stack Procedure body Result Restore $s0 Return ECE232: Procedures 6
Non-Leaf Procedures 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 ECE232: Procedures 7
Procedure Call Instructions Procedure call: jump and link jal ProcedureLabel 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 ECE232: Procedures 8
Jump-and-Link A special register (storage not part of the register file) maintains the address of the instruction currently being executed this is the program counter (PC) The procedure call is executed by invoking the jump-and-link (jal) instruction the current PC (actually, PC+4) is saved in the register $ra and we jump to the procedure s address (the PC is accordingly set to this address) jal NewProcedureAddress Since jal may over-write a relevant value in $ra, it must be saved somewhere (in memory?) before invoking the jal instruction How do we return control back to the caller after completing the callee procedure? ECE232: Procedures 9
The Stack The register scratchpad for a procedure seems volatile It seems to disappear every time we switch procedures A procedure s values are therefore backed up in memory on a stack Proc A s values High address Proc A Proc B s values Proc C s values Stack grows this way Low address return call Proc B call Proc C return return ECE232: Procedures 10
What values are saved? Preserved Saved registers: $s0-$s7 Stack Pointer: $sp Return Address Register; $ra Stack above the stack pointer Not Preserved Temporary registers: $t0-$t9 Argument registers: $a0-$a3 Return registers: $v0-$v1 Stack below the pointer $sp 7fff fffc Stack Dynamic Data pc 0040 0000 0 Static Data Text Reserved ECE232: Procedures 11
Storage Management on a Call/Return A new procedure must create space for all its variables on the stack Arguments are copied into $a0-$a3; the jal is executed After the callee creates stack space, it updates the value of $sp Once the callee finishes, it copies the return value into $v0, frees up stack space, and $sp is incremented The responsibility for copies between stack and registers may fall upon either the caller or the callee ECE232: Procedures 12
Example string copy Convert to assembly: void strcpy (char x[], char y[]) { int i; i=0; while ((x[i] = y[i])!= `\0 ) i += 1; } Base addresses for x and y in $a0 and $a1, respectively $s0 is used for i ECE232: Procedures 13 strcpy: addi $sp, $sp, -8 sw $s0, 0($sp) sw $ra, 4($sp) add $s0, $zero, $zero L1: add $t1, $s0, $a1 lb $t2, 0($t1) add $t3, $s0, $a0 sb $t2, 0($t3) beq $t2, $zero, L2 addi $s0, $s0, 1 j L1 L2: lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
Customs for function calls in MIPS Steps for the code that is calling a function: Caller Before Entry: Step 1: Pass the arguments: The first four arguments (arg0-arg3) are passed in registers $a0-$a3 Remaining arguments are pushed onto the stack Step 2: Execute a jal instruction Jump and Link will save your return address in the $ra register, so that you can return to where you started After Return: Step 1: Get result from $v0, $v1 ECE232: Procedures 14
Customs for function calls in MIPS Steps for the code that is called : Callee On Entry: Step 1: Save callee-saved registers on stack Save registers $s0-$s7 if they are used in the callee procedure Save $ra register Before Exit: Step 1: Save return values in $v0-$v1 registers Step 2: Restore $s0-$s7 registers, if they were saved Step 3: Restore $ra register Step 4: Execute jr $ra ECE232: Procedures 15
Summary Important to understand what happens in MIPs when a procedure is called Information stored on stack, control jumps made MIPs supports recursion All microprocessors support similar actions ECE232: Procedures 16