C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &

Slides:



Advertisements
Similar presentations
Chapter 16 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Advertisements

Programming and Data Structure
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
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:
CSS 372 Lecture 1 Course Overview: CSS 372 Web page Syllabus Lab Ettiquette Lab Report Format Review of CSS 371: Simple Computer Architecture Traps Interrupts.
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 Storage Organization
Run time vs. Compile time
Overview Projects – Project 1’s Project 2’s no later than Dec 14th Homework Problem – 14.X Pointer Variables Pass by Value Pass by Reference (or by Pointer)
RapUp Dynamic Allocation of Memory in C Last HW Exercise Review for Final Final Exam Next Thursday – Same Time / Same Place.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
CSS 372 Oct 2 nd - Lecture 2 Review of CSS 371: Simple Computer Architecture Chapter 3 – Connecting Computer Components with Buses Typical Bus Structure.
Run-time Environment and Program Organization
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays.
TCSS 372A Computer Architecture. Getting Started Get acquainted (take pictures) Review Web Page (
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
TCSS 372A Computer Architecture. Getting Started Get acquainted (take pictures) Purpose, scope, and expectations of the course Expectations & strategy.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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.
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.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Compiler Construction
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State 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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Chapter 14 Functions.
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.
CSE 220 – C Programming Pointers.
Chapter 12 Variables and Operators
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Chapter 12 Variables and Operators
Chapter 14 Functions.
Chapter 12 Variables and Operators
Chapter 9 :: Subroutines and Control Abstraction
Chapter 16 Pointers and Arrays
Chapter 14 Functions.
More C Stack Frames / Pointer variables
Chapter 16 Pointers and Arrays
Pointers and Arrays.
Understanding Program Address Space
EECE.3170 Microprocessor Systems Design I
Chapter 14 Functions.
Chapter 16 Pointers and Arrays
EECE.3170 Microprocessor Systems Design I
Chapter 16 Pointers and Arrays
Runtime Environments What is in the memory?.
Computer Organization and Assembly Language
Chapter 14 Functions.
Chapter 16 Pointers and Arrays
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining & using Pass by value Pass by reference (pointer)

Compiling C

Allocating Space for Variables Global data section –All global variables stored here (actually all static variables) –R4 points to Global Variables Run-time stack –Used for local variables (among other things) –R6 points to top of stack –R5 points to top frame on stack –New frame created for each “block” or scope (goes away when block exited) Accessing a variable: –Global: LDR R1, R4, #x –Local: LDR R2, R5, #-y Offset = distance from beginning of storage area instructions global data run-time stack Device Registers x0200 xFFFF PC R4 R6 R5 x0000 xFE00 Trap Vectors Op Sys x3000 Heap Intr Vectors x0100

Example C program #include int main() { /* Declare local variables */ int amount; /* The number of bytes to be transferred */ int rate; /* The average network transfer rate */ int time; /* The time, in seconds, for the transfer */ int hours; /* The number of hours for the transfer */ int minutes; /* The number of mins for the transfer */ int seconds; /* The number of secs for the transfer */ /* Get input: number of bytes and network transfer rate */ printf("How many bytes of data to be transferred? "); scanf("%d", &amount); printf("What is the transfer rate (in bytes/sec)? "); scanf("%d", &rate); /* Calculate total time in seconds */ time = amount / rate; /* Convert time into hours, minutes, seconds */ hours = time / 3600; /* 3600 seconds in an hour */ minutes = (time % 3600) / 60; /* 60 seconds in a minute */ seconds = ((time % 3600) % 60); /* remainder is seconds */ /* Output results */ printf("Transfer Time : %d h %d m %d s \n", hours, minutes, seconds); return 0 }

Symbol Table Like assembler, compiler needs to know information associated with identifiers – in assembler, all identifiers were labels and information is address Compiler keeps more information - Name (identifier) - Type - Location in memory - Scope Where are local variables stored? Why?

Local Variable Storage Local variables are stored in an stack frame. (also known as an activation record) Symbol table “offset” gives the distance from the base of the frame. –R5 is the frame pointer – holds address of the base of the current frame. –Because stack grows downward, base is the highest address of the frame, and variable offsets are <= 0. seconds minutes hours time rate amount R5

Context Frame (Activation Record) Format (Note: you will see that there is some inconsistency as to where the Frame begins) Function stacked stuff …….. Local Variables Caller’s Frame Pointer (R5) Caller’s R7 (contains caller’s caller’s PC) Function Return Value Function Pass Value 1 …….. Function Pass Value n R6 R5 …….. Local Variables “Previous R5” Frame

Function Call Implementation Caller pushes arguments (last to first). Caller invokes subroutine (JSR). Callee allocates return value, pushes R7 and R5. Callee allocates space for local variables. Callee executes function code. Callee stores result into return value slot. Callee pops local variables, pops R5, pops R7. Callee returns RET (or JMP R7). Caller loads return value and pops arguments. Caller resumes computation…

Function Call Implementation (with nested calls) Caller pushes arguments (last to first). Caller invokes subroutine (JSR). Callee allocates return value, pushes R7 and R5. Callee allocates space for local variables. Callee executes function code. Caller pushes arguments (last to first). Caller invokes subroutine (JSR). Caller loads return value and pops arguments. Caller resumes computation… Callee stores result into return value slot. Callee pops local variables, pops R5, pops R7. Callee returns RET (or JMP R7). Caller loads return value and pops arguments. Caller resumes computation…

Context Frame or Activation Record Format Function stacked stuff …….. Local Variables Caller’s Frame Pointer (R5) Caller’s R7 (contains ITS caller’s PC) Function Return Value Function Pass Value 1 …….. Function Pass Value n R6 R5 (Stack PTR) (Frame PTR) Calling program Called Program “PUSHED” on Stack By: …….. Local Variables

Declaring Pointer variables int *ptr; ptr is a pointer variable that points to an int type variable char *cp; cp is a pointer variable that points to a character type variable double *dp; dp is a pointer variable that points to a double type variable * is referred to as the indirection operator, or dereference operator *ptr returns the value of the variable pointed to by pointer variable ptr & is referred to as the unary or monadic operator &variable returns the address of the variable variable

Using Pointer Variables A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type. The unary or monadic operator & provides the “address of a variable”. The indirection or dereference operator * gives the “contents of an object pointed to by a pointer variable”. To declare a pointer to a variable: int *pointer; Note: ip = ip + 1 actually increments ip by 4. Why? int x=1, y=2; /* let x be at x3000 and y at 3001 */ int *ip; /* ip is an int type pointer */ ip=&x; /* ip=x3000 x= 1 y=2 */ y=*ip /* ip=x3000 x= 1 y=1 */ x=ip; /* ip=x3000 x=x3000 y=1 */ *ip=3; /* ip=x3000 x=3 y=1 */

Example Define local variables: int object; int *ptr; Now, let’s assign values to them: object = 4; ptr = &object *ptr = *ptr + 1; The last statement above is equivalent to: object = object + 1 What are the final values of object and ptr ?

Example: Parameter Passing by Value #include void Swap(int firstVal, int secondVal); int main() { int valueA = 3; int valueB = 4; Swap(valueA, valueB); return 0; } void Swap(int firstVal, int secondVal) { int tempVal; /* Holds firstVal when swapping */ tempVal = firstVal; firstVal = secondVal; secondVal = tempVal; return; } Snapshot before return from subroutine

Example: Parameter Passing by Reference #include void NewSwap(int *firstVal, int *secondVal); int main() { int valueA = 3; int valueB = 4; NewSwap(&valueA, &valueB); return 0; } void NewSwap(int *firstVal, int *secondVal) { int tempVal; /* Holds firstVal when swapping */ tempVal = *firstVal; *firstVal = *secondVal; *secondVal = tempVal; return; } Snapshots During the Exchange What happened differently using pass by reference, rather than pass by value ?

Scanf( ) function Recall reading from the keyboard in C: scanf(“%d”, &input); Why do we use &input ?

Pointer Example #include int IntDivide(int x, int y, int *quoPtr, int *remPtr); int main() { int dividend; /* The number to be divided */ int divisor; /* The number to divide by */ int quotient; /* Integer result of division */ int remainder; /* Integer remainder of division */ int error; /* Did something go wrong? */ printf("Input dividend: "); scanf("%d", &dividend); printf("Input divisor: "); scanf("%d", &divisor); error = IntDivide(dividend,divisor,&quotient,&remainder); if (!error) /* !error indicates no error */ printf("Answer: %d remainder %d\n", quotient, remainder); else printf("IntDivide failed.\n"); } int IntDivide(int x, int y, int *quoPtr, int *remPtr) { if (y != 0) { *quoPtr = x / y; /* Modify *quoPtr */ *remPtr = x % y; /* Modify *remPtr */ return 0; } else return -1; }