Section 5: Procedures & Stacks

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.
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Procedure call frame: Hold values passed to a procedure as arguments
Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
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.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 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.
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.
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
September 22, 2014 Pengju (Jimmy) Jin Section E
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
F’08 Stack Discipline Aug. 27, 2008 Nathaniel Wesley Filardo Slides stolen from CMU , whence they were stolen from CMU CS 438.
CS 155 Section 1 PP1 Eu-Jin Goh. Setting up Environment Demo.
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.
Carnegie Mellon Midterm Review : Introduction to Computer Systems October 15, 2012 Instructor:
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Recitation 3: Procedures and the Stack
Assembly function call convention
Chapter 14 Functions.
Computer Architecture & Operations I
Reading Condition Codes (Cont.)
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.
Computer Science 210 Computer Organization
C function call conventions and the stack
CSCE 212 Computer Architecture
Functions 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 14 Functions.
Recitation: Attack Lab
Switch & Procedures & The Stack I CSE 351 Winter 2017
Machine-Level Programming III: Switch Statements and IA32 Procedures
Machine-Level Programming 4 Procedures
Procedures & The Stack I CSE 351 Autumn 2016
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Stack Frame Linkage.
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
MIPS Procedure Calls CSE 378 – Section 3.
Understanding Program Address Space
The Runtime Environment
The Stack & Procedures CSE 351 Winter 2018
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Machine-Level Representation of Programs (x86-64)
“Way easier than when we were students”
© Craig Zilles (adapted from slides by Howard Huang)
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

Section 5: Procedures & Stacks Stacks in memory and stack operations The stack used to keep track of procedure calls Return addresses and return values Stack-based languages The Linux stack frame Passing arguments on the stack Allocating local variables on the stack Register-saving conventions Procedures and stacks on x64 architecture Procedure Calls

Procedure Call Overview Caller … <set up args> call <clean up args> <find return val> Callee <create local vars> … <set up return val> <destroy local vars> return Callee must know where to find args Callee must know where to find “return address” Caller must know where to find return val Caller and Callee run on same CPU → use the same registers Caller might need to save registers that Callee might use Callee might need to save registers that Caller has used Procedure Calls

Procedure Call Overview Caller … <save regs> <set up args> call <clean up args> <restore regs> <find return val> Callee <save regs> <create local vars> … <set up return val> <destroy local vars> <restore regs> return The convention of where to leave/find things is called the procedure call linkage Details vary between systems We will see the convention for IA32/Linux in detail What could happen if our program didn’t follow these conventions? Procedure Calls

Procedure Control Flow Use stack to support procedure call and return Procedure call: call label Push return address on stack Jump to label Procedure Calls

Procedure Control Flow Use stack to support procedure call and return Procedure call: call label Push return address on stack Jump to label Return address: Address of instruction after call Example from disassembly: 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax Return address = 0x8048553 Procedure return: ret Pop return address from stack Jump to address Procedure Calls

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax call 8048b90 0x110 0x10c 0x108 123 %esp 0x108 %eip 0x804854e %eip: program counter Procedure Calls

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 %esp 0x108 %esp 0x108 %eip 0x804854e %eip 0x804854e %eip: program counter Procedure Calls

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 %esp 0x108 %esp 0x108 %eip 0x804854e %eip 0x804854e 0x8048553 %eip: program counter Procedure Calls

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 %esp 0x108 %esp 0x108 0x104 %eip 0x804854e %eip 0x804854e 0x8048553 %eip: program counter Procedure Calls

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 %esp 0x108 %esp 0x108 0x104 %eip 0x804854e %eip 0x8048553 + 0x000063d 0x8048b90 %eip: program counter Procedure Calls

Procedure Return Example 8048591: c3 ret ret 0x110 0x10c 0x108 123 0x104 0x8048553 %esp 0x104 %eip 0x8048591 %eip: program counter Procedure Calls

Procedure Return Example 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 %esp 0x104 %esp 0x104 %eip 0x8048591 %eip 0x8048591 %eip: program counter Procedure Calls

Procedure Return Example 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 %esp 0x104 %esp 0x104 %eip 0x8048591 %eip 0x8048553 0x8048591 %eip: program counter Procedure Calls

Procedure Return Example 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 %esp 0x104 %esp 0x104 0x108 %eip 0x8048591 %eip 0x8048553 0x8048591 %eip: program counter Procedure Calls

Return Values By convention, values returned by procedures are placed in the %eax register Choice of %eax is arbitrary, could have easily been a different register Caller must make sure to save that register before calling a callee that returns a value Part of register-saving convention we’ll see later Callee placed return value (any type that can fit in 4 bytes – integer, float, pointer, etc.) into the %eax register For return values greater than 4 bytes, best to return a pointer to them Upon return, caller finds the return value in the %eax register Procedure Calls