COMP3221 lec15-function-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 15 : Functions in C/ Assembly - I

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
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
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
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
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.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
cs61c L5 Decisions 9/13/00 1 CS61C - Machine Structures Lecture 5 - Decisions in C/Assembly Language September 13, 2000 David Patterson
COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I Lecturer: Hui Wu Session 2, 2005.
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.
COMP3221 lec14-decision-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 14: Making Decisions in C/Assembly Language – II.
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);...
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
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
CS61C L11 Introduction to MIPS : Procedures I (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
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/
FUNCTION – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
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 (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
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.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
Writing Functions in Assembly
Rocky K. C. Chang Version 0.1, 25 September 2017
CSCI206 - Computer Organization & Programming
April 2006 Saeid Nooshabadi
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
Writing Functions in Assembly
April 2006 Saeid Nooshabadi
CSCI206 - Computer Organization & Programming
Krste Asanović and Randy H. Katz
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Procedures and addressing
April 2006 Saeid Nooshabadi
Computer Architecture
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

COMP3221 lec15-function-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 15 : Functions in C/ Assembly - I August, 2003 Saeid Nooshabadi

COMP3221 lec15-function-I.2 Saeid Nooshabadi Overview °C functions °Bookkeeping for function call/return °Instruction support for functions °Nested function calls °C memory allocation: static, heap, stack °Conclusion

COMP3221 lec15-function-I.3 Saeid Nooshabadi Review °HLL decisions (if, case) and loops (while, for) use same assembly instructions Flag Setting Instructions: cmp, cmn, tst, teq in ARM Data Processing Instructions with Flag setting Feature: adds, subs, ands, in ARM Conditional branches: beq, bne, bgt, blt, etc in ARM Conditional Instructions: addeq, ldreq,etc in ARM Unconditional branches: b, bal, and mv pc, Rn in ARM Switch/Case: chained if-else or jump table + ldr pc, [ ] ldr pc, [ ] is VERY POWERFUL!

COMP3221 lec15-function-I.4 Saeid Nooshabadi Review: Branches: PC-relative addressing °Recall register r15 in the machine also called PC; °points to the currently executing instruction °Most instruction add 4 to it. (pc increments by 4 after execution of most instructions) °Branch changes it to a specific value °Branch adds to it 24-bit signed value (contained in the instruction) Shifted left by 2 bits °Labels => addresses memory 0: FFF... registers r14 r0 r15 = pc beq address b address –32MB +32MB 24 bits

COMP3221 lec15-function-I.5 Saeid Nooshabadi C functions main(void) { int i,j,k,m; i = mult(j,k);... ; m = mult(i,i);... } int mult (int mcand, int mlier) { int product = 0; while (mlier > 0) { product = product + mcand; mlier = mlier -1; } return product; } What information must compiler/program keep track of?

COMP3221 lec15-function-I.6 Saeid Nooshabadi Basics of Function Call...(use regs) set up args jump to function access args... compute result......(use regs) set up return value jump back to caller Caller Callee access result...(use regs)

COMP3221 lec15-function-I.7 Saeid Nooshabadi 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 lec15-function-I.8 Saeid Nooshabadi 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 lec15-function-I.9 Saeid Nooshabadi Instruction Support for Function Call?... sum(a,b);... /* a,b:v1,v2 */ } int sum(int x, int y) { return x+y; } address 1000 mov a1,v1 ; x = a 1004 mov a2, v2 ; y = b 1008 mov lr,#1016 ; lr = b sum ; jump to sum sum: add a1,a1,a mov pc, lr C ARMARM Why mov pc, lr vs. b 1016 to return? ; b 1016

COMP3221 lec15-function-I.10 Saeid Nooshabadi b vs mov pc, lr : why jump register? °Consider a function foo that is called from several different places in your program each call is performed by a b instruction -b foo but the return jump goes to many places main: b bar... b foo... bar:... b foo... foo:... b foo... mov pc, lr

COMP3221 lec15-function-I.11 Saeid Nooshabadi A Level of Indirection °Solves many problems in CS! °How do you make a jump instruction behave differently each time it is executed? indirection mov pc, lr returns control to last caller -recorded in a registers °How do you make an instruction fetch a different element of an array each time though a loop? indirection update index address register of ldr, and str

COMP3221 lec15-function-I.12 Saeid Nooshabadi Accessing array elements => indirection int sumarray(int arr[]) { int i, sum; for(i=0;i<100;i=i+1) sum = sum + arr[i]; } movv1, #0; clear v0 adda2,a1,#400 ; beyond end of arr[] Loop:cmpa1,a2 bgeExit ldra3, [a1], #4 ; a3=arr[i], a1++ addv1,v1,a3 ; v1= v1+ arr[i] bLoop Exit:mov lr, pc

COMP3221 lec15-function-I.13 Saeid Nooshabadi Instruction Support for Functions? °Single instruction to branch and save return address: branch and link ( bl ): °Before: 1008 mov lr, #1016 ;lr = b sum ;goto sum °After: 1012 bl sum ; lr = 1016,goto sum °Why bl ? Make the common case fast and elegance

COMP3221 lec15-function-I.14 Saeid Nooshabadi Nested Procedures (#1/2)...sumSquare(a,b)... int sumSquare(int x, int y) { return mult (x,x)+ y; } °Need to save sumSquare return address saved in lr by bl sumSquare instruction, before call to mult Otherwise bl mult overwrites lr °One word per procedure in memory ? e.g., str lr, [sp,sumSquareRA]; sp = r13 °Recursive procedures could overwrite saved area => need safe area per function invocation => stack

COMP3221 lec15-function-I.15 Saeid Nooshabadi Nested Procedures (#2/2) °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 Heap: Variables declared dynamically (such as counters in for loops, or by function malloc() ) Stack: Space to be used by procedure during execution; this is where we can save register values

COMP3221 lec15-function-I.16 Saeid Nooshabadi “What’s This Stuff Good For?” Breathing Observation Bubble: BOB pipes air from a tank under the handlebars into an acrylic dome, replacing a diver's face mask and breathing apparatus. Wireless technology lets riders talk to other BOBsters darting through the water nearby, as well as to armchair divers above in a boat or back on shore. Saving energy from not having to kick, divers can stay submerged almost an hour with the BOB. Like most modern scuba gear, the BOB features a computer that tells riders when to come up and calculates decompression times for a safe return to the surface. One Digital Day, What do applications (“apps”) like these mean for reliability requirements of our technology?

COMP3221 lec15-function-I.17 Saeid Nooshabadi 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 Static base sb

COMP3221 lec15-function-I.18 Saeid Nooshabadi Stack Operations °PUSH v1 °POP v1 sub sp,sp, #4 ; space on stack str v1, [sp,#0] ; save x ldr v1, [sp,#0] ; restore x add sp, sp,#4 ; => stack space SP v What does sp point to? Decreasing address

COMP3221 lec15-function-I.19 Saeid Nooshabadi Stack Operations (Better Way) °PUSH v1 °POP v1 str v1, [sp,#-4]! ; space on stack ; and save x ldr v1, [sp], #4 ; restore x ; and reclaim stack space SP v What does sp point to? Decreasing address

COMP3221 lec15-function-I.20 Saeid Nooshabadi sp on entry  Register Save Area Local Variables sp during  Arguments for Callees 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 lec15-function-I.21 Saeid Nooshabadi “And in Conclusion …” (#1/2) °ARM Assembly language instructions Unconditional branches: b, mov pc, rx, bl °Operands Registers (word = 32 bits); a1 - a3, v1 – v8, ls, sb, fp, sp, lr

COMP3221 lec15-function-I.22 Saeid Nooshabadi “And in Conclusion …” (#2/2) °Functions, procedures one of main ways to give a program structure, reuse code °mov pc, Rn required instruction; most add bl (or equivalent) to make common case fast °Registers make programs fast, but make procedure/function call/return tricky °ARM SW convention divides registers for passing arguments, return address, return value, stack pointer