Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 7: Procedures I Partially adapted from Computer Organization and Design, 4 th edition,

Similar presentations


Presentation on theme: "ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 7: Procedures I Partially adapted from Computer Organization and Design, 4 th edition,"— Presentation transcript:

1 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

2 Review of the last lecture: Logic and Shift Instructions and multiplication/division ECE 15B Spring 2010

3 Logical Operations Instructions for bitwise manipulation OperationCJavaMIPS 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 ECE 15B Spring 2010

4 AND Operations Useful to mask bits in a word – Select some bits, clear others to 0 and $t0, $t1, $t2 0000 0000 0000 0000 0000 1101 1100 0000 0000 0000 0000 0000 0011 1100 0000 0000 $t2 $t1 0000 0000 0000 0000 0000 1100 0000 0000 $t0 ECE 15B Spring 2010

5 OR Operations Useful to include bits in a word – Set some bits to 1, leave others unchanged or $t0, $t1, $t2 0000 0000 0000 0000 0000 1101 1100 0000 0000 0000 0000 0000 0011 1100 0000 0000 $t2 $t1 0000 0000 0000 0000 0011 1101 1100 0000 $t0 ECE 15B Spring 2010

6 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 0000 0000 0000 0000 0011 1100 0000 0000 $t1 1111 1111 1111 1111 1100 0011 1111 1111 $t0 Register 0: always read as zero ECE 15B Spring 2010

7 Shift Operations 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 right logical (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 Note that shamt (immediate value) is only 5 bits oprsrtrdshamtfunct 6 bits 5 bits ECE 15B Spring 2010

8 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 ECE 15B Spring 2010

9 Uses for Shift Instructions Since shifting is faster than multiplication, 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 instruction For example: a = a*8; (in C) would compile to: sll $s0, $s0, 3 (in MIPS) ECE 15B Spring 2010

10 Use for Shift Instructions Let’s consider fast version for 4 bit unsigned number multiplication. Let’s have multiplicand in $a0 and multiplier 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 ECE 15B Spring 2010

11 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 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 ECE 15B Spring 2010

12 Multiplication by Constants Multiplication by the constant 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 Compiler will typically do the optimization for you ECE 15B Spring 2010

13 Division: Sign of the remainder divident = quotient*divisor + reminder for division of signed numbers the sign of the reminder is always the same as the sign of the divident correct: -7 = -3 * 2 – 1 incorrect: -7 = -4 * 2 + 1 ECE 15B Spring 2010

14 Procedures ECE 15B Spring 2010

15 C functions main() { int a,b,c;... c = sum(a,b);... } /* really dumb mult function */... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } What information must compiler/programmer keep track of? What instructions can accomplish this? ECE 15B Spring 2010

16 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. ECE 15B Spring 2010

17 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 1004 1008 1012 1016 … 2000 2004 C MIPSMIPS In MIPS, all instructions are 4 bytes, and stored in memory just like data. So here we show the addresses of where the programs are stored. ECE 15B Spring 2010

18 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 # y = b 1008 addi $ra,$zero,1016 #$ra=1016 1012 j sum #jump to sum 1016 … 2000 sum: add $v0,$a0,$a1 2004 jr $ra # new instruction C MIPSMIPS ECE 15B Spring 2010

19 Instruction Support for Functions... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } 2000 sum: add $v0,$a0,$a1 2004 jr $ra # new instruction 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. C MIPSMIPS ECE 15B Spring 2010

20 Instruction Support for Functions Syntax for jal (jump and link) is same as for j (jump): jallabel 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 ECE 15B Spring 2010

21 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 ECE 15B Spring 2010

22 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. ECE 15B Spring 2010

23 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., 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 ECE 15B Spring 2010

24 C memory Allocation review 0  Address Code Program Static Variables declared once per program; e.g., globals Heap Explicitly created space, i.e., malloc() Stack Space for saved procedure information $sp stack pointer ECE 15B Spring 2010

25 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 amount 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; } ECE 15B Spring 2010

26 Using the Stack Hand-compile sumSquare: 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:... int sumSquare(int x, int y) { return mult(x,x)+ y; } “push” “pop” ECE 15B Spring 2010

27 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? ECE 15B Spring 2010

28 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$t8-$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! ECE 15B Spring 2010

29 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. ECE 15B Spring 2010

30 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 ECE 15B Spring 2010

31 Saved Register Convention $0 – No change. Always 0 $s0-$s7 – Restore if you change. That is why the are called saved registers. If the callee changes these in any way, it must restore the original values before returning $sp – Restore is you change – The stack pointer must point to the same place before and after the jal call (otherwise the caller won’t be able to restore values from the stack) Hint – all saved registers starts with S ECE 15B Spring 2010

32 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 – 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 – 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 ECE 15B Spring 2010

33 Register Conventions What do these conventions 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 use before garbling up their values Remember: Caller/Callee need to save only temporary/saved registers they are using, not all registers ECE 15B Spring 2010

34 Basic Structure of a Function entry_label: addi $sp,$sp, -framesize sw $ra, framesize-4($sp) # save $ra save other registers if needed... restore other regs if needed lw $ra, framesize-4($sp) # restore $ra addi $sp,$sp, framesize jr $ra Epilogue Prologue Body (call other functions…) ra memory ECE 15B Spring 2010

35 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 ECE 15B Spring 2010


Download ppt "ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 7: Procedures I Partially adapted from Computer Organization and Design, 4 th edition,"

Similar presentations


Ads by Google