ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 7: Procedures I Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy, and classes taught by and classes taught by Patterson at Berkeley and Ryan Kastner at UCSB
Review of the last lecture: Logic and Shift Instructions and multiplication/division
Logical Operations Instructions for bitwise manipulation Operation C Java MIPS Shift left << << sll Shift right >> >>> srl Bitwise AND & & and, andi Bitwise OR or, ori Bitwise NOT ~ ~ nor Useful for extracting and inserting groups of bits in a word
AND Operations Useful to mask bits in a word Select some bits, clear others to 0 and $t0, $t1, $t2 $t2 $t1 0000 0000 0000 0000 0000 1101 1100 0000 0000 0000 0000 0000 0011 1100 0000 0000 $t0 0000 0000 0000 0000 0000 1100 0000 0000
OR Operations Useful to include bits in a word Set some bits to 1, leave others unchanged or $t0, $t1, $t2 $t2 $t1 0000 0000 0000 0000 0000 1101 1100 0000 0000 0000 0000 0000 0011 1100 0000 0000 $t0 0000 0000 0000 0000 0011 1101 1100 0000
NOT Operations Useful to invert bits in a word Change 0 to 1, and 1 to 0 MIPS has NOR 3 operand instruction a NOR b == NOT ( a OR b ) nor $t0, $t1, $zero Register 0: always read as zero $t1 $t0 0000 0000 0000 0000 0011 1100 0000 0000 1111 1111 1111 1111 1100 0011 1111 1111
Shift Operations op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits shamt: how many positions to shift Shift left logical (SLL) Shift left and fill with 0 bits sll by i bits multiplies by 2 i Shift hf right logical l( (SRL) Shift right and fill with 0 bits srl by i bits divides by 2 i (unsigned only) Shift right arithmetic (SRA) Shift right and fill emptied bits by sign extending Notethat that shamt (immediatevalue) is only 5 bits
Uses for Shift Instructions Very convenient operation to extract group of bits (e.g. one byte) within a word (e.g. think of operations on 8 bit pixels or 8 bit characters) For example, suppose we want to get bits 8 to 15 from $t0. The code below will do the job sll $t0, $t0, 16 srl $t0,,$ $t0, 24
Uses for Shift Instructions Since ceshifting is faster than multiplication, utp cato,a good compiler (or a good programmer for that matter) usually notices when C code multiplies by a power of 2 and compiles it to a shift hf instruction For example: a = a*8; (in C) would compile to: sll $s0, $s0, 3 (in MIPS)
Use for Shift Instructions Let s consider fast version for 4 bit unsigned number multiplication. Let s have multiplicand l in $a0 and multiplier l in $a1 and let s assume that we don t have to preserve the values of $a0 and $a1. addi $t1, $0, 4 # initialize loop counter srl $a0, $a0, 4 # align multiplicand with high 4 bit portion of the product loop: andi $t0, $a1, 1 # get the current LSB of the product (i.e. that of the multiplier) beq $t0, $0, skipadd # skip addition of the multiplicand to the product if current last bit of the product is 0 add $a1, $a1, $a0 # add multiplicand to the product skipadd:addi $t1, $t1, 1 # decrement loop counter srl $a1, $a1, 1 # shift to the right product by one bit bne $t1, $0, loop; # repeat loop 4 times The code will have 8 bit product in register $a1
Use for Shift Instructions The previous code have branch instruction in the loop. It is desirable to avoid branching whenever possible so that using shift and logic (masking) )instruction i we can have instead: addi $t1, $0, 4 # initialize loop counter srl $a0, $a0, 4 # align multiplicand with high 4 bit portion of the product loop: sll $t0, $a1, 31 # move the current LSB of the product to the MSB srla $t0, $t0, 24 # perform sign extension which will create mask with LSB value of the product and $t0, $t0, $a0 # if LSB =1 then $t0 = $a0, if LSB = 0 then $t0 = 0 add $a1, $a1, $t0 # add $t0 to the product skipadd:addi $t1, $t1, 1 # decrement loop counter srl $a1, $a1, 1 # shift to the right product by one bit bne $t1, $0, loop; # repeat loop 4 times
Multiplication by Constants Multiplication utp cato by the econstant t could be implemented more efficiently For example: b = a*7 is the same as b = a*8 a which can be implemented as one shift and one sub operation which could be faster than performing mult operation or if mult is not available faster than sequential multiplication algorithm Compilerwill typicallydo the optimization for you
Division: Sign of the remainder divident = quotient*divisor + reminder for division i i of signed numbers the sign of the reminder is always the same as the sign of the dividentid correct: 7 = 3 * 2 1 incorrect: 77 = 44 * 2 + 1
Procedures
C functions main() { int a,b,c;... c = sum(a,b);... } What information must compiler/programmer keep track of? /* really dumb mult function */... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } ret rn + What instructions can accomplish this?
Function Call Bookkeeping Registers play a major role in keeping track of information for function calls. Register conventions: Return address $ra Arguments $a0, $a1, $a2, $a3 Return value $v0, $v1 Local variables $s0, $s1,, $s7 The stack is also used; more later.
Instruction Support for Functions C... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } address (shown in decimal) 1000 1004 1008 1012 1016 2000 2004 M In MIPS, all instructions are 4 I bytes, and stored in memory P just like data. So here we S show the addresses of where the programs are stored.
Instruction Support for Functions... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } address (shown in decimal) 1000 add $a0,$s0,$zero # x = a 1004 add $a1,$s1,$zero $ 1 $ # y = b I 1008 addi $ra,$zero,1016 #$ra=1016 1012 j sum #jump to sum P 1016 C M S 2000 sum: add $v0,$a0,$a1$a0 $a1 2004 jr $ra # new instruction
Instruction Support for Functions C M I P S... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } Question: Why use jr here? Why not use j? Answer: sum might be called by many places, so we can t return to a fixed place. The calling proc to sum must be able to say return here somehow. 2000 sum: add $v0,$a0,$a1 2004 jr $ra # new instruction
Instruction Support for Functions Syntax for jal (jump and link) is same as for j (jump): jal label jal should really be called laj for link and jump : Step 1 (link): Save address of next instruction into $ra Why next instruction? Why not current one? Step 2 (jump): Jump to the given label
Instruction Support for Functions Syntax for jr (jump register): jr register Instead of providing a label to jump to, the jr instruction provides a register which contains an address to jump to. Very useful for function calls: jal stores return address in register ($ra) jr $ra jumps back to that address
Nested Procedures int sumsquare(int x, int y) { return mult(x,x)+ y; } Something called sumsquare,, now sumsquare is calling mult. So there s a value in $ra that sumsquare wants to jump back to, but this will be overwritten by the call to mult. Need to save sumsquare return address before call to mult.
Nested Procedures In general, may need to save some other info in addition to $ra. When a C program is run, there are 3 important memory areas allocated: Static: Variables declared once per program, cease to exist only after execution completes. E.g., g, C globals Heap: Variables declared dynamically via malloc Stack: Space to be used by procedure during execution; this is where we can save register values
Address C memory Allocation review $sp stack pointer 0 Stack Heap Staticti Code Space for saved procedure information Explicitly created space, i.e., malloc() Variables declared once per program; e.g., globals Program
Using the Stack So we have a register $sp which always points to the last used space in the stack. To use stack, we decrement this pointer by the eamount of space we need and then fill it with info. So, how do we compile this? int sumsquare(int x, int y) { return mult(x,x)+ y; }
Using the Stack Hand compile int sumsquare(int x, int y) { sumsquare: return mult(x,x)+ y; } addi $sp,$sp,-8 # space on stack sw $ra, 4($sp) # save ret addr sw $a1, 0($sp) # save y add $a1,$a0,$zero # mult(x,x) jal mult # call mult lw $a1, 0($sp) # restore y add $v0,$v0,$a1 # mult()+y lw $ra, 4($sp) # get ret addr addi $sp,$sp,8 $ # restore stack jr $ra mult:... push pop
Procedure Calls Steps for Making a Procedure Call 1. Save necessary values onto stack 2. Assign argument(s), if any 3. jal call 4. Restore values from stack Rules for Procedures Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3 Return value is always in $v0 (and if necessary in $v1) Must follow register conventions So what are they?
MIPS Registers The constant 0 $0 $zero Reserved for Assembler $1 $at Return Values $2 $3 $ $v0 $v1 $ Arguments $4 $7 $a0 $a3 Temporary $8 $15 $t0 $t7 Saved $16 $23 $s0 $s7 More Temporary $24 $25 $25 $t8 $t9$t9 Used by Kernel $26 27 $k0 $k1 Global Pointer $28 $gp Stack Pointer $29 $sp Frame Pointer $30 $fp Return Address $31 $ra Use names for registers code is clearer!
Other Registers $at: may be used by the assembler at any time; unsafe to use $k0-$k1: may be used by the OS at any time; unsafe to use $gp, $fp: don t worry about them Note: Feel free to read up on $gp and $fp in Appendix B, but you can write perfectly good MIPS code without them.
Register Convention Caller The calling function Callee The function being called When the callee returns from executing, the caller needs to know which registers may have changed and which are guaranteed to be unchanged Register conventions A set of generally accepted rules as to which registeres will be unchanged after a procedure call (jal) and which may be changed
Saved Register Convention $0 No change. Always 0 $s0 $s7 $sp Restore if you change. That is whythe are called saved registers. If the callee changes these in any way, it must restore the original values before returning Restore is you change The stack pointer must point to the same place before and after the jl jal call (otherwise the caller won t be able to restore values from the stack) Hint all saved registers starts with S
Volatile Register Convention $ra Can change. The jal call itself will change this register. Note that caller needs to save on stack if nested call $v0 $v1$v1 Can change. These will contain the new returned values $a0 $a3 Can change. These are volatile argument registers. Caller needs to save them if it will need them after the call $t0 $t9 $t9 Can change. Called temporary. Any procedure may change them at any time. Caller needs to save them if it will need them after the call
Register Conventions What do these eseconventions o mean? If function R calls function E, then Function R must save any temporary registers that it may be using onto the stack before making a jal call Function E must save any saved registers it intends to usebeforegarbling uptheir values Remember: Caller/Callee need to save only temporary/saved registers they are using, not all registers s
Prologue entry_label: Basic Structure of a Function addi $sp,$sp, -framesize sw $ra, framesize-4($sp) # save $ra save other registers if needed Body... (call other functions ) ra Epilogue restore other regs if needed memory lw $ra, framesize-4($sp) # restore $ra addi $sp,$sp, framesize jr $ra
Review Instructions so far: arithmetics: add, addi, sub, addu, addiu, subu arithmetics: mult, div, mfhi, mflo, multu, divu Memory operations: lw, sw, lb, lbu, lh, lhu, sb, shw Control: beq, bne, j, slt, slti, sltu, sltui, jal, jr Logic/shift: and, andi, or, ori, xor, xori, sll, srl, sra, srlv, sllv, nor Registers: All of them: $0 $31