Stacks and HeapsCS-502 Fall 20071 A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
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
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
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:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
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.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
CS 536 Spring Run-time organization Lecture 19.
CS 3013 & CS 502 Summer 2006 Digressions:– Producer-Consumer and Stacks 1 Two Digressions Stacks Producer-Consumer models.
Run-Time Storage Organization
Intro to Computer Architecture
Run time vs. Compile time
More on FunctionsCS-2301 B-term More on Functions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language,
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Run-time Environment and Program Organization
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Recursion and Function Implementation CS-2301 D-term Recursion and Implementation of Functions CS-2301 System Programming C-term 2009 (Slides include.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Recursion and Implementation of Functions
COP4020 Programming Languages
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
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.
Runtime Environments Compiler Construction Chapter 7.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
CPSC 388 – Compiler Design and Construction Runtime Environments.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
RecursionCIS 1057 Fall Recursion CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed from Professor Hugh C. Lauer. Slides.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
1 Topic 6: Activation Records COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Storage Allocation Mechanisms
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 Environments Chapter 7
Implementing Subprograms
© Craig Zilles (adapted from slides by Howard Huang)
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Run-time organization
Procedures (Functions)
Processes in Unix, Linux, and Windows
Outline for this evening
Chapter 9 :: Subroutines and Control Abstraction
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
The University of Adelaide, School of Computer Science
Understanding Program Address Space
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Recursion and Implementation of Functions
Program and memory layout
Processes in Unix, Linux, and Windows
Program and memory layout
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
© Craig Zilles (adapted from slides by Howard Huang)
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System Concepts, 7 th ed., by Silbershatz, Galvin, & Gagne and from Modern Operating Systems, 2 nd ed., by Tanenbaum)

Stacks and HeapsCS-502 Fall Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n <= 1) return (1); else int y = factorial(n-1); return (y * n); } Imagine also the caller:– int x = factorial(100); What does compiled code look like?

Stacks and HeapsCS-502 Fall Compiled code: the caller int x = factorial(100); Put the value “100” somewhere that factorial can find Put the current program counter somewhere so that factorial can return to the right place in caller Provide a place to put the result, so that caller can find it

Stacks and HeapsCS-502 Fall Compiled code: factorial function Save the caller’s registers somewhere Get the argument n from the agreed-upon place Set aside some memory for local variables and intermediate results – i.e., y, n - 1 Do whatever it was programmed to do Put the result where the caller can find it Restore the caller’s registers Transfer back to the program counter saved by the caller

Stacks and HeapsCS-502 Fall Question: Where is “somewhere”? So that caller can provide as many arguments as needed (within reason)? So that called routine can decide at run-time how much temporary space is needed? So that called routine can call any other routine, potentially recursively?

Stacks and HeapsCS-502 Fall Answer: a “Stack” Stack – a linear data structure in which items are added and removed in last-in, first-out order. Calling program Push arguments & return address onto stack After return, pop result off stack

Stacks and HeapsCS-502 Fall “Stack” (continued) Called routine Push registers and return address onto stack Push temporary storage space onto stack Do work of the routine Pop registers and temporary storage off stack Leave result on stack Return to address left by calling routine

Stacks and HeapsCS-502 Fall Stack (continued) Definition: context – the region of the stack that provides the execution environment of a particular call to a function Implementation Usually, a linear piece of memory and a stack pointer contained in a (fixed) register Occasionally, a linked list Recursion Stack discipline allows multiple contexts for the same function in the stack at the same time

Stacks and HeapsCS-502 Fall Stacks in Modern Systems All modern programming languages require a stack Fortran and Cobol did not (non-recursive) All modern processors provide a designated stack pointer register All modern process address spaces provide room for a stack Able to grow to a large size May grow upward or downward

Stacks and HeapsCS-502 Fall Process Address Space (Typical) 0x xFFFFFFFF Virtual address space program code (text) static data heap (dynamically allocated) stack (dynamically allocated) PC SP See also Silbershatz, figure 3.1

Stacks and HeapsCS-502 Fall Stacks in Multi-threaded Environments Every thread requires its own stack Separate from all other stacks Each stack may grow separately Address space must be big enough to accommodate stacks for all threads

Stacks and HeapsCS-502 Fall Stacks in Multi-threaded Address Space 0x xFFFFFFFF Virtual address space code (text) static data heap thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3) SP PC

Stacks and HeapsCS-502 Fall Stacks in Multi-threaded Environments Every thread requires its own stack Separate from all other stacks Each stack may grow separately Address space must be big enough to accommodate stacks for all threads Some small or RT operating systems are equivalent to multi-threaded environments

Stacks and HeapsCS-502 Fall Heap A place for allocating memory that is not part of last-in, first-out discipline I.e., dynamically allocated data structures that survive function calls E.g., strings in C new objects in C++, Java, etc.

Stacks and HeapsCS-502 Fall Process Address Space (Typical) 0x xFFFFFFFF Virtual address space program code (text) static data heap (dynamically allocated) stack (dynamically allocated) PC SP See also Silbershatz, figure 3.1

Stacks and HeapsCS-502 Fall Dynamically Allocating from Heap malloc() – POSIX standard function Allocates a chunk of memory of desired size Remembers size Returns pointer free () – POSIX standard function Returns previously allocated chunk to heap for reallocation Assumes that pointer is correct!

Stacks and HeapsCS-502 Fall Dynamically Allocating from Heap malloc() – POSIX standard function Allocates a chunk of memory of desired size Remembers size Returns pointer free () – POSIX standard function Returns previously allocated chunk to heap for reallocation Assumes that pointer is correct! Storage leak – failure to free something

Stacks and HeapsCS-502 Fall Heaps in Modern Systems Many modern programming languages require a heap C++, Java, etc. NOT Fortran Typical process environment Grows toward stack Multi-threaded environments All threads share the same heap Data structures may be passed from one thread to another.

Stacks and HeapsCS-502 Fall Heap in Multi-threaded Address Space 0x xFFFFFFFF Virtual address space code (text) static data heap thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3) SP PC Heap

Stacks and HeapsCS-502 Fall Stacks in Multi-threaded Address Space 0x xFFFFFFFF Virtual address space code (text) static data heap thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3) SP PC What’s this?

Stacks and HeapsCS-502 Fall Questions?