DOC PREVIEW
Berkeley COMPSCI 61C - Lecture Notes

This preview shows page 1-2-17-18-19-36-37 out of 37 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61C L8 MIPS Procedures (1)Beamer, Summer 2007 © UCBScott Beamer, Instructorinst.eecs.berkeley.edu/~cs61cCS61C : Machine Structures Lecture #8 – MIPS Procedures2007-7-9OpenDNS triesto makewebsurfing saferwww.nytimes.comCS61C L8 MIPS Procedures (2)Beamer, Summer 2007 © UCBReview• A Decision allows us to decide what toexecute at run-time rather than compile-time.• C Decisions are made using conditionalstatements within if, while, do while, for.• MIPS Decision making instructions are theconditional branches: beq and bne.• In order to help the conditional branchesmake decisions concerning inequalities, weintroduce a single instruction: “Set on LessThan”called slt, slti, sltu, sltiu• Unsigned add/sub don’t cause overflow• New MIPS Instructions: beq, bne, j, sll, srlslt, slti, sltu, sltiuaddu, addiu, subuCS61C L8 MIPS Procedures (3)Beamer, Summer 2007 © 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?CS61C L8 MIPS Procedures (4)Beamer, Summer 2007 © 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.CS61C L8 MIPS Procedures (5)Beamer, Summer 2007 © 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.CS61C L8 MIPS Procedures (6)Beamer, Summer 2007 © 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 instructionCMIPSCS61C L8 MIPS Procedures (7)Beamer, Summer 2007 © 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.CS61C L8 MIPS Procedures (8)Beamer, Summer 2007 © 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 case fast:function calls are very common. Also, you don’thave to know where the code is loaded intomemory with jal.CS61C L8 MIPS Procedures (9)Beamer, Summer 2007 © 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 labelCS61C L8 MIPS Procedures (10)Beamer, Summer 2007 © 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 addressCS61C L8 MIPS Procedures (11)Beamer, Summer 2007 © 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.CS61C L8 MIPS Procedures (12)Beamer, Summer 2007 © 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 valuesCS61C L8 MIPS Procedures (13)Beamer, Summer 2007 © UCBC memory Allocation review0∞AddressCodeProgramStaticVariables declaredonce per programHeapExplicitly created space, e.g., malloc(); C pointersStackSpace for saved procedure information$sp stackpointerCS61C L8 MIPS Procedures (14)Beamer, Summer 2007 © UCBUsing the Stack (1/2)• So we have a register $sp whichalways points to the last used space inthe stack.• To use stack, we decrement thispointer by the amount of space weneed and then fill it with info.• So, how do we compile this?int sumSquare(int x, int y) {return mult(x,x)+ y;}CS61C L8 MIPS Procedures (15)Beamer, Summer 2007 © UCBUsing the Stack (2/2)•Hand-compilesumSquare: addi $sp,$sp,-8 # space on stack sw $ra, 4($sp) # save ret addr sw $a1, 0($sp) # save y add $a1,$a0,$zero # prep args 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 $ramult: ...int sumSquare(int x, int y) {return mult(x,x)+ y; }“push”“pop”CS61C L8 MIPS Procedures (16)Beamer, Summer 2007 © UCBSteps for Making a Procedure Call1) Save necessary values onto stack.2) Assign argument(s), if any.3) jal call4) Restore values from stack.CS61C L8 MIPS Procedures (17)Beamer, Summer 2007 © UCBRules for Procedures• Called with a jal instruction, returnswith a jr $ra• Accepts up to 4 arguments in $a0,$a1, $a2 and $a3• Return value is always in $v0 (and ifnecessary in $v1)• Must follow register conventions(even in functions that only you willcall)! So what are they?• We’ll see these in a few slides…CS61C L8 MIPS Procedures (18)Beamer, Summer 2007 © UCBBasic Structure of a Functionentry_label:addi


View Full Document

Berkeley COMPSCI 61C - Lecture Notes

Documents in this Course
SIMD II

SIMD II

8 pages

Midterm

Midterm

7 pages

Lecture 7

Lecture 7

31 pages

Caches

Caches

7 pages

Lecture 9

Lecture 9

24 pages

Lecture 1

Lecture 1

28 pages

Lecture 2

Lecture 2

25 pages

VM II

VM II

4 pages

Midterm

Midterm

10 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?