Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

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
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
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)
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)
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.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
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.
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.
CS61C L11 Introduction to MIPS: Procedures I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
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.
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.
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
COMP3221 lec18-function-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 17 : Functions in C/ Assembly - III
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);...
ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly
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.
Chapter 8 :: Subroutines and Control Abstraction
Topic 8: Data Transfer Instructions CSE 30: Computer Organization and Systems Programming Winter 2010 Prof. Ryan Kastner Dept. of Computer Science and.
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.
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.
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.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
The Stack. ARMSim memory space: 0x Unused 0x x11400 Stack 0x x09400 Heap 0x?????-0x01400 Data 0x x????? Text 0x x01000.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
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
Writing Functions in Assembly
Rocky K. C. Chang Version 0.1, 25 September 2017
Computer Science 210 Computer Organization
The Stack.
April 2006 Saeid Nooshabadi
Procedures (Functions)
Writing Functions in Assembly
April 2006 Saeid Nooshabadi
Chapter 9 :: Subroutines and Control Abstraction
The University of Adelaide, School of Computer Science
CS61C : Machine Structures Lecture 2. 2
ECE 3430 – Intro to Microcomputer Systems
Program and memory layout
Procedures and addressing
Presentation transcript:

Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University of California, San Diego

C functions main() { int a,b,c;... c = sum(a,b);/* a,b,c:r0,r1,r2*/... } /* really dumb sum function */ int sum(int x, int y) { return x+y; } What information must compiler/programmer keep track of? What instructions can accomplish this?

Function Call Bookkeeping  Registers play a major role in keeping track of information for function calls  Register conventions:  Return address lr  Arguments r0, r1, r2, r3  Return value r0, r1, r2, r3  Local variables r4, r5, …, r12  The stack is also used; more later

Register Usage r8 r9/sb r10/sl r11 r12 r13/sp r14/lr r15/pc r0 r1 r2 r3 r4 r5 r6 r7 Register variables Must be preserved Arguments into function Result(s) from function otherwise corruptible ( Additional parameters passed on stack) Scratch register (corruptible) Stack Pointer Link Register Program Counter The compiler has a set of rules known as a Procedure Call Standard that determine how to pass parameters to a function (see AAPCS) CPSR flags may be corrupted by function call. Assembler code which links with compiled code must follow the AAPCS at external interfaces The AAPCS is part of the new ABI for the ARM Architecture Register - Stack base - Stack limit if software stack checking selected - R14 can be used as a temporary once value stacked - SP should always be 8-byte (2 word) aligned

Instruction Support for Functions... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } address C ARMARM In ARM, all instructions are stored in memory just like data. So here we show the addresses of where the programs are stored.

Instruction Support for Functions... sum(a,b);... /* a,b:r4,r5 */ } int sum(int x, int y) { return x+y; } address 1000 MOV r0,r4 ; x = a 1004 MOV r1,r5; y = b 1008 MOV lr,1016 ; lr = B sum ; branch to sum sum: ADD r0,r0,r BX lr ; MOV pc,lri.e., return C ARMARM

Instruction Support for Functions... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } 2000 sum: ADD r0,r0,r BX lr; new instruction C ARMARM  Question: Why use BX here? Why not simply use B ?  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.

Instruction Support for Functions  Single instruction to jump and save return address: jump and link ( BL )  Before: 1008 MOV lr, 1016 ; lr= B sum ; goto sum  After: 1008 BL sum # lr=1012, goto sum  Why have a BL ? 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 BL.

Instruction Support for Functions  Syntax for BL (branch and link) is same as for B (branc): BLlabel  BL functionality:  Step 1 (link): Save address of next instruction into lr (Why next instruction? Why not current one?)  Step 2 (branch): Branch to the given label

Instruction Support for Functions  Syntax for BX (branch and exchange): BX register  Instead of providing a label to jump to, the BX instruction provides a register which contains an address to jump to  Only useful if we know exact address to jump  Very useful for function calls:  BL stores return address in register (lr )  BX lr jumps back to that address

Nested Procedures 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 lr 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.

Nested Procedures  In general, may need to save some other info in addition to lr.  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 values

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

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.  So, how do we compile this? int sumSquare(int x, int y) { return mult (x,x)+ y; }

Using the Stack  Hand-compile sumSquare: ADD sp,sp,#-8 ; space on stack STR lr, [sp,#4]; save ret addr STR r1, [sp]; save y MOV r1, r0 ; mult(x,x) BL mult ; call mult LDR r1, [sp] ; restore y ADD r0,r0,r1 ; mult()+y LDR lr, [sp, #4] ; get ret addr ADD sp,sp,#8 ; restore stack BX lr mult:... int sumSquare(int x, int y) { return mult (x,x)+ y; } “push” “pop”

Steps for Making a Procedure Call 1) Save necessary values onto stack 2) Assign argument(s), if any 3) BL call 4) Restore values from stack

Rules for Procedures  Called with a BL instruction, returns with a BX lr (or MOV pc, lr )  Accepts up to 4 arguments in r0, r1, r2 and r3  Return value is always in r0 (and if necessary in r1, r2, r3)  Must follow register conventions (even in functions that only you will call)! So what are they?

ARM Registers RegisterSynonymRole in Procedure Call Standard r0-r1a1-a2Argument/Result/Scratch Register r2-r3a3-a4Argument/Scratch Register r4-r8v1-v5Variable Register r9v6/sb/trPlatform Register r10-r11v7-v8Variable Register r12ipIntra-Procedure Call Scratch Register r13spStack Pointer r14lrLink Register r15pcProgram Counter

Register Conventions  CalleR: the calling function  CalleE: the function being called  When callee returns from executing, the caller needs to know which registers may have changed and which are guaranteed to be unchanged.  Register Conventions: A set of generally accepted rules as to which registers will be unchanged after a procedure call ( BL ) and which may be changed.

Saved Register Conventions  r4-r11 (v1-v8): Restore if you change. Very important. If the callee changes these in any way, it must restore the original values before returning.  sp: Restore if you change. The stack pointer must point to the same place before and after the BL call, or else the caller won’t’be able to restore values from the stack.

Volatile Register Conventions  lr: Can Change. The BX call itself will change this register. Caller needs to save on stack if nested call.  r0-r3 (a1-a4): Can change. These are volatile argument registers. Caller needs to save if they’ll need them after the call. E.g., r0 will change if there is a return value  r12 (ip) may be used by a linker as a scratch register between a routine and any subroutine it calls. It can also be used within a routine to hold intermediate values between subroutine calls.

Register Conventions  What do these conventions mean?  If function R calls function E, then function R must save any temporary registers that it may be using onto the stack before making a BL call.  Function E must save any saved registers it intends to use before garbling up their values  Remember: Caller/callee need to save only volatile/saved registers they are using, not all registers.

Basic Structure of a Function entry_label: ADD sp,sp, -framesize STR lr, [sp, #framesize-4] ;save lr save other regs if need be... restore other regs if need be LDR lr, [$sp, framesize-4] ;restore lr ADD sp,sp, #framesize BX lr Epilogue Prologue Body (call other functions…) ra memory

Example main() { int i,j,k,m; /* i-m:v0-v3 */... i = mult(j,k);... m = mult(i,i);... } int mult (int mcand, int mlier){ int product; product = 0; while (mlier > 0) { product += mcand; mlier -= 1; } return product;}

Example __start: MOV a1,v1; arg1 = j MOV a2,v2; arg2 = k BL mult; call mult MOV v0,r0; i = mult()... MOV a1,v0; arg1 = i MOV a2,v0; arg2 = i BL mult; call mult MOV v3,r0; m = mult()... done main() { int i,j,k,m; /* i-m:v0-v3 */... i = mult(j,k);... m = mult(i,i);... }

Example  Notes:  main function ends with done, not BX lr, so there’s no need to save lr onto stack  All variables used in main function are saved registers, so there’s no need to save these onto stack

Example mult: MOV a3,#0; prod=0 Loop: CMP a2, #0; mlier == 0? BLE Fin; if mlier <= 0 goto Fin ADD a3,a3,a1; product += mcand ADD a2,a2,#-1; mlier -= 1 B Loop; goto Loop Fin: MOV a1,a3 ; setup return value BX lr; return int mult (int mcand, int mlier){ int product = 0; while (mlier > 0) { product += mcand; mlier -= 1; } return product;}

Example  Notes:  No BL calls are made from mult and we don’t use any saved registers, so we don’t need to save anything onto stack  Temp registers are used for intermediate calculations (could have used saved registers, but would have to save the caller’s on the stack.)  a2 is modified directly (instead of copying into a temp register) since we are free to change it  Result is put into a1 ( r0 ) before returning

Conclusion  Functions are called with BL, and return with BX lr.  The stack is your friend: Use it to save anything you need. Just be sure to leave it the way you found it.  Register Conventions: Each register has a purpose and limits to its usage. Learn these and follow them, even if you’re writing all the code yourself.

Conclusion  Instructions so far:  Previously: ADD, SUB, MUL, MULA, [U|S]MULL, [U|S]MLAL, RSB AND, ORR, EOR, BIC MOV, MVN LSL, LSR, ASR, ROR CMP, B{EQ,NE,LT,LE,GT,GE} LDR, LDR, STR, LDRB, STRB, LDRH, STRH  New: BL, BX  Registers we know so far  All of them!