Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.

Slides:



Advertisements
Similar presentations
Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
Advertisements

Calling sequence ESP.
CS 4284 Systems Capstone Godmar Back Processes and Threads.
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Procedure Calls Prof. Sirer CS 316 Cornell University.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I Lecturer: Hui Wu Session 2, 2005.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
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:
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
CS 536 Spring Run-time organization Lecture 19.
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.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
RapUp Dynamic Allocation of Memory in C Last HW Exercise Review for Final Final Exam Next Thursday – Same Time / Same Place.
Machine-Level Programming III: Procedures Jan 30, 2003
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Run-time Environment and Program Organization
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
CMSC 414 Computer and Network Security Lecture 20 Jonathan Katz.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
CSC 8505 Compiler Construction Runtime Environments.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Functions/Methods in Assembly
Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 JSR Instruction Jumps to a location (like.
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Declarations (aka prototype) int.
CS 155 Section 1 PP1 Eu-Jin Goh. Setting up Environment Demo.
4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.
Compiler Construction Code Generation Activation Records
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
1 Assembly Language: Function Calls Jennifer Rexford.
Section 5: Procedures & Stacks
CS 177 Computer Security Lecture 9
Instructions for test_function
Chapter 14 Functions.
Reading Condition Codes (Cont.)
Computer structure: Procedure Calls
C function call conventions and the stack
© Craig Zilles (adapted from slides by Howard Huang)
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
The Stack & Procedures CSE 351 Spring 2017
Chapter 7 Subroutines Dr. A.P. Preethy
Machine-Level Programming 4 Procedures
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
Understanding Program Address Space
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Procedures and Calling Conventions
Operating Systems: A Modern Perspective, Chapter 6
“Way easier than when we were students”
© Craig Zilles (adapted from slides by Howard Huang)
Topic 2b ISA Support for High-Level Languages
Implementing Functions: Overview
Presentation transcript:

Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Announcements Homework Set #2 due Friday at 11 am - extension Program Assignment #1 due Tuesday Feb. 15 at 11 am - note extension Read chapters 6 and 7

Multiple Processes Main Memory Code Data Process P1 Heap Stack Code Data Process P2 Heap Stack Code More Data, Heap, Stack OS PCB for P2 PCB for P1 CPU Execution Program Counter (PC) ALU

Threads Process P1 is multithreaded Process P2 is single threaded The OS is multiprogram med If there is preemptive timeslicing, the system is multitasked Main Memory Code Data Process P1’s Address Space Heap Code Data Process P2 Heap Stack PC1 Reg. State Thread 1 Stack PC2 Reg. State Thread 2 Stack PC3 Reg. State Thread 3

Stack Behavior Run-time memory image Essentially code, data, stack, and heap Code and data loaded from executable file Stack grows downward, heap grows upward User stack Heap Read/write.data,.bss Read-only.init,.text,.rodata Unallocated Run-time memory address 0 max address

unallocated Relating the Code to the Stack main() { int a1;... foo1();... } Stack max memory unallocated Stack grows downward from thread-specific max memory main()’s local variables are allocated here, e.g. int a1 space for main()’s local variables gets allocated on the stack foo1()’s local variables are allocated here space for foo1()’s local variables is allocated below main() on the stack top of stack

Relating the Code to the Stack The CPU uses two registers to keep track of a thread’s stack when the thread is executing –stack pointer register (%esp) points to the top of the stack, i.e. contains the memory address of top of the stack –frame/base pointer register (%ebp) points to the bottom or base of the current frame of the function that is executing this frame pointer provides a stable point of reference while the thread is executing, i.e. the compiled code references local variables and arguments by using offsets to the frame pointer These two CPU registers are in addition to the thread- specific state that we’ve already seen the CPU keeps track of: –program counter (PC) –instruction register (IR), –status registers

unallocated Relating the Code to the Stack main() { int a1;... foo1();... } Stack max memory unallocated main()’s local variables are allocated here, e.g. int a1 foo1()’s local variables are allocated here top of stack %ebp %esp main’s frame foo1’s frame While executing foo1(), the CPU’s base/frame pointer points to the beginning of foo1()’s frame, and the stack pointer points to the top of the frame (which is also the top of the stack)

Calling a Function When main() calls function foo1(), the calling function (a.k.a. caller) has to: –pass arguments to foo1() these can be passed on the stack can also be passed via additional CPU registers –make sure it informs foo1() where to resume in main() after returning from foo1() save the return address, i.e. PC, on the stack –save any register that would have to be restored after returning into main() these are called caller registers. Half of the six integer register for IA32 CPU’s are caller registers whose contents should be saved on the stack by the calling function before entering the called function.

unallocated Calling a Function main() { int a1;... foo1(a1);... } Stack max memory unallocated main()’s local variables are allocated here, e.g. int a1 %ebp %esp main’s frame When the PC is here, just before calling foo1(), the stack looks as follows PC top of the stack

unallocated Calling a Function main() { int a1;... foo1(a1);... } Stack max memory unallocated main()’s local variables are allocated here, e.g. int a1 %ebp %esp main’s frame When the PC causes foo1 to be called with argument a1, the assembly code actually contains several steps to set up arguments on the stack, save the return address, then jumps to the called function PC top of the stack

unallocated Calling a Function main() { int a1, b2;... foo1(a1, b2);... } PC unallocated Stack max memory unallocated main()’s local variables are allocated here, e.g. int a1 %ebp main’s frame %esp top of the stack save caller registers on stack (not shown) call foo1, e.g. jump to called function foo1 (changes PC) assembly code: push arguments onto the stack arg 2 arg 1 %esp top of the stack push the return address onto the stack return address %esp top of the stack

Entering a Function When foo1() begins executing, it first needs to: –save the old frame pointer so that it can be restored once foo1() is done executing and main() resumes this is saved onto the stack, i.e. pushed onto the stack –reset the frame pointer register to point to the new base of the current frame –save any register state that would have to be restored before exiting the function these are called callee registers. Half of the six integer registers for IA32 CPUs are callee registers whose contents should be saved on the stack by the called function after it is has begun execution

Entering a Function foo1(int v1, v2) { local var’s... } PC unallocated Stack max memory unallocated main()’s local variables are allocated here, e.g. int a1 %ebp main’s frame %esp top of the stack arg 2 arg 1 %esp top of the stack return address %esp top of the stack assembly code: foo1 first saves the old frame pointer by pushing it onto the stack: pushl %ebp saved fr ptr %ebp %esp top of the stack foo1’s frame foo1 resets frame ptr to new base (current stack ptr): movl %esp, %ebp %ebp and foo1 saves any callee CPU registers on stack (not shown) PC foo1 allocates local variables by decrementing stack ptr %ebp %esp local var’s PC

Entering a Function Each time a function calls another function, the same set of operations is repeated, causing the stack to grow frame by frame: –push arguments and return address and caller register state onto the stack –push the old frame pointer onto the stack –reset the frame pointer to the base of the current frame –push callee register state onto the stack –decrement stack pointer to allocate local variables Note: pushl %src_reg is equivalent to the following pair of instructions: –subl $4, %esp // decrement stack ptr to create space on stack –movl %src_reg, (%esp) // store reg.value in newly created space

Entering a Function Just to recap, the assembly code after entry into a function typically has at least the following two instructions: –pushl %ebp // save the old frame ptr on the stack –movl %esp, %ebp // reset frame ptr to serve as a base reference for the new frame

Exiting a Function When foo1() finishes executing and wants to exit/return, it needs to: –restore any callee register state –deallocate everything off the stack the stack pointer is reset to point to the address that the base frame register is currently pointing at note that this contains the saved old frame pointer –restore the frame pointer to the value that it had before entering foo(), so that main() sees a familiar restored value for the base/frame pointer since the stack ptr is now pointing to the saved old frame pointer, then pop the saved old frame pointer off the stack and into the base frame register popping also increments the stack pointer –Now the stack pointer is pointing at the return address. Invoke the “ret” system call to exit the function, which pops the return address off the stack and jumps to this location, which is the address of the first instruction in main() immediately after the call to foo()

Exiting a Function Note: popl %dest_reg is equivalent to the following pair of instructions: –movl (%esp), %dest_reg // store mem contents pointed to by stack ptr into destination –addl $4, %esp // increment the stack pointer to deallocate space off stack

Exiting a Function unallocated Stack max memory unallocated main()’s local variables are allocated here, e.g. int a1 %ebp %esp main’s frame arg 2 arg 1 return address saved fr ptr %ebp local var’s foo1’s frame assembly code: foo1 restores callee save registers (not shown) foo1(int v1, v2) { local var’s... } PC deallocate local variables off of the stack, which resets stack ptr equal to current base/frame ptr %esp and unallocated pop saved frame pointer off the stack and into the base/frame register %ebp %esp pop the saved return address off the stack and jump to this location (PC changes) %esp

Exiting a Function Assembly code for exiting a function typically looks like the following three instructions: –movl %ebp, %esp // deallocate local var’s by incrementing stack ptr all the way up to base/frame ptr –popl %ebp // pop saved frame ptr from stack into base/frame register –ret // pop return address from stack and jump to this location

Reentering the Calling Function When main() begins again after foo1() has exited, main() has to: –restore any caller registers –inspect any arguments that may have been passed back these arguments are still accessible in its frame!

User/Kernel Level Threads This material will be posted in an addendum to these slides