Int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1.

Slides:



Advertisements
Similar presentations
Algorithms and data structures
Advertisements

The University of Adelaide, School of Computer Science
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Stack Frames: Using LINK. CEG 320/5208: Stack frames and LINK2 Assembling source code One source file: –Use ORG statements for data and code –Assemble.
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
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
DSP Implementation Lecture 3. Anatomy of a DSP Project In VDSP Linker Description File (.LDF) Source Files (.asm,.c,.h,.cpp,.dat) Object Files (.doj)
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
1 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
Run-Time Storage Organization
Run time vs. Compile time
Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.
Run-time Environment and Program Organization
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
System Calls 1.
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 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.
Stack and Heap Memory Stack resident variables include:
CPSC 388 – Compiler Design and Construction Runtime Environments.
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
CSC 8505 Compiler Construction Runtime Environments.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Chapter 14 Functions.
Computer Architecture & Operations I
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
Functions.
CSCI206 - Computer Organization & Programming
COMPILERS Activation Records
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Procedures (Functions)
Chapter 9 :: Subroutines and Control Abstraction
Activation Records and Function Calls
Stack Frame Linkage.
Memory Allocation CS 217.
The University of Adelaide, School of Computer Science
Understanding Program Address Space
Topic 3-b Run-Time Environment
Binding Times Binding is an association between two things Examples:
UNIT V Run Time Environments.
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Basic Guarantees Right off the bat… what I'm going to describe is what almost certainly happens on contemporary machines, but there is absolutely nothing.
Run-time environments
Topic 2b ISA Support for High-Level Languages
Implementing Functions: Overview
Presentation transcript:

int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1

main calls a a calls b b calls c c returns to b b returns to a a returns to main The call tree: 2 main a b c

Partial assembly code for main() and a() main:.. branch a # call a_rtn: copy x, a_rtn_value. a:.. branch b # call b_rtn: copy y, b_rtn_value. branch a_rtn # return from a 3

Modified assembly code for main() and a() main:. branch a # call a_rtn1: copy x, a_rtn_value. branch a # another call to a a_rtn2: copy x, a_rtn_value. a:.. branch b # call b_rtn: copy y, b_rtn_value. branch # return from a 4

Different example: Recursion int main( ) { x = a( ); } int a( ) { z = a( ); } 5 main a

main: la a_rtn, rtn1 branch a # call rtn1: # copy out return value a: # recursive call la a_rtn, rtn2 branch a rtn2: # copy out return value. branch (a_rtn) 6

To make this work, the code needs to – just before each call, save the return address – before each return, get and use the most recently saved return address The data structure required is called a stack. 7

A stack organizes data LIFO – Last In First Out push puts data into the stack pop takes data out of the stack 8

9 The initial state of the stack: it is empty. After a first push of a onto the stack. a

10 After a third push of c onto the stack. a a b b c After a second push of b onto the stack.

11 Push d. aa bb d The stack returns c due to a pop operation.

12 Pop returns b. aa b Pop returns d.

Different example: Recursion main: la a_rtn, rtn1 branch a # call rtn1: # copy out return value a: push a_rtn # recursive call la a_rtn, rtn2 branch a rtn2: # copy out return value. pop a_rtn branch (a_rtn) 13

The problem of overwritten return addresses is solved with a stack. The same problem exists with – parameters – variables local to a function The solution bundles all these saved/restored values into a single set to be pushed/popped. The set of items is called an activation record (AR) or stack frame. 14

Important detail: A compiler cannot always know if there is recursion in a program! Consider separate compilation. In file 1: int a() { z = b(); } In file 2: int b() { x = a(); } 15

Memory Allocation What is in memory? When is it in memory? And, when is or was the memory space allocated? Where within memory is it? 16

What and Where are – program code (machine code) – program variables (global) – stack – heap Each can be thought of as residing in its own, separate section of memory. These sections are often identified as segments. 17

When is the memory space allocated? static The compiler knows details of the allocation, so causes the assembler to allocate the space. This implies that the memory image created by the assembler contains the memory space. dynamic The allocation of memory space occurs while the program executes. 18

Program Code The program's source code is compiled and then it is assembled to produce machine code (memory image). To run the program, the memory image is placed (copied) into memory. Therefore, the code is already in memory, and classified as a static allocation. 19

int x, y, z; /* global variables */ int main() { } Both the compiler, and therefore the assembler, know exactly how much memory space is needed. When the program (memory image) is placed into memory, the allocation of memory for the integers has been completed. It is therefore classified as a static allocation. 20

#define MAX 4 int array[MAX]; /* global */ int main() { } 21

char *stringptr; stringptr = (char *) malloc(bytes_needed + 1); 22

23 Consider space for the activation record: a: push a_rtn. la a_rtn, rtn2 branch a rtn2:. pop a_rtn branch (a_rtn)

int fcn(int y) { } int main() { int x, y; y = 20; x = fcn(y); } 24

Summary Static – program code – global variables Dynamic – anything that goes onto the stack, such as parameters and return address – memory allocated as the program is running, such as allocation done with malloc() 25