1 / 35

ECE 15B Computer Organization Spring 2010 Dmitri Strukov

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.

toviel
Download Presentation

ECE 15B Computer Organization Spring 2010 Dmitri Strukov

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 15B Computer OrganizationSpring 2010Dmitri Strukov Lecture 7: Procedures I Partially adapted from Computer Organization and Design, 4th 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 Instructionsand multiplication/division ECE 15B Spring 2010

  3. Logical Operations • Instructions for bitwise manipulation • 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 $t2 0000 0000 0000 0000 0000 1101 1100 0000 $t1 0000 0000 0000 0000 0011 1100 0000 0000 $t0 0000 0000 0000 0000 0000 1100 0000 0000 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 $t2 0000 0000 0000 0000 0000 1101 1100 0000 $t1 0000 0000 0000 0000 0011 1100 0000 0000 $t0 0000 0000 0000 0000 0011 1101 1100 0000 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 Register 0: always read as zero $t1 0000 0000 0000 0000 0011 1100 0000 0000 $t0 1111 1111 1111 1111 1100 0011 1111 1111 ECE 15B Spring 2010

  7. op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits 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 2i • Shift right logical (SRL) • Shift right and fill with 0 bits • srl by i bits divides by 2i (unsigned only) • Shift right arithmetic (SRA) • Shift right and fill emptied bits by sign extending • Note that shamt (immediate value) is only 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 What information mustcompiler/programmer keep track of? main() {inta,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 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 */}intsum(intx, inty) { return x+y;} address (shown in decimal)1000 1004 1008 1012 1016 …2000 2004 C MIPS 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 */}intsum(intx, inty) { return x+y;} address (shown in decimal)1000 add $a0,$s0,$zero # x = a1004 add $a1,$s1,$zero # y = b1008 addi $ra,$zero,1016 #$ra=10161012 j sum #jump to sum1016 …2000 sum: add $v0,$a0,$a12004 jr $ra# new instruction C MIPS ECE 15B Spring 2010

  19. Instruction Support for Functions ... sum(a,b);... /* a,b:$s0,$s1 */}intsum(intx, inty) { return x+y;} 2000 sum: add $v0,$a0,$a12004 jr $ra# new instruction C • Question: Why usejr 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. MIPS ECE 15B Spring 2010

  20. Instruction Support for Functions • Syntax for jal(jump and link) is same as for j (jump): jal label • jalshould really be called lajfor“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 jrinstruction provides a register which contains an address to jump to. • Very useful for function calls: • jalstores return address in register ($ra) • jr $rajumps back to that address ECE 15B Spring 2010

  22. Nested Procedures intsumSquare(intx, inty) { return mult(x,x)+ y;} • Something called sumSquare, now sumSquareis calling mult. • So there’s a value in $rathat sumSquarewants to jump back to, but this will be overwritten by the call to mult. • Need to save sumSquarereturn 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. Space for saved procedure information $sp stack pointer Explicitly created space, i.e., malloc() Variables declared once perprogram; e.g., globals Code Static Heap Stack Program C memory Allocation review Address ¥ 0 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? intsumSquare(intx, inty) { return mult(x,x)+ y;} ECE 15B Spring 2010

  26. Using the Stack • intsumSquare(intx, inty) { return mult(x,x)+ y; } • Hand-compilesumSquare: addi $sp,$sp,-8 # space on stacksw $ra, 4($sp) # save ret addrsw $a1, 0($sp) # save yadd$a1,$a0,$zero # mult(x,x)jalmult# call multlw $a1, 0($sp) # restore yadd $v0,$v0,$a1 # mult()+ylw $ra, 4($sp) # get ret addraddi $sp,$sp,8 # restore stackjr$ramult: ... “push” “pop” ECE 15B Spring 2010

  27. Procedure Calls Steps for Making a Procedure Call • Save necessary values onto stack • Assign argument(s), if any • jalcall • Restore values from stack Rules for Procedures • Called with a jalinstruction, returns with a jr $ra • Accepts up to 4 arguments in $a0, $a1, $a2and $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 $zeroReserved for Assembler $1 $atReturn Values $2-$3 $v0-$v1Arguments $4-$7 $a0-$a3Temporary $8-$15 $t0-$t7Saved $16-$23 $s0-$s7More Temporary $24-$25 $t8-$t9Used by Kernel $26-27 $k0-$k1Global Pointer $28 $gpStack Pointer $29 $spFrame Pointer $30 $fpReturn 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 $fpin 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 temporaryregisters that it may be using onto the stack before making a jalcall • 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 Prologue entry_label: addi$sp,$sp, -framesizesw$ra, framesize-4($sp) # save $ra save other registers if needed ... restore other regs if needed lw $ra, framesize-4($sp) # restore $raaddi $sp,$sp,framesizejr $ra ra Body (call other functions…) memory Epilogue 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

More Related