1 CS61C L6 Machine Rep Clap if you like pizza! Pointless Poll.

Slides:



Advertisements
Similar presentations
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 – Introduction to MIPS Procedures I If cars broadcast their speeds to.
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
The University of Adelaide, School of Computer Science
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
CS61C L8 Introduction to MIPS : Decisions II & Procedures I (1) Pearce, Summer 2010 © UCB inst.eecs.berkeley.edu/~cs61c UCB CS61C.
CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L11 Introduction to MIPS: Procedures I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
1 CS 430 Computer Architecture Clap if you like pizza! Pointless Poll.
CS61C L8 MIPS Procedures (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #8 – MIPS.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 7: Procedures I Partially adapted from Computer Organization and Design, 4 th edition,
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
Lecture 6: Procedure Call (Part I)
CS61C L11 Introduction to MIPS : Procedures I (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Machine Structures Lecture 4 – Introduction to MIPS Procedures I
Procedures I Fall 2010 Lecture 6: Procedures. Procedures I Fall 2010 Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);...
1 CS 430 Computer Architecture Clap if you like pizza! Pointless Poll.
CS61C L8 Decisions(2), Procedures(1) (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture.
Procedures I Fall 2005 C functions main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */ int mult (int mcand,
CS61C L11 Introduction to MIPS : Procedures I (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L08 MIPS Procedures (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #8: MIPS Procedures
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
Procedure Conventions & The Stack Jen-Chang Liu, Spring 2006 Adapted from
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 11 – Introduction to MIPS Procedures I Internet2, a non-profit advanced.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013.
CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions 1 Instructors: John Wawrzynek & Vladimir Stojanovic
CS61C L8 MIPS Procedures (1) Garcia, Fall 2005 © UCB Lecturer PSOE, new dad Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
1 CS61C L6 Machine Rep CENG 311 Procedure Conventions & The Stack.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
IT 251 Computer Organization and Architecture Introduction to MIPS Procedures Chia-Chi Teng.
CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Cs 61C L4 Functions.1 Patterson Spring 99 ©UCB CS61C Functions, Procedures in C/Assembly Language Lecture 4 January 29, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 – Introduction to MIPS Procedures I Apple took a bold step recently.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
1. CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions 2 Instructors: Vladimir Stojanovic & Nicholas Weaver
Decisions (cont), Procedures. Review Memory transfer: lw, sw, lb, sb A Decision allows us to decide what to execute at run-time rather than compile-time.
Rocky K. C. Chang Version 0.1, 25 September 2017
CSCI206 - Computer Organization & Programming
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Procedures (Functions)
April 2006 Saeid Nooshabadi
Instructions - Type and Format
Krste Asanović and Randy H. Katz
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
CS61C : Machine Structures Lecture 2. 2
Procedures and addressing
Where is all the knowledge we lost with information? T. S. Eliot
Reduced Instruction Set Computer (RISC)
Pointless Poll Clap if you like pizza!.
Presentation transcript:

1 CS61C L6 Machine Rep Clap if you like pizza! Pointless Poll

2 CS61C L6 Machine Rep In “Lecture II: C vs. Java,” The Stack played several roles. Which role(s) did The Stack play? CS Trivia a.Mr. Space for Local Variables b.Mme. Space for the Stack Pointer c.Ms. Space for the Return Address d.King Space for Global Variables e.Dr. Space for Arguments f.Lt. Space for World Peace

3 CS61C L6 Machine Rep In “Lecture II: C vs. Java,” The Stack played several roles. Which role(s) did The Stack play? CS Trivia Answer a.Mr. Space for Local Variables some local vars are on The Stack c. Ms. Space for the Return Address save $ra if calling another function e. Dr. Space for Arguments use stack for “a4,” “a5,” “a6,”...

4 CS61C L6 Machine Rep CS61C - Machine Structures Lecture 7 - Procedure Conventions & The Stack September 20, 2000 David Oppenheimer & Steve Tu

5 CS61C L6 Machine Rep Review 1/2 °3 formats of MIPS instructions in binary: Op field determines format °Operands Registers: $0 to $31 mapped onto $zero, $at, $v_, $a_, $s_, $t_, $gp, $sp, $fp, $ra Memory : Memory[0], Memory[4], Memory[8],..., Memory[ ] -Index is the “address” 6 bits5 bits 6 bits oprsrtrdfunctshamt oprsrtimmediate R I

6 CS61C L6 Machine Rep Review 2/2 °Big Idea: Stored Program Concept Assembly language instructions encoded as numbers (machine language) Everything has an address, even instructions Pointer in C == Address in MAL °3 pools of memory: Static: global variables The Heap: dynamically allocated ( malloc() ) The Stack: some local variables, some arguments, return address

7 CS61C L6 Machine Rep Overview °C Functions °MIPS Instructions for Procedures °The Stack °Administrivia °Procedure Conventions °Practice Compilation °Instructions Potpourri °Conclusion

8 CS61C L6 Machine Rep C functions main() { int i,j,k,m; i = mult(j,k);... ; m = mult(i,i);... } int mult (int mcand, int mlier){ int product; product = 0; while (mlier > 0) { product = product + mcand; mlier = mlier -1; } return product; } What information must compiler/programmer keep track of?

9 CS61C L6 Machine Rep Function Call Bookkeeping Labels $ra $a0, $a1, $a2, $a3 $v0, $v1 $s0, $s1, …, $s7 ; °Procedure address °Return address °Arguments °Return value °Local variables °Most problems above are solved simply by using register conventions.

10 CS61C L6 Machine Rep Instruction Support for Functions (1/4)... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } address 1000 add $a0,$s0,$zero # x = a 1004 add $a1,$s1,$zero # y = b 1008 addi $ra,$zero,1016 #$ra= j sum #jump to sum sum: add $v0,$a0,$a jr $ra# new instruction C MIPSMIPS

11 CS61C L6 Machine Rep Instruction Support for Functions (2/4) °Single instruction to jump and save return address: jump and link ( jal ) °Before: 1008 addi $ra,$zero,1016 #$ra= j sum #go to sum °After: 1012 jal sum # $ra=1016,go to sum °Why have a jal ? Make the common case fast: functions are very common.

12 CS61C L6 Machine Rep Instruction Support for Functions (3/4) °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?) Step 2 (jump): Jump to the given label

13 CS61C L6 Machine Rep Instruction Support for Functions (4/4) °Syntax for jr (jump register): jrregister °Instead of providing a label to jump to, the jr instruction provides a register that contains an address to jump to. °Usually used in conjunction with jal, to jump back to the address that jal stored in $ra before function call.

14 CS61C L6 Machine Rep Nested 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.

15 CS61C L6 Machine Rep Nested 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 Heap: Variables declared dynamically Stack: Space to be used by procedure during execution; this is where we can save register values -Not identical to the “stack” data structure!

16 CS61C L6 Machine Rep C memory Allocation 0  Address Code Program Static Variables declared once per program Heap Explicitly created space, e.g., malloc(); C pointers Stack Space for saved procedure information $sp stack pointer

17 CS61C L6 Machine Rep 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.

18 CS61C L6 Machine Rep Compiling nested C func into MIPS int sumSquare(int x, int y) { return mult (x,x)+ y; } sumSquare: subi $sp,$sp,12 # space on stack sw $ra,$ 8($sp) # save ret addr sw $a0,$ 0($sp) # save x sw $a1,$ 4($sp) # save y addi $a1,$a0,$zero # mult(x,x) jal mult # call mult lw $ra,$ 8($sp) # get ret addr lw $a0,$ 0($sp) # restore x lw $a1,$ 4($sp) # restore y add $vo,$v0,$a1 # mult()+y addi $sp,$sp,12 # => stack space jr $ra C Epilogue Prologue Body

19 CS61C L6 Machine Rep Administrivia (1/2) Most assignments are now submitted online (even homeworks)! Vote on when Proj2 (sprintf) is due Fri at midnight vs. Sun at midnight Lab4 due by the beginning of your discussion this week Book has errata. See errata on web! Errata has errata?

20 CS61C L6 Machine Rep Return of M’Piero! Summer Misherghi, Alex Fabrikant M’Piero created by David A. Patterson I will find Patterson and bring him back!

21 CS61C L6 Machine Rep The Functional Contract  Definitions  Caller: function making the call, using jal  Callee: function being called  Before the Functional Contract, there was anarchy  Shared registers => Callee overwrote Caller’s information  Functional Contract established to bring peace to the land  Callee’s Rights and Responsibilities  Caller’s Rights and Responsibilities

22 CS61C L6 Machine Rep Callee’s Responsibilities (how to write a fn) 1.If using $s or big local structs, slide $sp down to reserve memory: e.g. addi $sp, $sp, If using $s, save before using: e.g. sw $s0, 44($sp) 3.Receive args in $a0-3, add’l args on stack 4.Run the procedure body 5.If not void, put return values in $v0,1 6.If applicable, undo steps 2-1 e.g. lw $s0, 44($sp) addi $sp, $sp, 48 7.jr $ra

23 CS61C L6 Machine Rep Compile using pencil and paper! (1/2) int Doh(int i, int j, int k, int m, char c, int n){ return i+j+n; } Doh: _______________ add $a0, _______ _______________

24 CS61C L6 Machine Rep Compile using pencil and paper! (2/2) int Doh(int i, int j, int k, int m, char c, int n){ return i+j+n; } Doh: _______________ add $a0, _______ _______________ lw $t0, 20($sp) $a0, $a1 add $v0, $a0, $t0 jr $ra 6th argument VAT Safe For Callee

25 CS61C L6 Machine Rep Caller’s Responsibilities (how to call a fn) 1.Slide $sp down to reserve memory: e.g. addi $sp, $sp, Save $ra on stack b/c jal clobbers it: e.g. sw $ra, 24 ($sp) 3.If you’ll still need their values after the function call, save $v, $a, $t on stack or copy to $s registers. Callee can overwrite VAT, but not S. 4.Put first 4 words of args in $a0-3, at most 1 arg per word, add’l args go on stack: “a4” is 16($sp) 5.jal to the desired function 6.Receive return values in $v0, $v1 7.Undo steps 3-1: e.g. lw $t0, 20($sp) lw $ra, 24($sp) addi $sp, $sp, 28 Keep this slide in mind for exam cheat sheet

26 CS61C L6 Machine Rep Hard Compilation Problem (1/2) int Doh(int i, int j, int k, int m, char c, int n){ return i+j+n; } int Sum(int m, int n); /* returns m + n */ Doh: ______________ ______________ jal ___________ add ___________ _______________

27 CS61C L6 Machine Rep Slow-motion Replay (1/2) int Doh(int i, int j, int k, int m, char c, int n){ return i+j+n; } int Sum(int m, int n); /* returns m + n */ Doh: ______________ ______________ jal ___________ add ___________ _______________ sw $ra, ($sp) addi $sp, $sp,- lw $ra, ($sp) addi $sp, $sp, jr $ra Sum lw, ($sp)$s0 2.Saving $ra => must move $sp 1.Calling Sum => save $ra,VAT 3.Need n after funccall => $s0

28 CS61C L6 Machine Rep Callees’ Rights, Callers’ Rights  Callees’ Rights  Right to use VAT registers freely  Right to assume args are passed correctly  Callers’ Rights  Right to use S registers without fear of being overwritten by Callee  Right to assume return value will be returned correctly

29 CS61C L6 Machine Rep Legalese 1.Proj2 (sprintf) passes all args on the stack. 2.Floating point has different conventions. 3.Variable # of arguments, specified by … in C, is slightly different. 4.See manuals for details.

30 CS61C L6 Machine Rep Instructions Potpourri 9 new instructions in 9 minutes! Multiplication and Division: mult, multu, div, divu, mfhi, mflo Accessing Individual Bytes Instead of Words: lb, lbu, sb

31 CS61C L6 Machine Rep Hi Lo X $t $t1 Multiplication mult $t1, $t2 # t1 * t2 No dest register: Product could be ~2^64; need two special registers to hold it 3-step process $t $t4mflo $t4 mfhi $t3

32 CS61C L6 Machine Rep Division div $t1, $t2 # t1 / t2 Quotient stored in Lo Bonus prize: Remainder stored in Hi mflo $t3 #copy quotient to t3 mfhi $t4 #copy remainder to t4 3-step process

33 CS61C L6 Machine Rep Unsigned Multiplication and Division multu $t1, $t2 # t1 * t2 divu $t1, $t2 # t1 / t2 Just like mult, div, except now interpret t1, t2 as unsigned integers instead of signed Answers are also unsigned, use mfhi, mflo to access

34 CS61C L6 Machine Rep Data Types in MAL What if t1, t2 are signed ints, and you try to do multu, divu? a) Segmentation fault? b) Bus error? c) Green gecko? NO! None of the above! BIG IDEA: registers contain TYPELESS, MEANINGLESS BIT PATTERNS! Signed/unsigned/char/color is determined by instruction or operation

35 CS61C L6 Machine Rep Load byte, store byte lb $t0, 0($t1) ‘s’‘o’‘d’‘a’‘\0’‘y’‘e’‘h’ ‘o’ sb $t0, 0($t2) t1 t2t0 ‘o’ Similar to lw, sw, except bytes instead of words ‘o’

36 CS61C L6 Machine Rep Load byte unsigned lb $t1, 0($t0) F7Sign-extended lbu $t2, 0($t0) F7 t0 t1 t2 F7F012…… F7 FFFFFF Zero-extended

37 CS61C L6 Machine Rep Big Ideas Follow the procedure conventions and nobody gets hurt. Data is just 1’s and 0’s, what it represents depends on what you do with it M’Piero has returned to find Prof. Patterson, if he still lives!

38 CS61C L6 Machine Rep Summary of Instructions & Registers Registers we know so far $0, $at, $ra, $v_, $a_, $t_, $s_, $gp, $sp Instructions we know so far Arithmetic: add, addu, addi, addiu, sub, subu, mult, multu, div, divu, mflo, mfhi Memory: lw, sw, lb, lbu, sb Decision/Comparison: beq, bne, slt, sltu, slti, sltiu Unconditional Branches (Jumps): j, jal, jr