CSC 2400: Computer Systems. Using the Stack for Function Calls

Similar documents
CSC 2400: Computer Systems. Using the Stack for Function Calls

CSC 8400: Computer Systems. Using the Stack for Function Calls

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Assembly Language: Function Calls

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Assembly Language: Function Calls" Goals of this Lecture"

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Systems I. Machine-Level Programming V: Procedures

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

Program Exploitation Intro

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

Function Call Convention

Lecture 4 CIS 341: COMPILERS

Assembly Language: Function Calls

Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to

X86 Stack Calling Function POV

Buffer Overflow Attack (AskCypert CLaaS)

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Lecture 5. Announcements: Today: Finish up functions in MIPS

x86 assembly CS449 Fall 2017

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

Functions in MIPS. Functions in MIPS 1

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Machine Programming 3: Procedures

System Software Assignment 1 Runtime Support for Procedures

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

Today. Putting it all together

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

SRC Assembly Language Programming - III

CIT Week13 Lecture

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

Implementing Subroutines. Outline [1]

CS213. Machine-Level Programming III: Procedures

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Buffer-Overflow Attacks on the Stack

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

Course Administration

Digital Forensics Lecture 3 - Reverse Engineering

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

Machine Program: Procedure. Zhaoguo Wang

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Subprograms: Arguments

Lab 10: Introduction to x86 Assembly

G Programming Languages - Fall 2012

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

Machine-level Programming (3)

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

3. Process Management in xv6

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

Buffer-Overflow Attacks on the Stack

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

CSE 351: Week 4. Tom Bergan, TA

Wednesday, October 15, 14. Functions

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

ASSEMBLY III: PROCEDURES. Jo, Heeseung

MIPS Procedure Calls. Lecture 6 CS301

Assembly III: Procedures. Jo, Heeseung

ECE232: Hardware Organization and Design

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

The x86 Architecture

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

Chapter 9 :: Subroutines and Control Abstraction

Procedure Call. Procedure Call CS 217. Involves following actions

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Plan. Regression testing: Demo of how to use the regress.sh script.

Chapter 14 Functions

Subprograms: Local Variables

Virtual Machine (Part II)

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

University of Washington

High Performance Computing Lecture 7. Matthew Jacob Indian Institute of Science

Reserve Engineering & Buffer Overflow Attacks. Tom Chothia Computer Security, Lecture 17

Assembler Programming. Lecture 10

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Giving credit where credit is due

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Stacks and Function Calls

Transcription:

CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing the calling function to continue where it left off Return address and contents of registers Stack: last-in-first-out data structure! Stack frame: args, local vars, return address, registers! Stack pointer: pointing to the current top of the stack Calling functions! CALL and instructions! PUSH into and POP from the stack frame! Using the base pointer as a reference point 1

Function Calls main calls add3! Push arguments on the stack! Push return address on stack! Jump to add3! Allocate local variables on stack, save registers, etc. Return Address int add3(int a, int b, int c) { int d; } d = a + b + c; return d; int main() { int sum, avg; } sum = add3(3, 4, 5); avg = sum / 3; return avg Returning to main! Clear the stack frame for add3! Pop return address from stack for add3 Return Address 3 4 5 for main s High-Level Picture main begins executing main calls P (Extended Stack Pointer) Always points to the top element of the stack Bottom main s 2

High-Level Picture main begins executing main calls P P calls Q Bottom P s main s High-Level Picture main begins executing main calls P P calls Q Q returns Bottom Q s P s main s 3

High-Level Picture main begins executing main calls P P calls Q Q returns P returns Bottom P s main s High-Level Picture main begins executing main calls P P calls Q Q returns P returns main returns Bottom main s 4

High-Level Picture main begins executing main calls P P calls Q Q returns P returns main returns Bottom Function Call Details Call and Return instructions! CALL: push EIP on the stack, and jump to function! : pop the stack into the EIP to go back Function arguments get placed onto the stack before CALL 5

CALL Called function can address arguments relative to : as [+4] before CALL after CALL EIP(caller) : Returning to Caller before EIP(caller) after 6

Base Pointer: As Callee executes, may change! e.g., preparing to call another function How to access arguments then?! Use as fixed reference Need to save old value of! Before overwriting register Callee begins by executing prolog PUSH,, (caller) EIP(caller) Base Pointer: Before returning, Callee must restore to its old value Executes, (caller) EIP(caller) 7

Base Pointer: Before returning, Callee must restore to its old value Executes,, (caller) EIP(caller) Base Pointer: Before returning, Callee must restore to its old value Executes, EIP(caller) 8

Base Pointer: Before returning, Callee must restore to its old value Executes, Allocation for Local Variables Local variables of the Callee are also allocated on the stack Allocation done by moving the stack pointer Example: allocate two integers! SUB, 4! SUB, 4! (or equivalently, SUB, 8) Reference local variables using the base pointer! [-4]! [-8] Var 2 Var 1 (caller) EIP(caller) 9

A Simple Example int sum(int x, int y) { return x+y; } int main () { int result; } result = sum(, ); printf("%d\n", result); return ; Compile: gcc sum.c o xsum Debug: gdb./xsum set disassembly-flavor intel disas main Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 Recall that:! The stack grows from high addresses towards low addresses (os) 1

Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 (os) Recall that:! The stack grows from high addresses towards low addresses (os) Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 (os) (main)

Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 (os) (main) Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 (os) (main) 12

Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 <sum>: PUSH ADD POP, EAX, DWORD PTR [+12] EAX, DWORD PTR [+8] (os) (main) Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], RA(main) DWORD PTR [-4],EAX EAX, ADD, 16 RA(main) <sum>: PUSH ADD POP, EAX, DWORD PTR [+12] EAX, DWORD PTR [+8] (os) (main) 13

Example RA(main) PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 (main) RA(main) <sum>: PUSH ADD POP, EAX, DWORD PTR [+12] EAX, DWORD PTR [+8] (os) (main) Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], RA(main) DWORD PTR [-4],EAX EAX, ADD, 16 (main) RA(main) (sum) <sum>: PUSH ADD POP, EAX, DWORD PTR [+12] EAX, DWORD PTR [+8] (os) (main) 14

Example RA(main) PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 (main) RA(main) (sum) <sum>: PUSH ADD POP, EAX, DWORD PTR [+12] EAX, DWORD PTR [+8] (os) EAX (main) Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], RA(main) DWORD PTR [-4],EAX EAX, ADD, 16 (main) RA(main) (sum) <sum>: PUSH ADD POP, EAX, DWORD PTR [+12] EAX, DWORD PTR [+8] (os) EAX 33 (main) 15

Example RA(main) PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 RA(main) <sum>: PUSH ADD POP, EAX, DWORD PTR [+12] EAX, DWORD PTR [+8] (os) EAX 33 (main) Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 (os) (main) EAX 33 16

Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 33 (os) (main) Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 33 (os) (main) Prepare to: leave EAX 17

Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 (os) (main) EAX Example PUSH, SUB, 16 DWORD PTR [+4], DWORD PTR [], DWORD PTR [-4],EAX EAX, ADD, 16 (os) EAX 18

Summary Invoking a function! CALL: call the function! : return from the instruction for a function call includes! Function arguments! Return address! Local variables! Saved registers Base pointer! Fixed reference point in the! Useful for referencing arguments and local variables 19