4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.

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 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.
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.
The University of Adelaide, School of Computer Science
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
COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I Lecturer: Hui Wu Session 2, 2005.
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:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards.
Run time vs. Compile time
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CMPE13Cyrus Bazeghi Chapter 14 Functions in C. CMPE13 2 Functions Smaller, simpler, subcomponents of programs Provide abstraction –hide low-level details.
1 C Programming Language Review and Dissection I: Overview, Static and Automatic Variables, and Program Control Flow These lecture notes created by Dr.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
Chapter 14 Functions.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
CSC 8505 Compiler Construction Runtime Environments.
Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 JSR Instruction Jumps to a location (like.
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Declarations (aka prototype) int.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
Chapter 14 Functions.
Computer Architecture & Operations I
Lecture 7 Macro Review Stack Frames
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
C Programming Language Review and Dissection
Implementing Subprograms Chapter 10
Computer structure: Procedure Calls
Procedures (Functions)
Chapter 14 Functions.
Chapter 9 :: Subroutines and Control Abstraction
Chapter 14 Functions.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
The University of Adelaide, School of Computer Science
Chapter 14 Functions.
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Procedures and Calling Conventions
Runtime Environments What is in the memory?.
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter 14 Functions.
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4

Embedded Systems4-2 Today Activation Record –Arguments and Automatics –Return value Arrays –Layout in memory –Code for accessing elements

Embedded Systems4-3 Activation Record Each time a function is activated (run), space is needed to store data – this is called an activation record –arguments – data passed to function –local variables –return value –other bookkeeping information Calling a function B from function A involves 1.Possibly placing arguments in a mutually-agreed location 2.Transfering control from function A to the function B 3.Allocating space for B’s local data 4.Executing the function B 5.Possibly placing return value in a mutually-agreed location 6.De-allocating space for B’s 7.Returning control to the function A

Embedded Systems4-4 Activation Record / Stack Frame Frame base == dynamic link 5 bytes used for –old frame base (0[FB], 1[FB]) –return address (2[FB], 3[FB], 4[FB]) enter and exitd instructions used to –modify, save and restore SP and FB –return from subroutine 0xFFFFF 0x00000 FB SP Arguments of func Automatic Variables of func Old Frame Base Caller’s Stack Frame Return Adx Space to Save Regs. More Auto. Variables More Arguments Stack Grows to Smaller Addresses 2 Bytes 3 Bytes

Embedded Systems4-5 Example Program with Function Calls const int globalD=6; int compute(int x, int y); int squared(int r); void main() { int a, b, c;// These are main’s automatic variables, and will be a = 10;// stored in main’s frame b = 16; c = compute(a,b); } int compute(int x, int y) { int z; z = squared(x); z = z + squared(y) + globalD; return(z); } int squared(int r) { return (r*r); }

Embedded Systems4-6 Step 1 - Passing Arguments Two methods for passing an argument to a function -- through a register or on the stack –registers are preferred due to speed Requirements for register passing –Argument types are prototype declared –At least one argument is of a type which can be assigned to a register –Prototype declaration is complete Arguments passed by register are allocated space in the stack for temporary use Compiler keeps track of where arguments are located, so it knows which registers or memory the code should access to find the arguments

Embedded Systems4-7 Example of Step 1 void main() { int a, b, c;// These variable are local to main, and will be a = 10;// stored in main’s frame b = 16; c = compute(a,b); } _main: enter#06H mov.w#000aH,-2[FB]; a mov.w#0010H,-4[FB]; b mov.w-4[FB],R2; b mov.w-2[FB],R1; a jsr$compute mov.wR0,-6[FB]; c exitd Move b (arg 2) into R2 Move a (arg 1) into R1

Embedded Systems4-8 Example of Step 2 void main() { int a, b, c;// These variable are local to main, and will be a = 10;// stored in main’s frame b = 16; c = compute(a,b); } _main: enter#06H mov.w#000aH,-2[FB]; a mov.w#0010H,-4[FB]; b mov.w-4[FB],R2; b mov.w-2[FB],R1; a jsr$compute mov.wR0,-6[FB]; c exitd Jump to subroutine 1.push address of next instruction (mov.w R0,-6[FB]) onto call stack 2.load PC with address $compute

Embedded Systems4-9 Call Stack before main executes jsr compute abc SP FB main’s automatic variables

Embedded Systems4-10 Call Stack arriving at compute abc SP FB main’s automatic variables Return Adx. 1. jsr pushes old PC onto stack 1.

Embedded Systems4-11 Step 3 – Allocate space on stack Save dynamic link and allocate space for local processing Enter #n instruction – Enter function and allocate n bytes –Push FB onto stack (SP -= 2). SP points to data on top of stack. –Copy SP to FB (make FB point to where SP currently points) –Subtract n from SP – automatically allocate n bytes of space on stack MALPM, pp

Embedded Systems4-12 Example of Steps 3 & 4 E1: ;## #FUNCTION compute ;## #FRAMEAUTO(z)size 2,offset -6 ;## #FRAMEAUTO(y)size 2,offset -2 ;## #FRAMEAUTO(x)size 2,offset -4 ;## #REGISTER ARG(x)size 2,REGISTER R1 ;## #REGISTER ARG(y)size 2,REGISTER R2.glb$compute $compute: enter#06H mov.wR1,-4[FB]; x x mov.wR2,-2[FB]; y y mov.w-4[FB],R1; x jsr$squared mov.wR0,-6[FB]; z mov.w-2[FB],R1; y jsr$squared add.w-6[FB],R0; z add.w_globalD,R0 mov.wR0,-6[FB]; z mov.w-6[FB],R0; z exitd Step 3. Save dynamic link, Allocate 6 bytes of space on stack for 3 local variables Step 4. Execute body of function

Embedded Systems4-13 Call Stack executing enter #6 in compute abc SP FB main’s automatic variables Return Adx enter #6 pushes old FB value onto stack, copies SP to FB, and allocates 6 more bytes (for automatic variables x, y, z) 2. Old FB 2. yxz

Embedded Systems4-14 Step 5 – Place Return Value in Proper Location Some functions return a value – integer, pointer, structure, etc. If not a struct or union, pass back in register for speed If a struct or union, pass back on stack. Pointer to space on stack is provided (and space is allocated) when function is called

Embedded Systems4-15 Example of Step 5 E1: ;## #FUNCTION compute ;## #FRAMEAUTO(z)size 2,offset -6 ;## #FRAMEAUTO(y)size 2,offset -2 ;## #FRAMEAUTO(x)size 2,offset -4 ;## #REGISTER ARG(x)size 2,REGISTER R1 ;## #REGISTER ARG(y)size 2,REGISTER R2.glb$compute $compute: enter#06H mov.wR1,-4[FB]; x x mov.wR2,-2[FB]; y y mov.w-4[FB],R1; x jsr$squared mov.wR0,-6[FB]; z mov.w-2[FB],R1; y jsr$squared add.w-6[FB],R0; z add.w_globalD,R0 mov.wR0,-6[FB]; z mov.w-6[FB],R0; z exitd Step 5. Compute returns an int, so copy result to R0

Embedded Systems4-16 Steps 6 and 7 – Deallocate Space and Return Exitd – Deallocate space on stack and exit function –Copy FB to SP (make SP point to where FB currently points) –Pop FB from Stack (SP += 2) –Return from subroutine (pop return address from stack) – step 7 MALPM, pp

Embedded Systems4-17 Call Stack as compute executes exitd abc SP FB main’s activation record Return Adx. Old FByxz 8. exitd copies FB to SP, deallocating space for x,y,z exitd then pops FB exitd pops the return address off the stack into the program counter 10. compute’s activation record

Embedded Systems4-18 Call Stack as compute continues in main abc SP FB main’s activation record

Embedded Systems4-19 Activation Record – Auto Vars and Arguments int init2(long ss, char * a, int n, char j, int lala) { int i, i2, i3, sum = 0; char mc = 'r', nc = 'j'; ;## #FRAMEAUTO( i2)size 2,offset -12 ;## #FRAMEAUTO( i3)size 2,offset -10 ;## #FRAMEAUTO( i)size 2,offset -8 ;## #FRAMEAUTO( sum)size 2,offset -6 ;## #FRAMEAUTO( a)size 2,offset -4 ;## #FRAMEAUTO( nc)size 1,offset -2 ;## #FRAMEAUTO( mc)size 1,offset -1 ;## #FRAMEARG( ss)size 4,offset 5 ;## #FRAMEARG( n)size 2,offset 9 ;## #FRAMEARG( j)size 1,offset 11 ;## #FRAMEARG( lala)size 2,offset 12 ;## #REGISTER ARG( a)size 2,REGISTER R2 ;## #ARG Size(9)Auto Size(12)Context Size(5)

Embedded Systems4-20 Putting It All Together Next we’ll see how the stack grows and shrinks –main calls compute –compute calls squared –squared ends, returning control to compute –compute ends, returning control to main

Embedded Systems4-21 Main() Function ;## #FUNCTION main ;## #FRAMEAUTO(c)size 2,offset -6 ;## #FRAMEAUTO(b)size 2,offset -4 ;## #FRAMEAUTO(a)size 2,offset -2.sectionprogram,align _main: enter#06H mov.w#000aH,-2[FB]; a mov.w#0010H,-4[FB]; b mov.w-4[FB],R2; b mov.w-2[FB],R1; a jsr$compute mov.wR0,-6[FB]; c exitd To get the assembly language output file: -In HEW, select Options->Renesas M16C standard toolchain … -Exand the C source file option under your project name -Highlight the name of the C file (or default) -On the C tab, set Category to Object -Set Output file type to [-S] Assembly language source file (*.a30) -Select OK -Build the project (Build all)

Embedded Systems4-22 Compute() Function ;## #FUNCTION compute ;## #FRAMEAUTO(z)size 2,offset -6 ;## #FRAMEAUTO(y)size 2,offset -2 ;## #FRAMEAUTO(x)size 2,offset -4 ;## #REGISTER ARG(x)size 2,REGISTER R1 ;## #REGISTER ARG(y)size 2,REGISTER R2.glb$compute $compute: enter#06H mov.wR1,-4[FB]; x x mov.wR2,-2[FB]; y y mov.w-4[FB],R1; x jsr$squared mov.wR0,-6[FB]; z mov.w-2[FB],R1; y jsr$squared add.w-6[FB],R0; z add.w_globalD,R0 mov.wR0,-6[FB]; z mov.w-6[FB],R0; z exitd

Embedded Systems4-23 Squared() Function ;## #FUNCTION squared ;## #FRAMEAUTO(r)size 2,offset -2 ;## #REGISTER ARG(r)size 2,REGISTER R1 ;## #ARG Size(0)Auto Size(2)Context Size(5).glb$squared $squared: enter#02H mov.wR1,-2[FB]; r r mov.w-2[FB],R0; r mul.w-2[FB],R0; r exitd

Embedded Systems4-24 Call Stack before main executes jsr compute abc SP FB main’s automatic variables

Embedded Systems4-25 Call Stack arriving at compute abc SP FB main’s automatic variables Return Adx. 1. jsr pushes old PC onto stack 1.

Embedded Systems4-26 Call Stack executing enter #6 in compute abc SP FB main’s automatic variables Return Adx enter #6 pushes old FB value onto stack, copies SP to FB, and allocates 6 more bytes (for x, y, z) 2. Old FB 2. yxz

Embedded Systems4-27 Call Stack as compute executes jsr squared abc SP FB Return Adx Old FB 2. yxz Return Adx jsr pushes old PC onto stack main’s activation record compute’s activation record

Embedded Systems4-28 Call Stack as squared executes enter #2 abc SP FB main’s activation record Return Adx Old FB 2. yxz compute’s activation record Return Adx. 3. Old FB 4. enter #2 pushes old FB value onto stack, copies SP to FB and allocates 2 more bytes (for r) r 4.

Embedded Systems4-29 Call Stack as squared executes exitd abc SP FB main’s activation record Return Adx. Old FByxz compute’s activation record Return Adx. Old FB 5. exitd copies FB to SP, deallocating space for r r exitd then pops FB exitd pops the return address off the stack into the program counter 7.

Embedded Systems4-30 Call Stack as compute executes exitd abc SP FB main’s activation record Return Adx. Old FByxz 8. exitd copies FB to SP, deallocating space for x,y,z exitd then pops FB exitd pops the return address off the stack into the program counter 10. compute’s activation record

Embedded Systems4-31 Call Stack as compute continues in main abc SP FB main’s activation record

Embedded Systems4-32 Notes for Actual Implementation by Compiler Desired Format: –offset[FB] variable_or_item_name Order in which local variables are located in activation record is cryptic Instead assume items are added the following order (with decreasing address) –One byte items Automatic variables, in order of declaration Arguments which have been passed by register, in order of declaration –Why? For local temporary storage –Two byte items Automatic variables, in order of declaration Arguments which have been passed by register, in order of declaration –Why? For local temporary storage –etc. Don’t forget the old frame pointer and return address

Embedded Systems4-33 Example of Passing Mixed Arguments ;## # FUNCTION compute2 ;## # FRAME AUTO ( z)size 2,offset -2 ;## # FRAME AUTO ( x)size 2,offset -2 ;## # FRAME ARG ( f) size 4, offset 5 ;## # FRAME ARG ( y) size 2, offset 9 ;## # REGISTER ARG ( x) size 2, REGISTER R2 ;## #ARG Size(6)Auto Size(2)Context Size(5) $compute2: enter#02H mov.w R2,-2[FB]; x x mov.w -2[FB],R1; x jsr $squared mov.w R0,-2[FB]; z mov.w 9[FB],R1; y jsr $squared add.w -2[FB],R0; z lde.w _globalD,R1 add.w R1,R0 mov.w R0,-2[FB]; z mov.w -2[FB],R0; z exitd int compute2(float f, int x, int y) { … } Load argument y from stack

Embedded Systems4-34 1D Arrays ;## # C_SRC : i = buff2[0] + buff2[n]; ;on entry, n is in A0, and local var i is located at -8[FB] mov.w9[FB],A0; copy n (element #) into A0 shl.w#01H,A0; multiply n by elem. size (2 bytes/elem.) mov.w_buff2[A0],R0; load R0 from _buff2+(n*2) mov.w_buff2,-8[FB]; move buff2[0] into i (on stack at -8[FB] add.wR0,-8[FB]; add R0 (=buff2[n]) to i (=buff2[0]), store sum in i

Embedded Systems4-35 2D Arrays [0][0] [1][0] [0][1] [1][1] [0][2] [1][2] Columns Rows ColumnRow C arrays are stored in a row-major form (a row at a time) ;## # C_SRC : i2 = buff3[n][j]; ;Stack: arg n at 9[FB], j at 11[FB]. Local var i2 at -10[FB] ; compute adx. offset due to column j mov.b11[FB],A0; move j from stack into A0 shl.w#01H,A0; multiply j by el. size (2 bytes/int) mov.wA0,R0; save ; compute adx. offset due to row n mov.w9[FB],A0; move n into A0 mul.w#0006H,A0; multiply by row size (3 els * 2 bytes/int) add.wR0,A0; add row and col. offsets to form total offset mov.w_buff3[A0],-10[FB]; access array, move element to i2