ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
ITEC 352 Lecture 27 Memory(4). Review Questions? Cache control –L1/L2  Main memory example –Formulas for hits.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Assembly.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
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:
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Runtime Environments Source language issues Storage organization
Run-Time Storage Organization
Intro to Computer Architecture
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.
Run-time Environment and Program Organization
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
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.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
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
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
CPSC 388 – Compiler Design and Construction Runtime Environments.
5-1 Chapter 5 - Languages and the Machine Department of Information Technology, Radford University ITEC 352 Computer Organization Principles of Computer.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Basic Semantics Associating meaning with language entities.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
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.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 Implementing Subprograms.
Int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1.
ISBN Chapter 10 Implementing Subprograms.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
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.
Run-time organization
Procedures (Functions)
Procedures (Functions)
In this lecture Global variables Local variables System stack
Chapter 9 :: Subroutines and Control Abstraction
Activation Records and Function Calls
Application Binary Interface (ABI)
Implementing Subprograms
Understanding Program Address Space
by Richard P. Paul, 2nd edition, 2000.
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
Presentation transcript:

ITEC 352 Lecture 18 Functions in Assembly

Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly –Register based –Memory based Sethi / srl

Functions + Assembly Outline Functions

Functions + Assembly Registers Subroutine linkage with registers passes parameters in registers. High level language equivalent?

Functions + Assembly Memory Subroutine linkage with a data link area passes parameters in a separate area in memory. The address of the memory area is passed in a register ( %r5 here).

Functions + Assembly Back to functions Limitations of each approach –Registers –Data link area Recursion Possible solutions

Functions + Assembly Function calls void f() { printf(“enter f”); g(); printf(“exit f”); } void g() { printf(“enter g”); h(); printf(“exit g”); } void h() { printf(“enter h”); i(); printf(“exit h”); } void i() { printf(“enter i”); i(); printf(“exit i”); } void main() { f(); } Write out what is called? Does this remind you of any particular data structure?

Functions + Assembly Terms Function activation –When its code is being activated –How many times is f activated? Function deactivation –When a function’s code goes from being active to non-active

Functions + Assembly OS / Languages Keeps track of what is active what is not Scheduling algorithms –Multi-tasking Scope of variables –Where do they live, when can they be accessed?

Functions + Assembly Memory When a program needs to execute, the OS gives the program some memory. This memory is divided into (usually) atleast 3 parts: –Memory to store the generated assembly code of the program. Memory contains the instructions that make up the program. –Data segment: memory to store global variables. Sometimes, this same memory can be used to store dynamically allocated memory: i.e., we do not know at compilation time what memory is to be allocated here. (This is usually the memory we allocate using the “new” or “malloc” functions.) E.g., int x;//Let x be a variable whose value is given by user. get(x); // ask the user to enter the value for x. int y[] = new int[x] ; // Here we don’t know how much memory to allocate for y until the user supplies the value of x. –Control stack (also known as program stack) to store function activations as the program is executing. Here is where the local variables go. Hence, the lifetime of a local variable is limited to the lifetime of the function activation.

Functions + Assembly Activation record An activation record is the memory allocated for each function activation. It contains the following data: return address memory for each local variable. memory for parameter values passed from caller function memory addresses of dynamically allocated memory.

Functions + Assembly Step 1 Consider a program: 1. int f( int x, int y) { 2. int a = 10; 3. int b = 5; 4. } 5. int main() { 6. int z = 5; 7. f(z, z) ; 8. } Initially the stack is empty. Execution of this program starts with the function main. Hence, an activation record for main is created as shown on the stack. PROGRAM STACK Return address Activation record for main. Memory for z Stack pointer (%sp)

Functions + Assembly Step 2 Consider a program: 1. int f( int x, int y) { 2. int a = 10; 3. int b = 5; 4. } 5. int main() { 6. int z = 5; 7. f(z, z) ; 8. } Next: The function “f” is invoked. We have to create and then push the activation record of “f”. However, before we do that, we have to create space for the actual parameters that we want to pass to function f (here it is z, z) PROGRAM STACK Return address Parameters to be passed to f. Memory for z Stack pointer (%sp) 5 5 return address In ARC this is stored in %r15 (address of line 8)

Functions + Assembly Step 2B Consider a program: 1. int f( int x, int y) { 2. int a = 10; 3. int b = 5; 4. } 5. int main() { 6. int z = 5; 7. f(z, z) ; 8. } Next: This is a continuation from previous slide. You can see that we now have the complete activation record of function f. PROGRAM STACK Return address Activation record of function f. Memory for z Stack pointer (%sp) 5 5 return address(%r15) a (value 10) b (value 5)

Functions + Assembly Final step Consider a program: 1. int f( int x, int y) { 2. int a = 10; 3. int b = 5; 4. } 5. int main() { 6. int z = 5; 7. f(z, z) ; 8. } Next: After f finishes execution its activation record is no longer “live” -- it is popped out. PROGRAM STACK Return address Program stack after f has finished execution. You can see that the local variables a and b are no longer accessible, Memory for z Stack pointer (%sp)

Functions + Assembly Review More on how it works –Goal: understand how java  assembly works