Cs 61C L8 Proc II., Arrays and Pointers in C 1 Patterson Fall 00 ©UCB CS61C - Machine Structures Lecture 8 - Procedure Conventions part II, plus, Arrays.

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
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
MIPS Calling Convention Chapter 2.7 Appendix A.6.
COMP3221 lec18-pointers.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 18 : Pointers & Arrays in C/ Assembly
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
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.
Kernighan/Ritchie: Kelley/Pohl:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Cs 61C L6 Instruction, Proc II.1 Patterson Spring 99 ©UCB CS61C Instruction Representation and Functions, Part II Lecture 6 February 5, 1999 Dave Patterson.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
1 CS 430 Computer Architecture Clap if you like pizza! Pointless Poll.
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
COMP3221 lec18-function-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 17 : Functions in C/ Assembly - III
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly
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/
CWRU EECS 3221 Language of the Machine EECS 322 Computer Architecture Instructor: Francis G. Wolff Case Western Reserve University.
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
CDA 3101 Fall 2013 Introduction to Computer Organization Pointers & Arrays MIPS Programs 16 September 2013.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
CWRU EECS 3141 Language of the Machine EECS 314 Computer Architecture Instructor: Francis G. Wolff Case Western Reserve University.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
CS61C Midterm 1 Review Summer 2004 Pooya Pakzad Ben Huang Navtej Sadhal.
1 CS61C L6 Machine Rep CENG 311 Procedure Conventions & The Stack.
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.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Storage Allocation Mechanisms
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
Procedures (Functions)
Procedures (Functions)
CDA 3101 Spring 2016 Introduction to Computer Organization
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Program and memory layout
Program and memory layout
April 2006 Saeid Nooshabadi
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
MIPS function continued
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

cs 61C L8 Proc II., Arrays and Pointers in C 1 Patterson Fall 00 ©UCB CS61C - Machine Structures Lecture 8 - Procedure Conventions part II, plus, Arrays and Pointers in C September 22, 2000 Sumeet Shendrikar& Daniel C. Silverstein

cs 61C L8 Proc II., Arrays and Pointers in C 2 Patterson Fall 00 ©UCB Review 1/3 °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 Function Call Bookkeeping: Caller Saved Registers are saved by the caller, that is, the function that includes the jal instruction Callee Saved Registers are saved by the callee, that is, the function that includes the jr $ra instruction Some functions are both a caller and a callee

cs 61C L8 Proc II., Arrays and Pointers in C 3 Patterson Fall 00 ©UCB Review 2/3 Caller Saved Registers: Return address$ra Arguments$a0, $a1, $a2, $a3 Return value$v0, $v1 $t Registers$t0 - $t9 Callee Saved Registers: $s Registers$s0 - $s7

cs 61C L8 Proc II., Arrays and Pointers in C 4 Patterson Fall 00 ©UCB Review 3/3 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 L8 Proc II., Arrays and Pointers in C 5 Patterson Fall 00 ©UCB Overview °Why Procedure Conventions? °Basic Structure of a Function °Example: Recursive Function °Administrivia °Arrays, Pointers, Functions in C °Example °Pointers, Arithmetic, and Dereference °Conclusion

cs 61C L8 Proc II., Arrays and Pointers in C 6 Patterson Fall 00 ©UCB Why Procedure Conventions? 1/2 °Think of procedure conventions as a contract between the Caller and the Callee If both parties abide by a contract, everyone is happy ( : ) ) If either party breaks a contract, disaster and litigation result ( : O ) °Similarly, if the Caller and Callee obey the procedure conventions, there are significant benefits. If they don’t, disaster and program crashes result

cs 61C L8 Proc II., Arrays and Pointers in C 7 Patterson Fall 00 ©UCB Why Procedure Conventions? 2/2 °Benefits of Obeying Procedure Conventions: People who have never seen or even communicated with each other can write functions that work together Recursion functions work correctly

cs 61C L8 Proc II., Arrays and Pointers in C 8 Patterson Fall 00 ©UCB Basic Structure of a Function entry_label: addi $sp,$sp, -framesize sw $ra, framesize-4($sp)# save $ra save other regs... restore other regs lw $ra, framesize-4($sp)# restore $ra addi $sp,$sp, framesize jr $ra Epilogue Prologue Body ra

cs 61C L8 Proc II., Arrays and Pointers in C 9 Patterson Fall 00 ©UCB Example: Fibonacci Numbers 1/ °The Fibonacci numbers are defined as follows: F(n) = F(n – 1) + F(n – 2), F(0) and F(1) are defined to be 1 °In scheme, this could be written: (define (Fib n) (cond((= n 0) 1) ((= n 1) 1) (else (+(Fib (- n 1)) (Fib (- n 2)))))

cs 61C L8 Proc II., Arrays and Pointers in C 10 Patterson Fall 00 ©UCB Example: Fibonacci Numbers 2/ °Rewriting this in C we have: int fib(int n) { if(n == 0) { return 1; } if(n == 1) { return 1; } return (fib(n - 1) + fib(n - 2)); }

cs 61C L8 Proc II., Arrays and Pointers in C 11 Patterson Fall 00 ©UCB ___________________ fib: ___________________ # Space for three words # Save the return address # Save $s0 addi $sp, $sp, -12 sw $ra, 8($sp) sw $s0, 4($sp) °Now, let’s translate this to MIPS! °You will need space for three words on the stack °The function will use one $s register, $s0 °Write the Prologue: Example: Fibonacci Numbers 3/

cs 61C L8 Proc II., Arrays and Pointers in C 12 Patterson Fall 00 ©UCB fin: ___________________ jr $ra_____________ lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 # Restore $s0 # Restore return address # Pop the stack frame # Return to caller ___________________ °Now write the Epilogue: Example: Fibonacci Numbers 4/

cs 61C L8 Proc II., Arrays and Pointers in C 13 Patterson Fall 00 ©UCB beq $a0 $zero beq $a0 $t0 addi $v0, $zero, 1_ _______, _____,_fin addi $t0, $zero, 1_ _______,______,_fin Contiued on next slide... # $v0 = 1 # # $t0 = 1 # °Finally, write the body. The C code is below. Start by translating the lines indicated in the comments int fib(int n) { if(n == 0) { return 1; } /*Translate Me!*/ if(n == 1) { return 1; } /*Translate Me!*/ return (fib(n - 1) + fib(n - 2)); } ___________________ __if (n == 0)... ______ ____________ __if (n == 1)... Example: Fibonacci Numbers 5/

cs 61C L8 Proc II., Arrays and Pointers in C 14 Patterson Fall 00 ©UCB $a0 0($sp) jal fib $a0 0($sp) $a0, -1 Continued on next slide... ___________________ __Need $a0 after jal _ fib(n – 1) ______ __Restore $a0______ __$a0 = n – 2_________ # $a0 = n - 1 # addi $a0, $a0, -1__ sw____, ___________ ___________________ lw____,____________ addi $a0, ___,_____ °Almost there, but be careful, this part is tricky! int fib(int n) {... return (fib(n - 1) + fib(n - 2)); } Example: Fibonacci Numbers 6/8

cs 61C L8 Proc II., Arrays and Pointers in C 15 Patterson Fall 00 ©UCB add $s0,____,______ ___________________ add $v0, $v0, $s0__ To the epilogue and beyond... _______ $v0 $zero jal fib # Place fib(n – 1) # somewhere it won’t get # clobbered # ____________________ __fib(n – 2) __________ __$v0 = fib(n-1) + fib(n-2) °Remember that $v0 is caller saved! int fib(int n) {... return (fib(n - 1) + fib(n - 2)); } Example: Fibonacci Numbers 7/8

cs 61C L8 Proc II., Arrays and Pointers in C 16 Patterson Fall 00 ©UCB °Here’s the complete code for reference: Example: Fibonacci Numbers 8/8 fib: addi $sp, $sp, -12 sw $ra, 8($sp) sw $s0, 4($sp) addi $v0, $zero, 1 beq $a0, $zero, fin addi $t0, $zero, 1 beq $a0, $t0, fin addi $a0, $a0, -1 sw $a0, 0($sp) jal fib lw $a0, 0($sp) addi $a0, $a0, -1 add $s0, $v0, $zero jal fib add $v0, $v0, $s0 fin: lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra

cs 61C L8 Proc II., Arrays and Pointers in C 17 Patterson Fall 00 ©UCB Administrivia 1/2 Most assignments are now submitted online (even homeworks)! Proj2 (sprintf) due date moved to Sunday (9/24) at midnight You voted on this in Wed lecture TAs will NOT be in the labs on Sat/Sun so seek help NOW if you need it. °Remember that you must use proper register/proc conventions in Proj2

cs 61C L8 Proc II., Arrays and Pointers in C 18 Patterson Fall 00 ©UCB Administrivia 2/2 M’Piero has found Professor Patterson! He’ll be back next week

cs 61C L8 Proc II., Arrays and Pointers in C 19 Patterson Fall 00 ©UCB Argument Passing Options °2 choices “Call by Value”: pass a copy of the item to the function/procedure “Call by Reference”: pass a pointer to the item to the function/procedure °Single word variables passed by value °Passing an array? e.g., a[100] Pascal--call by value--copies 100 words of a[] onto the stack C--call by reference--passes a pointer (1 word) to the array a[] in a register

cs 61C L8 Proc II., Arrays and Pointers in C 20 Patterson Fall 00 ©UCB Arrays, Pointers, Functions in C °4 versions of array function that adds two arrays and puts sum in a third array ( sumarray ) Third array is passed to function Using a local array (on stack) for result and passing a pointer to it Third array is allocated on heap Third array is declared static °Purpose of example is to show interaction of C statements, pointers, and memory allocation

cs 61C L8 Proc II., Arrays and Pointers in C 21 Patterson Fall 00 ©UCB Calling sumarray, Version 1 int x[100], y[100], z[100]; sumarray(x, y, z); °C calling convention means above the same as sumarray(&x[0], &y[0], &z[0]); °Really passing pointers to arrays addi $a0,$gp,0 # x[0] starts at $gp addi $a1,$gp,400 # y[0] above x[100] addi $a2,$gp,800 # z[0] above y[100] jal sumarray

cs 61C L8 Proc II., Arrays and Pointers in C 22 Patterson Fall 00 ©UCB Version 1: Optimized Compiled Code void sumarray(int a[],int b[],int c[]) { int i; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; } addi$t0,$a0,400 # beyond end of a[] Loop:beq$a0,$t0,Exit lw$t1, 0($a0) # $t1=a[i] lw$t2, 0($a1) # $t2=b[i] add$t1,$t1,$t2 # $t1=a[i] + b[i] sw$t1, 0($a2)# c[i]=a[i] + b[i] addi$a0,$a0,4# $a0++ addi$a1,$a1,4# $a1++ addi$a2,$a2,4# $a2++ jLoop Exit:jr $ra

cs 61C L8 Proc II., Arrays and Pointers in C 23 Patterson Fall 00 ©UCB Version 2 to Fix Weakness of Version 1 °Would like recursion to work int sumarray(int a[],int b[]); /* adds 2 arrays and returns sum */ sumarray(x, sumarray(y,z)); °Cannot do this with Version 1 style solution: what about this int * sumarray(int a[],int b[]) { int i, c[100]; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; }

cs 61C L8 Proc II., Arrays and Pointers in C 24 Patterson Fall 00 ©UCB Pointers, Arithmetic, and Dereference int x = 1, y = 2;/* x and y are integer variables */ int z[10];/* an array of 10 ints, z points to start */ int *p;/* p is a pointer to an int */ x = 21;/* assigns x the new value 21 */ z[0] = 2; z[1] = 3/* assigns 2 to the first, 3 to the next */ p = &z[0];/* p refers to the first element of z */ p = z; /* same thing; p[ i ] == z[ i ]*/ p = p+1;/* now it points to the next element, z[1] */ p++;/* now it points to the one after that, z[2] */ *p = 4;/* assigns 4 to there, z[2] == 4*/ p = 3;/* bad idea! Absolute address!!! */ p = &x;/* p points to x, *p == 21 */ z = &yillegal!!!!! array name is not a variable y: x: p: z[0] z[1]

cs 61C L8 Proc II., Arrays and Pointers in C 25 Patterson Fall 00 ©UCB Version 2: Revised Compiled Code for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c;} addi$t0,$a0,400 # beyond end of a[] addi $sp,$sp,-400# space for c addi $t3,$sp,0# ptr for c addi$v0,$t3,0# $v0 = &c[0] Loop:beq$a0,$t0,Exit lw$t1, 0($a0) # $t1=a[i] lw$t2, 0($a1) # $t2=b[i] add$t1,$t1,$t2 # $t1=a[i] + b[i] sw$t1, 0($t3)# c[i]=a[i] + b[i] addi$a0,$a0,4# $a0++ addi$a1,$a1,4# $a1++ addi$t3,$t3,4# $t3++ jLoop Exit:addi $sp,$sp, 400# pop stack jr $ra

cs 61C L8 Proc II., Arrays and Pointers in C 26 Patterson Fall 00 ©UCB Weakness of Version 2 °Legal Syntax; What’s Wrong? °Will work until call another function that uses stack °Won’t be reused instantly (e.g, add a printf ) °Stack allocated + unrestricted pointer is problem c[100] $sp low high Address stack grows

cs 61C L8 Proc II., Arrays and Pointers in C 27 Patterson Fall 00 ©UCB Version 3 to Fix Weakness of Version 2 °Solution: allocate c[] on heap int * sumarray(int a[],int b[]) { int i; int *c; c = (int *) malloc(100); for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; } CodeStatic Heap Stack c[100] °Not reused unless freed Can lead to memory leaks Java, Scheme have garbage collectors to reclaim free space

cs 61C L8 Proc II., Arrays and Pointers in C 28 Patterson Fall 00 ©UCB Version 3: Revised Compiled Code addi$t0,$a0,400 # beyond end of a[] addi $sp,$sp,-12 # space for regs sw$ra, 0($sp) # save $ra sw$a0, 4($sp) # save 1st arg. sw$a1, 8($sp) # save 2nd arg. addi$a0,$zero,400 # jalmalloc addi $t3,$v0,0# ptr for c lw$a0, 4($sp) # restore 1st arg. lw$a1, 8($sp) # restore 2nd arg. Loop:beq$a0,$t0,Exit... (loop as before on prior slide ) jLoop Exit:lw$ra, 0($sp) # restore $ra addi $sp,$sp, 12# pop stack jr $ra

cs 61C L8 Proc II., Arrays and Pointers in C 29 Patterson Fall 00 ©UCB Lifetime of storage & scope °automatic (stack allocated) typical local variables of a function created upon call, released upon return scope is the function °heap allocated created upon malloc, released upon free referenced via pointers °external / static exist for entire program

cs 61C L8 Proc II., Arrays and Pointers in C 30 Patterson Fall 00 ©UCB Version 4 : Alternative to Version 3 °Static declaration int * sumarray(int a[],int b[]) { int i; static int c[100]; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; } Code Static Heap Stack c[100] °Compiler allocates once for function, space is reused Will be changed next time sumarray invoked Why describe? used in C libraries

cs 61C L8 Proc II., Arrays and Pointers in C 31 Patterson Fall 00 ©UCB What about Structures? °Scalars passed by value °Arrays passed by reference (pointers) °Structures by value too °Can think of C passing everything by value, just that arrays are simply a notation for pointers and the pointer is passed by value

cs 61C L8 Proc II., Arrays and Pointers in C 32 Patterson Fall 00 ©UCB “And in Conclusion …” 1/2 °Procedure Conventions are a contract between caller and callee functions Honor the contract and good fortune will be your companion for all your days And if that’s not a good enough reason, honor it because it makes it possible for others to plug their MIPS code into yours without major retooling. As an added bonus, honoring the register conventions makes recursion work automagically! °Get in the habit of breaking down assembly functions into a Prologue, Body, and Epilogue before you write them.

cs 61C L8 Proc II., Arrays and Pointers in C 33 Patterson Fall 00 ©UCB “And in Conclusion …” 2/2 °C passes arguments by value °Instead of passing an entire array on stack, a pointer to the array’s first element is passed.