DOC PREVIEW
Berkeley COMPSCI 61C - Lecture Notes

This preview shows page 1-2-3-22-23-24-45-46-47 out of 47 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 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 47 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 47 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 47 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 47 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 47 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 47 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 47 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 47 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Topic OutlineC functionsFunction Call BookkeepingSlide 5Instruction Support for Functions (1/6)Instruction Support for Functions (2/6)Instruction Support for Functions (3/6)Instruction Support for Functions (4/6)Instruction Support for Functions (5/6)Instruction Support for Functions (6/6)Nested Procedures (1/2)Nested Procedures (2/2)C memory Allocation reviewUsing the Stack (1/2)Using the Stack (2/2)Steps for Making a Procedure CallRules for ProceduresMIPS RegistersOther RegistersBasic Structure of a FunctionRegister Conventions (1/4)Slide 23Register Conventions (2/4) - savedRegister Conventions (3/4) - volatileRegister Conventions (4/4)Peer Instruction 1AdministriviaSlide 29Bitwise OperationsLogical Operators (1/3)Logical Operators (2/3)Logical Operators (3/3)Uses for Logical Operators (1/3)Uses for Logical Operators (2/3)Uses for Logical Operators (3/3)Shift Instructions (1/4)Shift Instructions (2/4)Shift Instructions (3/4)Shift Instructions (4/4)Peer Instruction: Compile This (1/5)Peer Instruction: Compile This (2/5)Peer Instruction: Compile This (3/5)Peer Instruction: Compile This (4/5)Peer Instruction: Compile This (5/5)“And in Conclusion…” (1/2)“And in Conclusion…” (2/2)CS 61C L08 MIPS Procedures (1)A Carle, Summer 2005 © UCBinst.eecs.berkeley.edu/~cs61c/su05CS61C : Machine StructuresLecture #8: MIPS Procedures2005-06-30Andy CarleCS 61C L08 MIPS Procedures (2)A Carle, Summer 2005 © UCBTopic Outline•Functions•More Logical OperationsCS 61C L08 MIPS Procedures (3)A Carle, Summer 2005 © 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 L08 MIPS Procedures (4)A Carle, Summer 2005 © UCBFunction Call Bookkeeping•What are the properties of a function?•Function call transfers control somewhere else and then returns.•Arguments•Return Value•Black-box operation/scoping•Re-entranceCS 61C L08 MIPS Procedures (5)A Carle, Summer 2005 © UCBFunction 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.CS 61C L08 MIPS Procedures (6)A Carle, Summer 2005 © UCBInstruction Support for Functions (1/6) ... sum(a,b);... /* a,b:$s0,$s1 */}int sum(int x, int y) {return x+y;} address1000 1004 1008 1012 1016 2000 2004CMIPSIn 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.CS 61C L08 MIPS Procedures (7)A Carle, Summer 2005 © 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 = b 1008 addi $ra,$zero,1016 #ra=10161012 j sum #jump to sum1016 ...2000 sum: add $v0,$a0,$a12004 jr $ra # new instructionCMIPSCS 61C L08 MIPS Procedures (8)A Carle, Summer 2005 © 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 not simply use j?•Answer: sum might be called by many functions, so we can’t return to a fixed place. The calling proc to sum must be able to say “return here” somehow.CS 61C L08 MIPS Procedures (9)A Carle, Summer 2005 © UCBInstruction Support for Functions (4/6)•Single instruction to jump and save return address: jump and link (jal)•Before:1008 addi $ra,$zero,1016 #$ra=10161012 j sum #go to sum•After:1008 jal sum # $ra=1012,go to sum•Why have a jal? Make the common case fast: function calls are very common. Also, you don’t have to know where the code is loaded into memory with jal.CS 61C L08 MIPS Procedures (10)A Carle, Summer 2005 © UCBInstruction Support for Functions (5/6)•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 labelCS 61C L08 MIPS Procedures (11)A Carle, Summer 2005 © 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 register which contains an address to jump to.•Only useful if we know exact address to jump to.•Very useful for function calls:•jal stores return address in register ($ra)•jr $ra jumps back to that addressCS 61C L08 MIPS Procedures (12)A Carle, Summer 2005 © UCBNested Procedures (1/2)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.CS 61C L08 MIPS Procedures (13)A Carle, Summer 2005 © UCBNested Procedures (2/2)•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•Stack: Space to be used by procedure during execution; this is where we can save register valuesCS 61C L08 MIPS Procedures (14)A Carle, Summer 2005 © UCBC memory Allocation review0AddressCodeProgramStaticVariables declaredonce per programHeapExplicitly created space, e.g., malloc(); C pointersStackSpace for saved procedure information$sp stackpointerCS 61C L08 MIPS Procedures (15)A Carle, Summer 2005 © UCBUsing the Stack (1/2)•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;}CS 61C L08 MIPS Procedures (16)A Carle, Summer 2005 © UCBUsing the Stack (2/2)•Hand-compilesumSquare: addi $sp,$sp,-8 # space on stack sw $ra, 4($sp) # save ret


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?