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)

Slides:



Advertisements
Similar presentations
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.
Advertisements

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
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
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)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
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.
1 CS61C L6 Machine Rep Clap if you like pizza! Pointless Poll.
Cs 61C L3 Decisions.1 Patterson Spring 99 ©UCB CS61C Constants and Making Decisions in C/Assembly Language Lecture 3 January 27, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)
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)
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
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);...
COMP3221 lec15-function-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 15 : Functions in C/ Assembly - I
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/
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
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
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
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.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
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.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 – Introduction to MIPS Procedures I Apple took a bold step recently.
Cs 61C L5 Machine Lang.1 Patterson Spring 99 ©UCB CS61C Instruction Representation and Machine Language Lecture 5 February 3, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)
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.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
April 2006 Saeid Nooshabadi
Procedures (Functions)
Procedures (Functions)
April 2006 Saeid Nooshabadi
Instructions - Type and Format
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
CS61C : Machine Structures Lecture 2. 2
Procedures and addressing
Computer Architecture
Presentation transcript:

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) www-inst.eecs.berkeley.edu/~cs61c/schedule.html

cs 61C L4 Functions.2 Patterson Spring 99 ©UCB Review 1/1 °Constants so common have special version of arithmetic, registers addi, subi; register $zero (always 0) Principle: Making common case fast °HLL decisions (if, case) and loops (while, for) use same assembly instructions Conditional branches: beq, bne in MIPS Unconditional branches: j, jr in MIPS Relative test: slt, slti in MIPS Case/Switch: either jump table + jr or simply chained if-else

cs 61C L4 Functions.3 Patterson Spring 99 ©UCB Review 2/2 °MIPS assembly language instructions Arithmetic: add,sub,addi, subi Data transfer: lw, sw, Relative test: slt, slti Conditional branches: beq, bne Unconditional branches: j, jr °Operands Registers (word = 32 bits): $zero ; $s0, $s1,... ; $t0, $t1,... ; Memory (8-bit byte address, 4 bytes/word): Memory[0], Memory[4], Memory[8],,..., Memory[ ]

cs 61C L4 Functions.4 Patterson Spring 99 ©UCB Overview °C functions (2 minutes) °Bookkeeping for function call/return °Instruction support for functions °Nested function calls °C memory allocation: static, heap, stack °Administrivia,“Computers in the news” °Resolving Registers Conflicts (6 min) °Frame/Stack pointer (12) °C/Assembly Examples (6 min) °Conclusion (1 minute)

cs 61C L4 Functions.5 Patterson Spring 99 ©UCB 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?

cs 61C L4 Functions.6 Patterson Spring 99 ©UCB Function Call Bookkeeping °Registers for functions °$ra °$a0, $a1, $a2, $a3 °$v0, $v1 °$s0, $s1, …, $s7 ; °Procedure address °Return address °Arguments °Return value °Local variables °Registers (conflicts)

cs 61C L4 Functions.7 Patterson Spring 99 ©UCB Instruction Support for Functions?... 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 C MIPSMIPS Why jr vs. j to return?

cs 61C L4 Functions.8 Patterson Spring 99 ©UCB Instruction Support for Functions? °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 jal ? Make the common case fast

cs 61C L4 Functions.9 Patterson Spring 99 ©UCB Nested Procedures int sumSquare(int x, int y) { return mult (x,x)+ y; } °Need to save sumSquare return address before call mult Otherwise jal mult overwrites $ra °One word per procedure in memory ? e.g., sw $ra, sumSquareRA($s3) °Recursive procedures could overwrite saved area => need safe area per function invocation => stack

cs 61C L4 Functions.10 Patterson Spring 99 ©UCB C memory allocation seen by the Program 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 global pointer $gp

cs 61C L4 Functions.11 Patterson Spring 99 ©UCB Compiling C if into MIPS: Summary °Compile by hand 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

cs 61C L4 Functions.12 Patterson Spring 99 ©UCB Exceeding limits of registers °Recall: assembly language has fixed number of operands, HLL doesn’t °Local variables: $s0,..., $s7 What if more than 8 words of local variables? °Arguments; $a0,..., $a3 What if more than 4 words of arguments? °Place extra variables and extra arguments onto stack ($sp) °Use temp registers and data transfers to access these variables

cs 61C L4 Functions.13 Patterson Spring 99 ©UCB Administrivia °Readings: 3.6, Appendix A.6; next 3.4,3.8 °1st project: C spelling checker philspel Due Wed. 2/3 7PM (do by yourself) www-inst.EECS/~cs61c/handouts/proj1.pdf “C Survial” Mon 2/1 5:30-7, 306 Soda by CSUA °Change from 1 week ago: team size < 3 °2nd homework: Due Wed 2/3 7PM Exercises 3.1, 3.2, 3.4, 3.6, 3.9; Which instruction set inside? Search WWW  Apple iMAC  Casio PalmPC  Cisco Network Routers  Nintendo 64  Sony Playstation  Web TV set top box  HP LaserJet 4000  IBM PC  Kodak DC260  NASA Mars Rover

cs 61C L4 Functions.14 Patterson Spring 99 ©UCB Survey Results °61A 94% UC, 3.2 GPA °61B 63% UC, 3.2 GPA °9C (S.P. C)? 10% °9C (S.P. C++)? 15% °Printer? 20% elsewhere °Print at night? 1/3 Before 8AM? 1/3 No Stairs? 10% Free energy? 5% Special? 7% Java Scheme C C++ Pascal Basic

cs 61C L4 Functions.15 Patterson Spring 99 ©UCB “Computers in the News” °“Intel Alters Plan Said To Undermine PC Users' Privacy”, 1st p., N.Y. Times 1/26/99 °Processor-specific IDs per chip accessed by SW and transmitted over the Internet -96-bit unique serial number: 32 CPU type+64 ID -Idea: ID helps intellectual property protection, tying apps, information to a specific machine °“Big Brother” inside? Boycott Intel! -No anonymity? Track 1 consumer over Internet? °“The Intel Corporation yesterday reversed a plan to activate an identifying signature in its next generation of computer chips, bowing to protests that the technology would compromise the privacy of users.” -Not removed; default now off on reboot

cs 61C L4 Functions.16 Patterson Spring 99 ©UCB Function Call Bookkeeping: thus far °Procedure address x °Return address x °Arguments x °Return value x °Local variables x °Registers (conflicts)

cs 61C L4 Functions.17 Patterson Spring 99 ©UCB Register Conflicts °Procedure A calls Procedure B A referred to as is “calling procedure” or “caller” B referred to as is “called procedure” or “callee” °Both A and B want to use the 32 registers, but must cooperate

cs 61C L4 Functions.18 Patterson Spring 99 ©UCB Register Conflicts: 2 options (A calls B) 1) Called procedure/callee (B) leaves registers the way it found them (except $ra); its B’s job to save it before using it and then restore it: “callee saves” Since B only saves what it changes, more accurate is “callee saves (what it uses)” 2) B can use any register it wants; Calling procedure/caller A must save any register it wants to use after call of B: “caller saves” Since A knows what it needs after call, more accurate is “caller saves (if it wants to)”

cs 61C L4 Functions.19 Patterson Spring 99 ©UCB MIPS Solution to Register Conflicts °Divide registers into groups Saved registers ($s0-$s7) Temporary regs ($t0-$t9), Argument ($a0-$a3) Some caller saved (if used) and some callee saved (if used) °Caller (A) save/restore temporary ($t0-$t9) and argument ($a0-$a3) if needs them after the call; also $ra => callee can use $ti,$ai,$ra °Callee (B) must save/restore saved registers ($s0-$s7) if it uses them => caller can $si Procedure that doesn’t call another tries to use only temporary and argument registers

cs 61C L4 Functions.20 Patterson Spring 99 ©UCB Memory Allocation... sum(a,b);... } int sum(int x, int y) { return x+y; } a => x b => y CS61A name for? Frame

cs 61C L4 Functions.21 Patterson Spring 99 ©UCB Memory Allocation °C Procedure Call Frame °Pass arguments (4 regs) °Save caller-saved regs °jal °space on stack ($sp-n) word of frame °Save callee-saved regs °set $fp ($sp+n-4) word of frame Saved Registers Argument 5 Argument 6... $fp low high Address stack grows Local Variables $sp

cs 61C L4 Functions.22 Patterson Spring 99 ©UCB Frame Pointer Optional °If allocate all stack space need at beginning of procedure (e.g., space for any arguments for any procedure might call), °Then don’t need to separate frame pointer to refer to variables; refer to every variable from $sp °GCC uses frame pointer, MIPS compilers don’t (get extra temporary register, less bookkeeping on procedure call)

cs 61C L4 Functions.23 Patterson Spring 99 ©UCB MIPS Register Summary °Registers Total Regs $Zero1 (Return) Value registers ($v0,$v1)3 Argument registers ($a0-$a3)7 Return Address ($ra) 8 Saved registers ($s0-$s7)16 Temporary registers ($t0-$t9)26 Global Pointer ($gp)27 Stack Pointer ($sp)28 Frame Pointer ($fp), or $t1029 °2 for OS ($k0, $k1), 1 for assembler ($at)

cs 61C L4 Functions.24 Patterson Spring 99 ©UCB HLL and Frames °C,C++, Java follow “Stack Discipline”; e.g., D cannot return to A Frames can be adjacent in memory Frames can be allocated, discarded as a FIFO queue (stack) Main A B C D E What about Scheme?

cs 61C L4 Functions.25 Patterson Spring 99 ©UCB If there is time, Compile this MIPS code: °main() { int i,j,k,m; /* i-m:$s0-$s3 */ 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; }

cs 61C L4 Functions.26 Patterson Spring 99 ©UCB (If time allows) Do it yourself: int i,j,k,m; /* i-m:$s0-$s3 */ i = mult(j,k);... ; m = mult(i,i);... add $a0,$s1,$zero# arg0 = j add $a1,$s2,$zero# arg1 = k jal mult # call mult add $s0,$vo,$zero# i = mult()... add $a0,$s0,$zero# arg0 = i add $a1,$s0,$zero# arg1 = i jal mult # call mult add $s3,$vo,$zero# m = mult()... C MIPSMIPS

cs 61C L4 Functions.27 Patterson Spring 99 ©UCB (If time allows) Do it yourself: int mult (int mcand, int mlier){ int product; product = 0; while (mlier > 0) { product = product + mcand; mlier = mlier - 1; } return product; add $t0,$zero,$zero# prod = 0 Loop:slt $t1,$zero,$a1# mlr > 0? beq $t1,$zero,Exit# no=>Exit add $t0,$t0,$a0# prod+=mc subi $a1,$a1,1# mlt=mlr-1 j Loop# goto Loop Exit:add $v0,$t0,$zero# $v0=prod jr $ra °instructions per loop iteration? C MIPSMIPS Save Registers?

cs 61C L4 Functions.28 Patterson Spring 99 ©UCB (If time allows) Do it yourself: int mult (int mcand, int mlier){ int product; product = 0; while (mlier > 0) { product = product + mcand; mlier = mlier - 1; } return product; add $v0,$zero,$zero# prod = 0 slt $t1,$zero,$a1# mlr > 0? beq $t1,$zero,Exit# no=>Exit Loop:add $v0,$v0,$a0# prod+=mc subi $a1,$a1,1# mlt=mlr-1 slt $t1,$zero,$a1# mlr > 0? bne $t1,$zero,Loop# yes=>Loop Exit: jr $ra # $v0 °4 v. 5 instructions/loop iteration; 1 less C MIPSMIPS

cs 61C L4 Functions.29 Patterson Spring 99 ©UCB “And in Conclusion …” 1/2 °MIPS assembly language instructions Arithmetic: add,sub,addi, subi Data transfer: lw, sw Relative test: slt, slti Conditional branches: beq, bne Unconditional branches: j, jr, jal °Operands Registers (word = 32 bits): $zero ; $v0, $v1, $a0 - $a3, $s0 - $s7, $t0 - $t9, $gp, $sp, $fp, $ra Memory (8-bit byte address, 4 bytes/word): Memory[0], Memory[4], Memory[8],,..., Memory[ ]

cs 61C L4 Functions.30 Patterson Spring 99 ©UCB “And in Conclusion …” 2/2 °Functions, procedures one of main ways to give a program structure, reuse code °jr required instruction; most add jal (or equivalent) to make common case fast °Registers make programs fast, but make procedure/function call/return tricky °MIPS SW convention divides registers into those calling procedure save/restore and those called procedure save/restore Assigns registers to arguments, return address, return value, stack pointer °Next Machine Representation: COD 3.4,3.8