Systems I. Machine-Level Programming V: Procedures

Similar documents
CS213. Machine-Level Programming III: Procedures

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

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

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

X86 Assembly -Procedure II:1

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

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

An Experience Like No Other. Stack Discipline Aug. 30, 2006

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

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing

Stack Discipline Jan. 19, 2018

Machine-level Programming (3)

Giving credit where credit is due

Machine-Level Programming III: Procedures

Sungkyunkwan University

Machine- Level Programming III: Switch Statements and IA32 Procedures

University of Washington

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

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

Assembly Language: Function Calls

Machine Programming 3: Procedures

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls" Goals of this Lecture"

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

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

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

Machine-Level Programming III: Procedures

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

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

Machine-level Programs Procedure

CS429: Computer Organization and Architecture

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

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

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

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

Machine- Level Programming III: Switch Statements and IA32 Procedures

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

Page 1. IA32 Stack CISC 360. Machine-Level Programming III: Procedures Sept. 22, IA32 Stack Popping Stack Bottom. IA32 Stack Pushing

CIT Week13 Lecture

This is a medical robot, guided by a skilled surgeon and designed to get to places doctors are unable to reach without opening a pacent up.

CS241 Computer Organization Spring 2015 IA

x86 assembly CS449 Fall 2017

X86 Stack Calling Function POV

Machine Program: Procedure. Zhaoguo Wang

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

CS429: Computer Organization and Architecture

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

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

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

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

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

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

ANITA S SUPER AWESOME RECITATION SLIDES

W4118: PC Hardware and x86. Junfeng Yang

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

Machine- Level Representa2on: Procedure

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

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

x86 Assembly Tutorial COS 318: Fall 2017

CSE 351: Week 4. Tom Bergan, TA

The Stack & Procedures

CPEG421/621 Tutorial

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

Machine-Level Programming (2)

Lecture 4 CIS 341: COMPILERS

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

Practical Malware Analysis

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

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Assembly I: Basic Operations. Jo, Heeseung

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

Instruction Set Architecture

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Machine-Level Programming I: Introduction Jan. 30, 2001

Instruction Set Architecture

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

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

Giving credit where credit is due

Course Administration

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

Program Exploitation Intro

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Subprograms, Subroutines, and Functions

Concepts Introduced in Chapter 7

Machine Programming 1: Introduction

Data Representa/ons: IA32 + x86-64

211: Computer Architecture Summer 2016

Page # CISC 360. Machine-Level Programming I: Introduction Sept. 18, IA32 Processors. X86 Evolution: Programmerʼs View.

System Software Assignment 1 Runtime Support for Procedures

x86 architecture et similia

Compiling Code, Procedures and Stacks

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

Procedure-Calling Conventions October 30

CISC 360. Machine-Level Programming I: Introduction Sept. 18, 2008

CMSC 313 Lecture 12 [draft] How C functions pass parameters

CISC 360 Instruction Set Architecture

Today: Machine Programming I: Basics

Chapter 4 Processor Architecture: Y86 (Sections 4.1 & 4.3) with material from Dr. Bin Ren, College of William & Mary

Transcription:

Systems I Machine-Level Programming V: Procedures Topics abstraction and implementation IA32 stack discipline

Procedural Memory Usage void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Where is the memory that holds t0 and t1 (or local variables in general)? What happens if we run out of registers (x86 only has 8!)? Where are parameters passed from callers to callee? Registers? Memory? What memory? 2

Data Structure LIFO data structure Last In, First Out Allocated somewhere in memory Where doesnʼt really matter as long as we store the stack pointer By convention, stack grows toward smaller addresses Could do it either way Values within the stack are referenced relative to the stack pointer Addr xffffffff Bottom Increasing Addresses Grows Down Top Addr x00000000 3

IA32 Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address address of top element Bottom Increasing Addresses Grows Down Top 4

IA32 Pushing Pushing pushl Src Bottom Fetch operand at Src Decrement by 4 Write operand at address given by Increasing Addresses -4 Grows Down Top 5

IA32 Popping Popping popl Dest Read operand at address given by Increment by 4 Bottom Increasing Addresses Write to Dest +4 Grows Down Top 6

Operation Examples pushl %eax popl %edx 0x110 0x110 0x110 0x10c 0x10c 0x10c 0x108 123 0x108 123 0x108 123 0x104 213 0x104 213 %eax 213 %eax 213 %eax 213 %edx 555 %edx 555 %edx 555 213 0x108 0x108 0x104 0x104 0x108 7

What Elements for Procedures? Method of computing address of first instruction of called procedure. Place to store passed parameters. Call by value or by reference Method of computer return address Need to come back to first instruction after point of procedure call Method of passing return value(s) back 8

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 value Address of instruction beyond call Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax Return address = 0x8048553 Procedure return: ret Pop address from stack; Jump to address 9

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 0x108 0x108 0x104 %eip 0x804854e %eip 0x804854e 0x8048b90 %eip is program counter 10

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

-Based Languages Languages that Support Recursion e.g., C, Pascal, Java Code must be Reentrant Multiple simultaneous instantiations of single procedure Need some place to store state of each instantiation Arguments Local variables Return pointer Discipline State for given procedure needed for limited time From when called to when return Callee returns before caller does Allocated in Frames state for single procedure instantiation 12

Call Chain Example Code Structure ( ) (); } Procedure recursive ( ) (); (); } ( ) (); } Call Chain 13

Frames Contents Local variables Return information Temporary space Management Space allocated when enter procedure Set-up code Deallocated when return Finish code s pointer indicates stack top Frame %ebp proc Top Frame pointer %ebp indicates start of current frame 14

Operation ( ) (); } Call Chain Frame %ebp 15

Operation ( ) (); (); } Call Chain Frame %ebp 16

Operation ( ) (); } Call Chain Frame %ebp 17

Operation ( ) (); } Call Chain Frame %ebp 18

Operation ( ) (); } Call Chain Frame %ebp 19

Operation ( ) (); } Call Chain Frame %ebp 20

Operation ( ) (); } Call Chain Frame %ebp 21

Operation Call Chain ( ) (); (); } Frame %ebp 22

Operation ( ) } Call Chain Frame %ebp 23

Operation Call Chain ( ) (); (); } Frame %ebp 24

Operation ( ) (); } Call Chain Frame %ebp 25

Summary Today Basic stack organization and access Activation records (stack frames) Call chains Next time Detailed example of calls and stack state Register saving conventions Recursion 26