Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.

Slides:



Advertisements
Similar presentations
Functions and scope Confidence In scope Reference: K&K textbook, Chapter 4.
Advertisements

Assembly Language Programming Chapter 8
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.
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Computer Architecture CSCE 350
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
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)
CS 536 Spring Run-time organization Lecture 19.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
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.
CS2422 Assembly Language & System Programming October 26, 2006.
Run time vs. Compile time
Semantics of Calls and Returns
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Chapter 9: Subprogram Control
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.
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
7/13/20151 Topic 3: Run-Time Environment Memory Model Activation Record Call Convention Storage Allocation Runtime Stack and Heap Garbage Collection.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Procedures and the Stack Chapter 10 S. Dandamudi.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Objective At the conclusion of this chapter you will be able to:
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
CSC 221 Computer Organization and Assembly Language
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
CSC 8505 Compiler Construction Runtime Environments.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Instructions for test_function
Run-time organization
Microprocessor and Assembly Language
Stack Frames and Advanced Procedures
Assembly Language for Intel-Based Computers, 4th Edition
PZ09A - Activation records
Chapter 6 - Procedures and Macros
Multi-modules programming
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Miscellaneous Topics.
Runtime Environments What is in the memory?.
Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/24
Computer Organization and Assembly Language
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing parameters by value and by reference Classifying parameters as input, output, and input- output Recursion

Local Variables Variables that are declared in the data segment are static global variables Static – a variable’s lifetime is the same as the duration of the program Global – visible from all procedures in the current source code file. A local variable is a variable is created, used, and destroyed within a single procedure.

Local Variable Advantages Restricted access to a local variable helps when you are debugging, because only a limited number of program statements can modify the variable. Make efficient use of memory, because their storage space can be released and made available to new variables. The same variable name can appear in two or more procedures without creating a name clash.

Local Variable Constraints Created on the runtime stack. They cannot be given default values at assembly time. They will be initialized at runtime.

Local Directive Must be placed on the line immediately following a PROC directive. MySub PROC LOCAL var1 : BYTE BubbleSort PROC LOCAL temp:DWORD, SwapFlag:BYTE Variables and variable types are separated by a colon Variables are separated by commas, which may span multiple lines

Automatic Code Generation Where are these variables saved – on the stack. Assembling and debugging this procedure BubbleSort PROC LOCAL temp:DWORD, SwapFlag:BYTE Ret BubbleSort Endp

Assembly Code listing of Proc BubbleSort: –Push ebp –Movebp, esp –Addesp, 0FFFFFFF8h ;add –8 to ESP –Movesp, ebp –Popebp –ret

The Stack for the PROC EBP = 4 bytes Temp = 4 bytes SwapFlag = 1 byte => The add instruction adds –8 to ESP, moving it downward, and creating an opening in the stack between ESP and EBP for the two local variables. Return address EBP temp SwapFlag EBP [EBP – 4] [EBP – 8] ESP

How big should the stack be? Need 2-4 bytes for each return address Need enough space for local variables If procedure calls are nested, the stack space must be large enough to hold the sum of all local variables active at any point in the program’s execution.

Stack Parameters There are two basic types of procedure parameters: –Register parameters Optimized for program execution speed Create code clutter in calling program (existing register contents often must be saved before they can be loaded with argument values)

Procedure Parameters (2) Stack parameters The required arguments must be pushed on the stack by a calling program. Push OFFSET array Push LENGTHOF array Push TYPE array Call DumpMem The INVOKE directive automatically pushes arguments on the stack and calls a procedure. INVOKE DumpMem, OFFSET array, LENGTHOF array, TYPE array Nearly all high-level languages use stack parameters

ADDR Operator Can be used to pass a pointer when calling a procedure with the INVOKE directive. Passing by reference INVOKE fillarray, ADDR myarray ADDR returns either a near pointer or a far pointer, depending on what is called for by the program’s memory model.

Recursion

Stack Frames See other slides