CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Fall 2004 © UCBLecturer PSOE Dan Garciawww.cs.berkeley.edu/~ddgarciainst.eecs.berkeley.edu/~cs61cCS61C : Machine Structures Lecture 11 – Introduction to MIPS Procedures I 2004-09-24World’s highest res! ⇒ 2.2 inch VGA res LCDdisplay @ 368 pixels per inch (ppi),and might be on cell phones soon…www.i4u.com/article2157.htmlCS 61C L11 Introduction to MIPS: Procedures I (2) Garcia, Fall 2004 © UCBReview• In order to help the conditional branchesmake decisions concerning inequalities,we introduce a single instruction: “Seton Less Than”called slt, slti, sltu,sltiu• One can store and load (signed andunsigned) bytes as well as words• Unsigned add/sub don’t cause overflow• New MIPS Instructions: sll, srlslt, slti, sltu, sltiuaddu, addiu, subuCS 61C L11 Introduction to MIPS: Procedures I (3) Garcia, Fall 2004 © UCBExample: The C Switch Statement (3/3)• Final compiled MIPS code: bne $s5,$0,L1 # branch k!=0 add $s0,$s3,$s4 #k==0 so f=i+j j Exit # end of case so ExitL1: addi $t0,$s5,-1 # $t0=k-1 bne $t0,$0,L2 # branch k!=1 add $s0,$s1,$s2 #k==1 so f=g+h j Exit # end of case so ExitL2: addi $t0,$s5,-2 # $t0=k-2 bne $t0,$0,L3 # branch k!=2 sub $s0,$s1,$s2 #k==2 so f=g-h j Exit # end of case so ExitL3: addi $t0,$s5,-3 # $t0=k-3 bne $t0,$0,Exit # branch k!=3 sub $s0,$s3,$s4 #k==3 so f=i-jExit:Removing breaks does NOT translate toremoving jumps… (my bad)CS 61C L11 Introduction to MIPS: Procedures I (4) Garcia, Fall 2004 © UCBC functionsmain() {int i,j,k,m;...i = mult(j,k); ...m = mult(i,i); ...}/* really dumb mult function */int mult (int mcand, int mlier){int product; product = 0;while (mlier > 0) { product = product + mcand; mlier = mlier -1; }return product;}What information mustcompiler/programmer keep track of?What instructions can accomplish this?CS 61C L11 Introduction to MIPS: Procedures I (5) Garcia, Fall 2004 © UCBFunction Call Bookkeeping• Registers play a major role inkeeping track of information forfunction 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.CS 61C L11 Introduction to MIPS: Procedures I (6) Garcia, Fall 2004 © UCBInstruction Support for Functions (1/6) ... sum(a,b);... /* a,b:$s0,$s1 */}int sum(int x, int y) {return x+y;} address1000100410081012101620002004CMIPSIn MIPS, all instructionsare 4 bytes, and stored inmemory just like data. Sohere we show theaddresses of where theprograms are stored.CS 61C L11 Introduction to MIPS: Procedures I (7) Garcia, Fall 2004 © UCBInstruction Support for Functions (2/6) ... sum(a,b);... /* a,b:$s0,$s1 */}int sum(int x, int y) {return x+y;} address1000 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 instructionCMIPSCS 61C L11 Introduction to MIPS: Procedures I (8) Garcia, Fall 2004 © UCBInstruction Support for Functions (3/6) ... sum(a,b);... /* a,b:$s0,$s1 */}int sum(int x, int y) {return x+y;}2000 sum: add $v0,$a0,$a12004 jr $ra # new instructionCMIPS• Question: Why use jr here? Why notsimply use j?• Answer: sum might be called by manyfunctions, so we can’t return to a fixedplace. The calling proc to sum must be ableto say “return here” somehow.CS 61C L11 Introduction to MIPS: Procedures I (9) Garcia, Fall 2004 © UCBInstruction Support for Functions (4/6)• Single instruction to jump and save returnaddress: jump and link (jal)• Before:1008 addi $ra,$zero,1016 #$ra=10161012 j sum #goto sum• After:1008 jal sum # $ra=1012,goto sum• Why have a jal? Make the common casefast: function calls are very common. Also,you don’t have to know where the code is loaded into memory with jal.CS 61C L11 Introduction to MIPS: Procedures I (10) Garcia, Fall 2004 © UCBInstruction Support for Functions (5/6)• Syntax for jal (jump and link) is sameas for j (jump):jal label• ! jal should really be called laj for“link and jump”:• Step 1 (link): Save address of nextinstruction into $ra (Why nextinstruction? Why not current one?)• Step 2 (jump): Jump to the given labelCS 61C L11 Introduction to MIPS: Procedures I (11) Garcia, Fall 2004 © UCBInstruction Support for Functions (6/6)• Syntax for jr (jump register):jr register• Instead of providing a label to jump to,the jr instruction provides a registerwhich contains an address to jump to.• Only useful if we know exact address tojump to.• Very useful for function calls:•jal stores return address in register ($ra)•jr $ra jumps back to that addressCS 61C L11 Introduction to MIPS: Procedures I (12) Garcia, Fall 2004 © UCBAdministrivia• Newsgroup growing out of control…• Read postings before posting!• Read Errata.txt for each project/hw beforeposting• Project 1 out (make sure to work on it thisweekend), due next Friday• An easy HW4 will follow, due Wed after• UCB Programming contest tomorrow from1000 - 1530 in 306 Soda!• If you partake, EPA! Points! + 2 slip days• www.cs/~hilfingr/programming-contest• Dan’s videos: www.siggraph.org/publications/video-review/SVR.htmlCS 61C L11 Introduction to MIPS: Procedures I (13) Garcia, Fall 2004 © UCBNested Procedures (1/2)int sumSquare(int x, int y) {return mult(x,x)+ y;}• Something called sumSquare, nowsumSquare is calling mult.• So there’s a value in $ra thatsumSquare wants to jump back to, butthis will be overwritten by the call tomult.• Need to save sumSquare return addressbefore call to mult.CS 61C L11 Introduction to MIPS: Procedures I (14) Garcia, Fall 2004 © UCBNested Procedures (2/2)• In general, may need to save someother info in addition to $ra.• When a C program is run, there are 3important memory areas allocated:• Static: Variables declared once perprogram, cease to exist only afterexecution completes. E.g., C globals• Heap: Variables declared dynamically• Stack: Space to be used by procedureduring execution; this is where we cansave register valuesCS 61C L11 Introduction to MIPS: Procedures I (15) Garcia, Fall 2004 © UCBC memory Allocation review0∞AddressCodeProgramStaticVariables declaredonce per programHeapExplicitly created space, e.g., malloc(); C pointersStackSpace for saved
View Full Document