ECE331: Hardware Organization and Design Lecture 8: Procedures (cont d), Binary Numbers and Adders Adapted from Computer Organization and Design, Patterson & Hennessy, UCB
Review: Procedure Calling Steps required 1. The calling program places parameters in registers 2. The calling program transfers control to the procedure (callee) 3. The called procedure acquire storage that it needs from memory 4. The called procedure executes its operations 5. The called procedure places results in registers for the calling program to retrieve. 6. The called procedure reverts the appropriate MIPS registers to their original or correct state. 7. The called procedure returns control to the the next word in memory from which it was called. 8. The calling program proceeds with its calculations ECE331: Adders 2
Procedure Call Instructions (concise) 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 ECE331: Adders 3
Jump-and-Link (detail) 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 New_Procedure_Address Since jal may over-write a relevant value in $ra, it must be saved somewhere (in memory?) before invoking the jal instruction Control is returned back to the caller after completing the callee procedure by using the jump register command jr $ra ECE331: Adders 4
Storage Management on a Call/Return A called procedure must create space for all its variables on the stack Arguments are copied into $a0-$a3; the jal is executed After the called procedure (callee) creates stack space, it updates the value of $sp Once the called procedure finishes, it copies the return value into $v0, frees up stack space, and $sp is restored to its original value The responsibility for copies between stack and registers may fall upon either the calling procedure or the called procedure before during after ECE331: Adders 5
What values are saved? $sp 0x7fff fffc Stack Dynamic Data pc 0x0040 0000 0 Static Data Text Reserved ECE331: Adders 6
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 ECE331: Adders 7
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 Return Grow the stack by 4 bytes Save $s0 on stack Procedure body Result Restore $s0 Shrink the stack by 4 bytes 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 ECE331: Adders 8
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 ECE331: Adders 9
Customs for function calls in MIPS Steps for the code that is calling a function: Caller (calling procedure) 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 ECE331: Adders 10
Customs for function calls in MIPS Steps for the code that is called : Callee (called procedure) 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 ECE331: Adders 11
Example string copy Copy the string stored in y into the array called x. 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 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 ECE331: Adders 12
New Topic: Computer Organization 5 classic components of any computer Computer Processor (CPU) Control Memory Devices Input Datapath Output In the upcoming week we will be looking at optimizing the datapath for addition and multiplication ECE331: Adders 13
Review: Unsigned Binary Integers Given an n-bit number x n-1 x 0 x = x + n 1 n 2 1 0 n 12 + xn 22 +! + x12 x02 Range: 0 to +2 n 1 Example 0000 0000 0000 0000 0000 0000 0000 1011 2 = 0 + + 1 2 3 + 0 2 2 +1 2 1 +1 2 0 = 0 + + 8 + 0 + 2 + 1 = 11 10 Using 32 bits 0 to +4,294,967,295 ECE331: Adders 14
Review: 2 s-complement Signed Integers Given an n-bit number x = x + n 1 n 2 1 0 n 12 + xn 22 +! + x12 x02 Bit n-1 is sign bit 1/0 for negative/non-negative numbers Range: 2 n 1 to +2 n 1 1 x n-1 Example 1111 1111 1111 1111 1111 1111 1111 1100 2 = 1 2 31 + 1 2 30 + + 1 2 2 +0 2 1 +0 2 0 = 2,147,483,648 + 2,147,483,644 = 4 10 Using 32 bits 2,147,483,648 to +2,147,483,647 Most-negative: 1000 0000 0000 Most-positive: 0111 1111 1111 x 0 2 s comp. binary decimal 1000-8 1001-7 1010-6 1011-5 1100-4 1101-3 1110-2 1111-1 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 ECE331: Adders 15
Review: Signed Negation To get X complement X and add 1 Complement means 1 0, 0 1 Example: negate +2 +2 = 0000 0000 0010 2 2 = 1111 1111 1101 2 + 1 = 1111 1111 1110 2 Subtraction: y x = y + ( x +1) Why this works x + x = 1111...1112 x + 1= x = 1 Sign Extension Representing a number using more bits Preserve the numeric value Replicate the sign bit to the left Examples: 8-bit to 16-bit +5: 0000 0101 => 0000 0000 0000 0101 5: 1111 1011 => 1111 1111 1111 1011 ECE331: Adders 16