Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.

Slides:



Advertisements
Similar presentations
Passing by-value vs. by-reference in ARM by value C code equivalent assembly code int a;.section since a is not assigned an a:.skip initial.
Advertisements

The University of Adelaide, School of Computer Science
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Introduction to Embedded Systems Intel Xscale® Assembly Language and C Lecture #3.
10/6: Lecture Topics Procedure call Calling conventions The stack
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 6.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
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:
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
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
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.
7/13/20151 Topic 3: Run-Time Environment Memory Model Activation Record Call Convention Storage Allocation Runtime Stack and Heap Garbage Collection.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
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
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Arrays and Strings in Assembly
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Chapter 14 Functions.
Computer Architecture & Operations I
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.
The Stack.
Run-time organization
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Chapter 14 Functions.
In this lecture Global variables Local variables System stack
The Stack.
Stack and Subroutines Module M17.1 Section 11.2.
Passing by-value vs. by-reference in ARM
The University of Adelaide, School of Computer Science
Understanding Program Address Space
ARM Load/Store Instructions
Lecture 5: Procedure Calls
Von Neumann model - Memory
ECE 3430 – Intro to Microcomputer Systems
UNIT V Run Time Environments.
Program and memory layout
Program and memory layout
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Chapter 14 Functions.
Run-time environments
Topic 2b ISA Support for High-Level Languages
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided into stack frames - one per procedure invocation) heap – dynamically allocated variables initialized data (data) – global and static variables that are initialized by the programmer. includes rodata – read only data unitialized data (bss) – global and static variables that are initialized to zero or do not have explicit initialization in source code. text - program code / instructions

Run-time Environment for a Program high addresses low addresses Program int g = g in data int main() { int i alloc. on stack... } (note: some memory diagrams may be reversed, with high memory at top) Dynamically allocated variable, e.g., malloc(); new; C pointers Space for saved procedure information Variables declared once per program... Text Data Heap... Stack... program’s layout in memory Stack pointer

Memory Stack stack matches the last in first out (LIFO) behavior of nested procedure calls (including recursion) storage for automatic variables, that is, for local variables that are allocated at the beginning of a subroutine call and that are discarded at the end of the call The collection of information related to a specific instance of calling a subroutine is termed a "stack frame" -- a generic stack frame consists of parameters, return address, registers to save, and local variables Stack pointer (sp) –points to top of stack

Run-time Environment for a Program base of stack sp – stack pointer higher addresses Stack is located in high addresses in memory and grows toward small addresses, so creating space for local variables decrements the stack pointer Subroutines must make room for local variables local variables Current stack frame – locals addressed with addresses sp+k older stack frames – represent call chain from main() lower addresses

Memory Stack Recall that ARM has the following load and store instructions: ldr – load word ldrh – load halfword ldrb – load byte ldm – load multiple words str – store word strh – store halfword strb – store byte stm – store multiple words

Memory Stack typical form of load for accessing a local variable: ldr rn, [sp, #4] typical form of store for accessing a local variable: str rs, [sp, #4]

Memory Stack Loads and stores (example) /* int main(){ int a = 0; a = a + 1; } */ a.req r4 add sp, sp, space for local a's address is sp mov a, a = 0; next three instructions str a, perform a = a + 1; ldr a, 1. read a's value (from the stack) add a, a, 2. add one str a, 3. store new value back into (into the stack) mov r0, #0 add sp, sp, restore the stack pointer pop {lr} bx lr

Memory Stack Pointers a pointer is the address of an object in memory a pointer is 64 bits in length (on our current machines) a pointer can be in a register or in a memory word example int main() { int a; int *pa = &a; *pa = 5; return 0; }

Memory Stack Pointers.global main.type main, %function main: push {r5} add sp, sp, space for local vars add r5, sp, &a in r5 mov r4, &a in r4 str r4, [r5, &a in pa ([sp+4]) mov r3, 5 in r3 str r3, [r4, 5 in a ([sp]) mov r4, #0 mov r0, r4 add r5, r5, #8 mov sp, r5 pop {r5} bx lr... 5 &a... a: pa: sp sp+4

Memory Stack Extended example with printf stmts int main(){ int a = 0; int *pa = &a; printf("address of a = 0x%08x and contents of a = 0x%08x\n", &a,a); printf("address of pa = 0x%08x and contents of pa = 0x%08x\n", &pa,pa); *pa = 5; printf("address of a = 0x%08x and contents of a = 0x%08x\n", &a,a); printf("address of pa = 0x%08x and contents of pa = 0x%08x\n", &pa,pa); return 0; } [23:18:44] [130]./a.out address of a = 0xf6fffac8 and contents of a = 0x address of pa = 0xf6fffacc and contents of pa = 0xf6fffac8 address of a = 0xf6fffac8 and contents of a = 0x address of pa = 0xf6fffacc and contents of pa = 0xf6fffac8