Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.

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

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
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:
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
RapUp Dynamic Allocation of Memory in C Last HW Exercise Review for Final Final Exam Next Thursday – Same Time / Same Place.
Semantics of Calls and Returns
CSS 372 Oct 2 nd - Lecture 2 Review of CSS 371: Simple Computer Architecture Chapter 3 – Connecting Computer Components with Buses Typical Bus Structure.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
TCSS 372A Computer Architecture. Getting Started Get acquainted (take pictures) Review Web Page (
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
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.
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Function Smaller, simpler, subcomponent.
CMPE13Cyrus Bazeghi Chapter 14 Functions in C. CMPE13 2 Functions Smaller, simpler, subcomponents of programs Provide abstraction –hide low-level details.
Functions & Activation Records Recursion
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.
Compiler Construction
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.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
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.
4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
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.
Run-Time Environments Chapter 7
Chapter 12 Variables and Operators
Implementing Subprograms
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Chapter 12 Variables and Operators
Introduction to Compilers Tim Teitelbaum
Chapter 14 Functions.
In this lecture Global variables Local variables System stack
Chapter 12 Variables and Operators
Chapter 9 :: Subroutines and Control Abstraction
Chapter 14 Functions.
More C Stack Frames / Pointer variables
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Stack Frame Linkage.
The University of Adelaide, School of Computer Science
Understanding Program Address Space
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
Runtime Environments What is in the memory?.
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.
Topic 2b ISA Support for High-Level Languages
Midterm 2 Review Chapters 4-16 LC-3
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation

Example 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 : %dh %dm %ds\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 Name Type Offset Scope amount hours minutes rate seconds time int main

Local Variable Storage Local variables are stored in an activation record, also known as a stack frame. 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. –A new frame is pushed on the run-time stack each time a block is entered. –Because stack grows downward, base is the highest address of the frame, and variable offsets are <= 0. seconds minutes hours time rate amount R5

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

Activation Record Bookkeeping Return value –space for value returned by function –allocated even if function does not return a value Return address –save pointer to next instruction in calling function –convenient location to store R7 in case another function (JSR) is called Dynamic link –caller’s frame pointer –used to pop this activation record from stack

Activation Record Format Function stacked stuff …….. Local Variables Caller’s Frame Pointer (R5) Caller’s Return PC (R7) Function Return Value Function Pass Value n …….. Function Pass Value 1 R6 R5

Example Program with Function Calls int main () { int a = 23; int b = 14;... b = Watt(a); /* main calls both */ b = Volta(a,b);... } int Watt(int c); { int w = 5;... w = Volta(w,10); /* Watt calls Volta */... return w; } int Volta(int q, int r) { int k = 3; int m = 6;... /* Volta calls no one */ return k+m; }

Memory Snapshots

Storage

Activation Records

Summary of LC-3 Function Call Implementation 1.Caller pushes arguments (last to first). 2.Caller invokes subroutine (JSR). 3.Callee allocates return value, pushes R7 and R5. 4.Callee allocates space for local variables. 5.Callee executes function code. 6.Callee stores result into return value slot. 7.Callee pops local vars, pops R5, pops R7. 8.Callee returns (RET or JMP R7). 9.Caller loads return value and pops arguments. 10.Caller resumes computation…

Calling the Function w = Volta(w, 10); ; push second arg AND R0, R0, #0 ADD R0, R0, #10 ADD R6, R6, #-1 STR R0, R6, #0 ; push first argument LDR R0, R5, #0 ADD R6, R6, #-1 STR R0, R6, #0 ; call subroutine JSR Volta q r w dyn link ret addr ret val a xFD00 new R6 Note: Caller needs to know number and type of arguments, doesn't know about local variables. R5 R6

Starting the Callee Function ; leave space for return value ADD R6, R6, #-1 ; push return address ADD R6, R6, #-1 STR R7, R6, #0 ; push dyn link (caller’s frame ptr) ADD R6, R6, #-1 STR R5, R6, #0 ; set new frame pointer ADD R5, R6, #-1 ; allocate space for locals ADD R6, R6, #-2 m k dyn link ret addr ret val q r w dyn link ret addr ret val a xFCFB x xFD00 new R6 new R5 R6 R5

Ending the Callee Function return k; ; copy k into return value LDR R0, R5, #0 STR R0, R5, #3 ; pop local variables ADD R6, R5, #1 ; pop dynamic link (into R5) LDR R5, R6, #0 ADD R6, R6, #1 ; pop return addr (into R7) LDR R7, R6, #0 ADD R6, R6, #1 ; return control to caller RET m k dyn link ret addr ret val q r w dyn link ret addr ret val a xFCFB x xFD00 R6 R5 new R6 new R5

Resuming the Caller Function w = Volta(w,10); JSR Volta ; load return value (top of stack) LDR R0, R6, #0 ; perform assignment STR R0, R5, #0 ; pop return value ADD R6, R6, #1 ; pop arguments ADD R6, R6, #2 ret val q r w dyn link ret addr ret val a xFD00 R6 R5 new R6

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

Activation Record Format Function stacked stuff …….. Local Variables Caller’s Frame Pointer (R5) Caller’s Return PC (R7) Function Return Value Function Pass Value n …….. Function Pass Value 1 R6 R5 X0000 XFFFF

Practice Creating Activation Records C program with function calls C program with recursive calls