C function call conventions and the stack

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.
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.
Context Switch Animation Another one by Anastasia.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
Context switch in Linux
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Accessing parameters from the stack and calling functions.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
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.
Machine-Level Programming III: Procedures Jan 30, 2003
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.
ESP int f(int x) {.... } int g(int y) { …. f(2); …. } int main() { …. g(1); …. } EIP 100: 200: 250: 300: 350:
6.828: PC hardware and x86 Frans Kaashoek
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Y86 Processor State Program Registers
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.
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.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 21: Calling.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
F’08 Stack Discipline Aug. 27, 2008 Nathaniel Wesley Filardo Slides stolen from CMU , whence they were stolen from CMU CS 438.
Functions/Methods in Assembly
Compiler Construction Code Generation Activation Records
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.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Structured Data –struct s / union s –Arrays of structs.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Section 5: Procedures & Stacks
Recitation 3: Procedures and the Stack
A job ad at a game programming company
CS 177 Computer Security Lecture 9
Assembly function call convention
Reading Condition Codes (Cont.)
143A: Principles of Operating Systems Lecture 4: Calling conventions
Exploiting & Defense Day 2 Recap
Introduction to Compilers Tim Teitelbaum
Recitation 2 – 2/4/01 Outline Machine Model
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
asum.ys A Y86 Programming Example
Machine-Level Programming III: Switch Statements and IA32 Procedures
Y86 Processor State Program Registers
Machine-Level Programming 4 Procedures
Condition Codes Single Bit Registers
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
MIPS Procedure Calls CSE 378 – Section 3.
The Runtime Environment
X86 Assembly Review.
Machine-Level Representation of Programs (x86-64)
“Way easier than when we were students”
ICS51 Introductory Computer Organization
Presentation transcript:

C function call conventions and the stack based on http://www.cs.umbc.edu/~chang/cs313.s02/stack.shtml  ©Gabriel Kliot, Technion

©Gabriel Kliot, Technion The stack High Addresses 0x5000 Bottom of the Stack . Stack grows down 0x1004 esp 0x1000 . Pushes Move the Top Of Stack to Lower Addresses Pops Move the Top Of Stack to Higher Addresses Low Addresses ESP always points on the last item on the stack PUSH decrements ESP and writes the value on top of the stack POP reads the value from top of the stack and increments ESP ©Gabriel Kliot, Technion

©Gabriel Kliot, Technion #1 - The caller’s actions before the function call int main() { foo(3,5); } int foo(int x, int y); Caller saved EAX, ECX, EDX – if needed Argument 2 = 5 Argument 1 = 3 Return address esp main() pushes EAX, ECX, EDX on the stack (if needed) main() pushes the arguments of foo(), last argument first on the stack main() issues: call foo EIP register is pushed on the stack EIP is loaded with foo() address ©Gabriel Kliot, Technion

©Gabriel Kliot, Technion #2 - The callee’s actions after the function call int main() { foo(3,5); } int foo(int x, int y); Caller saved EAX, ECX, EDX – if needed Argument 2 = 5 [EBP+12] Argument 1 = 3 [EBP+8] Return address Old (main()’s) EBP ebp foo() sets up its stack frame: pushl %ebp movl %esp, %ebp store EBX, ESI, EDI on the stack (if needed) allocate space for local vars on the stack Old ESI, EDI, EBX Function frame Local variable 1 Local variable 2 esp ©Gabriel Kliot, Technion

©Gabriel Kliot, Technion #3 - The callee’s actions before returning int main() { foo(3,5); } int foo(int x, int y); Caller saved EAX, ECX, EDX – if needed Argument 2 = 5 Argument 1 = 3 esp store return value in EAX advance ESP back to point above all local variables Restore EBX, ESI, EDI – pop them of the stack restore EBP: movl %ebp,%esp – (not really necessary if ESP already points to where old EBP stored) popl %ebp ret: pops the return address of the stack and stores it into EIP ©Gabriel Kliot, Technion

©Gabriel Kliot, Technion #4 - The caller’s actions after returning int main() { foo(3,5); } int foo(int x, int y); esp main can pop all arguments of the stack add $8, %esp Use the return value stored in EAX POP EAX, ECX, EDX (if needed) ©Gabriel Kliot, Technion