COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II

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 lec23-decode.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 23: Instruction Representation; Assembly and Decoding.
©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.
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.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
COMP3221 lec9-logical-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 9: C/Assembler Logical and Shift - I
COMP3221 lec-12-mem-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 12: Memory Access - II
 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.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
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.
Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 11: Memory Access - I
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.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
COMP3221 lec18-function-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 17 : Functions in C/ Assembly - III
COMP3221 lec15-function-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 6 : Functions in C/ Assembly - 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
Procedure Conventions & The Stack Jen-Chang Liu, Spring 2006 Adapted from
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Lecture 4: MIPS Instruction Set
1 CS61C L6 Machine Rep CENG 311 Procedure Conventions & The Stack.
4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
Chapter 14 Functions.
Computer Architecture & Operations I
Writing Functions in Assembly
Rocky K. C. Chang Version 0.1, 25 September 2017
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
© Craig Zilles (adapted from slides by Howard Huang)
Procedures 101: There and Back Again
April 2006 Saeid Nooshabadi
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
Writing Functions in Assembly
April 2006 Saeid Nooshabadi
Chapter 9 :: Subroutines and Control Abstraction
Prof. Hakim Weatherspoon
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Lecture 6: Assembly Programs
Procedures and Calling Conventions
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
© Craig Zilles (adapted from slides by Howard Huang)
Topic 2b ISA Support for High-Level Languages
Implementing Functions: Overview
Presentation transcript:

COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II September, 2003 Saeid Nooshabadi

COMP3221 lec16-function-II.2 Saeid Nooshabadi Overview °Resolving Registers Conflicts °Caller /Callee Responsibilities °Frame/Stack pointer °Conclusion

COMP3221 lec16-function-II.3 Saeid Nooshabadi Review: Basics of Function Call...(use regs) set up args jump to function access result...(use regs) access args... compute result......(use regs) set up return value jump back to caller Caller Callee

COMP3221 lec16-function-II.4 Saeid Nooshabadi Review: Function Call Bookkeeping Registers for functions  lr = r14  a1, a2, a3, a4  a1  v1, v2,v3,v4,v5,v6,v7 °Procedure address °Return address °Arguments °Return value °Local variables °Registers (conflicts) =>ARM Procedure Call Standards (APCS) conventions for use of registers simplify bookkeeping

COMP3221 lec16-function-II.5 Saeid Nooshabadi Review: Instruction Support for Functions? °Single instruction to branch and save return address: branch and link ( bl ): 1012 bl sum ; lr = 1016,goto sum

COMP3221 lec16-function-II.6 Saeid Nooshabadi Review: APCS Register Convention: Summary register namesoftware nameuse and linkage r0 – r3a1 – a4 first 4 integer args scratch registers integer function results r4 – r11v1- v8local variables r9sbstatic variable base r10slstack limit r11fpframe pointer r12ipintra procedure-call scratch pointer r13spstack pointer r14lrreturn address r15pcprogram counter Red are SW conventions for compilation, blue are HW ARM Procedure Call Standard (APCS)

COMP3221 lec16-function-II.7 Saeid Nooshabadi Review: Nested Procedures °A caller function is itself called from another function. °We need to store lr for the caller before it can call another function. °In general, may need to save some other info in addition to lr. °But; Where do we save these info?

COMP3221 lec16-function-II.8 Saeid Nooshabadi Review: C memory allocation map 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 Static base sb

COMP3221 lec16-function-II.9 Saeid Nooshabadi sp on entry  Register Save Area Local Variables sp during  Arguments for Callees Review: Typical Structure of a Stack Frame °compiler calculates total frame size (F) °at start of call, subtract F from SP °at end, add F to SP °where are args from caller? °where are args for callee Caller Frame Callee Frame

COMP3221 lec16-function-II.10 Saeid Nooshabadi Basic Structure of a Function entry_label: sub sp,sp, #fsize ; create space on stack str lr,[sp, #fsize-4]; save lr ; save other regs... ;restore other regs ldr lr, [sp,#fsize-4];restore lr add sp, sp, #fsize ;reclaim space on stack mov pc, lr Epilogue Prologue Body lr

COMP3221 lec16-function-II.11 Saeid Nooshabadi Compiling nested C func into ARM int sumSquare(int x, int y) { return mult (x,x)+ y; } sumSquare: sub sp,sp,#8 ; space on stack str lr,[sp,#4] ; save ret addr str a2,[sp,#0] ; save y mov a2,a1 ; mult(x,x) bl mult ; call mult ldr a2,[sp,#0] ; restore y add a1,a1,a2 ; mult()+y ldr lr,[sp,#4] ; get ret addr add sp,sp,#8 ; => stack space mov pc, lr C Epilogue Prologue Body ARMARM

COMP3221 lec16-function-II.12 Saeid Nooshabadi Compiling nested C func into ARM (Better way) int sumSquare(int x, int y) { return mult (x,x)+ y; } sumSquare: str lr,[sp,#-4]!; save ret addr str a2,[sp,#-4]!; save y mov a2,a1 ; mult(x,x) bl mult ; call mult ldr a2,[sp,#4]! ; restore y add a1,a1,a2 ; mult()+y ldr lr,[sp,#4]! ; get ret addr ; => stack space mov pc, lr C Epilogue Prologue Body ARMARM

COMP3221 lec16-function-II.13 Saeid Nooshabadi Function Call Bookkeeping: thus far °Procedure addressx °Return address xlr °Arguments xa1 – a4 °Return values xa1 – a4 °Local variables xv1 – v8 °Registers what if they are reused? what if there aren’t enough?

COMP3221 lec16-function-II.14 Saeid Nooshabadi Readings for Week #6 °Steve Furber: ARM System On-Chip; 2nd Ed, Addison- Wesley, 2000, ISBN: Chapters 6 °Experiment 3 Documentation

COMP3221 lec16-function-II.15 Saeid Nooshabadi Exceeding limits of registers °Recall: assembly language has fixed number of operands, HLL doesn’t °Local variables: v1,..., v8 What if more than 8 words of local variables? °Arguments; a1,..., a4 What if more than 4 words of arguments? °Place extra variables and extra arguments onto stack ( sp ) °Use scratch registers and data transfers to access these variables

COMP3221 lec16-function-II.16 Saeid Nooshabadi 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 15 registers => must cooperate

COMP3221 lec16-function-II.17 Saeid Nooshabadi Register Conflicts: 2 options (A calls B) 1) Called procedure/callee (B) leaves registers the way it found them (except lr ); 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)” °Is either optimal?

COMP3221 lec16-function-II.18 Saeid Nooshabadi ARM Solution to Register Conflicts °Divide registers into groups Local variables / Callee Saved registers ( v1 – v8 ) Scratch registers / Argument / Caller Save registers ( a1 – a3 ) Some caller saved (if wants) and some callee saved (if used) °Caller (A) save/restore scratch / argument ( a1 – a4 ) if needs them after the call; also lr  callee can use ( a1 – a4 ) and lr °Callee (B) must save/restore local variables / callee saved registers ( v1 – v8 ) if it uses them  caller can leave them unsaved Procedure that doesn’t call another tries to use only scratch / argument registers ( a1 – a4 )

COMP3221 lec16-function-II.19 Saeid Nooshabadi Register Conventions (#1/5) °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.

COMP3221 lec16-function-II.20 Saeid Nooshabadi Register Conventions (#2/5) °Three views of registers a1 – a4 °a1 – a4 : Change. These are expected to contain new return values. Or °a1 – a4 : Change. These are volatile argument registers. Or °a1 – a4 : Change. They’re called scratch: any procedure may change them at any time.

COMP3221 lec16-function-II.21 Saeid Nooshabadi Register Conventions (#3/5) °v1 – v8 : No Change. Very important, that’s why they’re called callee saved registers / local variable. If the callee changes these in any way, it must restore the original values before returning. °sp : No 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. Grows downward, sp points to last full location °lr : Change. The bl call itself will change this register. °ip : Change. In most variants of APCS ip is an scratch register.

COMP3221 lec16-function-II.22 Saeid Nooshabadi Register Conventions (#4/5) °What do these conventions mean? If function A calls function B, then function A must save any scratch registers a1 – a4 that it may be using onto the stack before making a bl call. Remember: Caller needs to save only registers it is using, not all scratch registers. It also needs to store lr if A is in turn is called by another function.

COMP3221 lec16-function-II.23 Saeid Nooshabadi Register Conventions (#5/5) °Note that, even though the callee may not return with different values in the callee saved registers v1 – v8, it can use them by : save v1 – v8 on the stack use these eight registers restore v1 – v8 from the stack °The difference is that, with the scratch registers a1 – a4, the callee doesn’t need to save them onto the stack.

COMP3221 lec16-function-II.24 Saeid Nooshabadi sp on entry  Register Save Area Local Variables sp during  Arguments for Callees Recall: Typical Structure of a Stack Frame °compiler calculates total frame size (F) for the caller °at start of procedure, subtract F from SP °at end, add F to SP °where are args from caller? °where are the args for callee? Caller Frame Callee Frame

COMP3221 lec16-function-II.25 Saeid Nooshabadi Callees’ & Callers’ Rights (Summary) °Callees’ Right Right to use a1 – a4 registers freely Right to assume args are passed correctly in a1 – a4 °Callers’ Rights Right to use v1 – v8 registers without fear of being overwritten by Callee Right to assume return values will be returned correctly in a1 – a4 Keep this slide in mind for exam

COMP3221 lec16-function-II.26 Saeid Nooshabadi Callee’s Responsibilities ( Summary) 1.If using v1 – v8 or big local structs, slide sp down to reserve memory: e.g. sub sp, sp, #32 2.If using v1 – v8, save before using: e.g. str v1, [sp, #28] 3.Receive args in a1 – a4, additional args on stack 4.Run the procedure body 5.If not void, put return values in a1 – a4 6.If applicable, undo steps 2-1 e.g. ldr v1, [sp,#28] add sp, sp, #32 7.mov pc, lr Keep this slide in mind for exam

COMP3221 lec16-function-II.27 Saeid Nooshabadi Caller’s Responsibilities ( Summary) 1.Slide sp down to reserve memory: e.g. sub sp, sp, #28 2.Save lr on stack before bl to callee clobbers it: e.g. str lr, [sp, #24] 3.If you’ll still need their values after the function call, save a1 – a4 on stack or copy to v1 – v8 registers. Callee can overwrite “a” registers, but not “v” registers. e.g. str a1, [sp, #20] 4.Put first 4 words of args in a1 – a4, at most 1 arg per word, additional args go on stack: “arg 5” is [sp, #0] 5.bl to the desired function 6.Receive return values in a1 – a4 7.Undo steps 3-1: e.g. ldr a1, [sp, #20], ldr lr, [sp, #24], add sp, sp, #28 Keep this slide in mind for exam

COMP3221 lec16-function-II.28 Saeid Nooshabadi Compile using pencil and paper Example 1 (#1/2) int Doh(int i, int j, int k, int l){ return i+j+l; } Doh: _______________ add a1, _________________ _________________

COMP3221 lec16-function-II.29 Saeid Nooshabadi Compile using pencil and paper Example 1 (#2/2) int Doh(int i, int j, int k, int l){ return i+j+l; } Doh: _______________ add a1, _______ _______________ add a1, a2, a1 a4, a1 mov pc lr “a” Regs Safe For Callee

COMP3221 lec16-function-II.30 Saeid Nooshabadi Compile using pencil and paper Example 2 (#1/2) int Doh(int i, int j, int k, int m, char c, int n){ return i+j+n; } Doh: _______________ add a1, _______ _______________ _______________

COMP3221 lec16-function-II.31 Saeid Nooshabadi Compile using pencil and paper Example 2 (#2/2) int Doh(int i, int j, int k, int m, char c, int n){ return i+j+n; } Doh: _______________ add a1, ______ ldr ip,[sp,#4] add a1, a1, ip a1,a2 mov pc lr 6th argument “a” & ip Regs Safe For Callee

COMP3221 lec16-function-II.32 Saeid Nooshabadi Hard Compilation Problem (#1/2) int Doh(int i, int j, int k, int m, char c, int n){return mult(i,j)+n;} int mult(int m, int n);/* returns m  n */ Doh: ______________ ______________ bl ___________ add ___________ _______________

COMP3221 lec16-function-II.33 Saeid Nooshabadi Slow-motion Replay (#1/2) int Doh(int i, int j, int k, int m, char c, int n){ return sum(i,j)+n;} int mult(int m, int n);/* returns m  n */ Doh: ______________ ______________ bl ___________ add ___________ _______________ str lr, [sp] sub sp, sp,#4 ldr lr, [sp] add sp, sp, #4 mov pc, lr mult ldr,[sp,#8] v1 2.Saving lr => must move sp 1.Calling Sum => save lr, a Regs 3.Need n after funccall => v1 a1, a1, v1

COMP3221 lec16-function-II.34 Saeid Nooshabadi Stack Memory Allocation Revisited °Caller frame Save caller-saved regs Pass arguments (4 regs) bl °Callee frame set word of frame fp = Caller’s sp - 4 (fixed point) Save registers on stack and update sp as needed Accessing the local variables is always with reference to fp Return saved register from stack using fp Callee Saved Registers fp low high Address stack grows Local Variables sp... Argument 5 Argument 6 place for a1-a4 °GCC uses frame pointer, ARM compilers by default don’t (allocate extra temporary register ( ip ), less bookkeeping on procedure call)

COMP3221 lec16-function-II.35 Saeid Nooshabadi “And in Conclusion …’’ °ARM 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 °Optional Frame pointer fp reduces bookkeeping on procedure call