Assembly Language: Function Calls

Similar documents
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"

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

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

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

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

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

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

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

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

Assembly Language: Function Calls

Machine-level Programming (3)

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

CS213. Machine-Level Programming III: Procedures

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

CPEG421/621 Tutorial

X86 Stack Calling Function POV

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

Systems I. Machine-Level Programming V: Procedures

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

CIT Week13 Lecture

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

CS241 Computer Organization Spring 2015 IA

Machine Programming 3: Procedures

X86 Assembly -Procedure II:1

CSE 351: Week 4. Tom Bergan, TA

Register Allocation, iii. Bringing in functions & using spilling & coalescing

Machine-Level Programming III: Procedures

Sungkyunkwan University

Intel assembly language using gcc

Machine Program: Procedure. Zhaoguo Wang

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

Sistemi Operativi. Lez. 16 Elementi del linguaggio Assembler AT&T

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

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Digital Forensics Lecture 3 - Reverse Engineering

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

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

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

x86 assembly CS449 Fall 2017

CPS104 Recitation: Assembly Programming

Giving credit where credit is due

Lecture 4 CIS 341: COMPILERS

University of Washington

Function Call Convention

Machine-Level Programming II: Control and Arithmetic

W4118: PC Hardware and x86. Junfeng Yang

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

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

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

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

Instructor: Alvin R. Lebeck

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

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

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

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

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

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Process Layout and Function Calls

Process Layout, Function Calls, and the Heap

COMP 210 Example Question Exam 2 (Solutions at the bottom)

CPSC W Term 2 Problem Set #3 - Solution

x86 Assembly Crash Course Don Porter

x86 Assembly Tutorial COS 318: Fall 2017

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

ANITA S SUPER AWESOME RECITATION SLIDES

CS61 Section Solutions 3

Stack Discipline Jan. 19, 2018

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Program Exploitation Intro

CS642: Computer Security

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

Assembly I: Basic Operations. Jo, Heeseung

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

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Instruction Set Architecture

Assignment 11: functions, calling conventions, and the stack

Lab 10: Introduction to x86 Assembly

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

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

You may work with a partner on this quiz; both of you must submit your answers.

Machine- Level Programming III: Switch Statements and IA32 Procedures

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

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

mith College Computer Science CSC231 Assembly Week #12 Thanksgiving 2017 Dominique Thiébaut

Processes (Intro) Yannis Smaragdakis, U. Athens

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

Subprograms: Local Variables

Compiler Construction D7011E

Second Part of the Course

Assembly Language: IA-32 Instructions

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Instruction Set Architecture

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

Transcription:

Assembly Language: Function Calls 1

Goals of this Lecture Help you learn: Function call problems: Calling and returning Passing parameters Storing local variables Handling registers without interference Returning values IA-32 solutions to those problems Pertinent instructions and conventions 2

Function Call Problems 1. Calling and returning How does caller function jump to callee function? How does callee function jump back to the right place in caller function? 2. Passing parameters How does caller function pass parameters to callee function? 3. Storing local variables Where does callee function store its local variables? 4. Handling registers How do caller and callee functions use same registers without interference? 5. Returning a value How does callee function send return value back to caller function? 3

Problem 1: Calling and Returning How does caller function jump to callee function? I.e., Jump to the address of the callee s first instruction How does the callee function jump back to the right place in caller function? I.e., Jump to the instruction immediately following the most-recently-executed call instruction 4

Attempted Solution: Use Jmp Instruction Attempted solution: caller and callee use jmp instruction P: # Function P jmp R Rtn_point1: # Call R R: # Function R jmp Rtn_point1 # Return 5

Attempted Solution: Use Jmp Instruction Problem: callee may be called by multiple callers P: # Function P R: # Function R jmp R # Call R jmp??? # Return Rtn_point1: Q: # Function Q jmp R Rtn_point2: # Call R 6

Attempted Solution: Use Register Attempted solution 2: Store return address in register P: # Function P movl $Rtn_point1, %eax jmp R # Call R Rtn_point1: R: # Function R jmp *%eax # Return Q: # Function Q movl $Rtn_point2, %eax jmp R # Call R Rtn_point2: Special form of jmp instruction; we will not use 7

Attempted Solution: Use Register Problem: Cannot handle nested function calls P: # Function P movl $Rtn_point1, %eax jmp Q # Call Q Rtn_point1: R: # Function R jmp *%eax # Return Q: # Function Q movl $Rtn_point2, %eax jmp R # Call R Rtn_point2: jmp *%eax # Return Problem if P calls Q, and Q calls R Return address for P to Q call is lost 8

IA-32 Solution: Use the Stack May need to store many return addresses The number of nested functions is not known in advance A return address must be saved for as long as the function invocation continues, and discarded thereafter Addresses used in reverse order E.g., function P calls Q, which then calls R Then R returns to Q which then returns to P Last-in-first-out data structure (stack) EIP for P Caller pushes return address on the stack and callee pops return address off the stack IA 32 solution: Use the stack via call and ret EIP for Q 9

IA-32 Call and Ret Instructions Ret instruction knows the return address P: # Function P call R call Q 1 2 R: # Function R ret Q: # Function Q call R ret 10

IA-32 Call and Ret Instructions Ret instruction knows the return address P: # Function P call R call Q R: # Function R ret 3 4 5 Q: # Function Q call R 6 ret 11

Implementation of Call (stack pointer register) points to top of stack 0 Instruction pushl src popl dest Effective Operations subl $4, %esp movl src, (%esp) movl (%esp), dest addl $4, %esp 12

pushl src popl dest Implementation of Call EIP (instruction pointer register) points to next instruction to be 0 executed Note: can t really access EIP Instruction Effective Operations directly, but this is implicitly what call is doing call addr subl $4, %esp movl src, (%esp) movl (%esp), dest addl $4, %esp pushl %eip jmp addr Call instruction pushes return address (old EIP) onto stack before call 13

Implementation of Call 0 Instruction Effective Operations pushl src subl $4, %esp movl src, (%esp) popl dest movl (%esp), dest addl $4, %esp call addr pushl %eip jmp addr after call Old EIP 14

Implementation of Ret 0 Instruction pushl src popl dest call addr ret Effective Operations subl $4, %esp movl src, (%esp) movl (%esp), dest addl $4, %esp pushl %eip jmp addr popl %eip Ret instruction pops stack, thus placing return address (old EIP) into EIP Note: can t really access EIP directly, but this is implicitly what ret is doing. before ret Old EIP 15

Implementation of Ret 0 Instruction pushl src popl dest call addr ret Effective Operations subl $4, %esp movl src, (%esp) movl (%esp), dest addl $4, %esp pushl %eip jmp addr popl %eip after ret 16

Problem 2: Passing Parameters Problem: How does caller function pass parameters to callee function? int add3(int a, int b, int c) { int d; d = a + b + c; return d; } int f(void) { return add3(3, 4, 5); } 17

Attempted Solution: Use Registers Attempted solution: Pass parameters in registers f: movl $3, %eax movl $4, %ebx movl $5, %ecx call add3 add3: # Use EAX, EBX, ECX ret 18

Attempted Solution: Use Registers Problem: Cannot handle nested function calls f: add3: movl $3, %eax movl $4, %ebx movl $5, %ecx call add3 movl $6, %eax call g # Use EAX, EBX, ECX # But EAX is corrupted! Also: How to pass parameters that are longer than 4 bytes? ret 19

IA-32 Solution: Use the Stack Caller pushes parameters before executing the call instruction 0 before pushing params 20

IA-32 Parameter Passing Caller pushes parameters in the reverse order Push N th param first Push 1 st param last So first param is at top of the stack at the time of the Call 0 before call Param 1 Param Param N 21

IA-32 Parameter Passing Callee addresses params relative to : Param 1 as 4(%esp) 0 after call Old EIP Param 1 Param Param N 22

IA-32 Parameter Passing After returning to the caller 0 after return Param 1 Param Param N 23

IA-32 Parameter Passing the caller pops the parameters from the stack 0 after popping params 24

IA-32 Parameter Passing For example: f: # Push parameters pushl $5 pushl $4 pushl $3 call add3 # Pop parameters addl $12, %esp add3: movl 4(%esp), wherever movl 8(%esp), wherever movl 12(%esp), wherever ret 25

Base Pointer Register: EBP Problem: As callee executes, may change E.g., preparing to call another function Error-prone for callee to reference params as offsets relative to Solution: Use EBP as fixed reference point to access params after call 0 Old EIP Param 1 Param Param N EBP 26

Using EBP Need to save old value of EBP Before overwriting EBP register 0 Callee executes prolog pushl %ebp movl %esp, %ebp Old EBP Old EIP Param 1 Param Param N EBP 27

Base Pointer Register: EBP Callee executes prolog pushl %ebp 0 movl %esp, %ebp Regardless of, callee can reference param 1 as 8(%ebp), param 2 as 12(%ebp), etc., EBP Old EBP Old EIP Param 1 Param Param N 28

Base Pointer Register: EBP Before returning, callee must restore and EBP to their old values 0 Callee executes epilog movl %ebp, %esp popl %ebp ret EBP Old EBP Old EIP Param 1 Param Param N 29

Base Pointer Register: EBP Callee executes epilog movl %ebp, %esp 0 popl %ebp ret, EBP Old EBP Old EIP Param 1 Param Param N 30

Base Pointer Register: EBP Callee executes epilog movl %ebp, %esp 0 popl %ebp ret Old EIP Param 1 Param Param N EBP 31

Base Pointer Register: EBP Callee executes epilog movl %ebp, %esp 0 popl %ebp ret Param 1 Param Param N EBP 32

Problem 3: Storing Local Variables Where does callee function store its local variables? int add3(int a, int b, int c) { int d; d = a + b + c; return d; } int foo(void) { return add3(3, 4, 5); } 33

IA-32 Solution: Use the Stack Local variables: Short-lived, so don t need a permanent location in memory Size known in advance, so don t need to allocate on the heap So, the function just uses the top of the stack Store local variables on the top of the stack The local variables disappear after the function returns int add3(int a, int b, int c) { int d; d = a + b + c; return d; } int foo(void) { return add3(3, 4, 5); } 34

IA-32 Local Variables Local variables of the callee are allocated on the stack 0 Allocation done by moving the stack pointer Example: allocate memory for two integers subl $4, %esp subl $4, %esp (or equivalently, subl $8, %esp) Reference local variables as negative offsets relative to EBP -4(%ebp) -8(%ebp) EBP Var 2 Var 1 Old EBP Old EIP Param 1 Param Param N 35

IA-32 Local Variables For example: add3: # Allocate space for d subl $4, %esp # Access d movl whatever, -4(%ebp) ret 36

Problem 4: Handling Registers Problem: How do caller and callee functions use same registers without interference? Registers are a finite resource! In principle: Each function should have its own set of registers In reality: All functions must use the same small set of registers Callee may use a register that the caller also is using When callee returns control to caller, old register contents may be lost Caller function cannot continue where it left off 37

IA-32 Solution: Define a Convention IA-32 solution: save the registers on the stack Someone must save old register contents Someone must later restore the register contents Define a convention for who saves and restores which registers 38

IA-32 Register Handling Caller-save registers EAX, EDX, ECX If necessary Caller saves on stack before call Caller restores from stack after call Callee-save registers EBX, ESI, EDI If necessary EBP Callee saves on stack after prolog Callee restores from stack before epilog Caller can assume that values in EBX, ESI, EDI will not be changed by callee 0 Var 2 Var 1 Saved EBX, ESI,EDI Old EBP Old EIP Param 1 Param Param N Saved EAX, EDX,ECX 39

Problem 5: Return Values Problem: How does callee function send return value back to caller function? In principle: Store return value in stack frame of caller Or, for efficiency: Known small size => store return value in register Other => store int add3(int a, int b, int c) { int d; d = a + b + c; return d; } int foo(void) { return add3(3, 4, 5); } return value in stack 40

IA-32 Return Values IA-32 Convention: Integral type or pointer: Store return value in EAX char, short, int, long, pointer Floating-point type: Store return value in floating-point register (Beyond scope of course) Structure: Store return value on stack (Beyond scope of course) int add3(int a, int b, int c) { int d; d = a + b + c; return d; } int foo(void) { return add3(3, 4, 5); } 41

Stack Frames Summary of IA-32 function handling: Stack has one stack frame per active function invocation points to top (low memory) of current stack frame EBP points to bottom (high memory) of current stack frame Stack frame contains: Old EBP Saved register values Local variables Parameters to be passed to callee function Return address (Old EIP) 42

A Simple Example int add3(int a, int b, int c) { int d; d = a + b + c; return d; } /* In some calling function */ x = add3(3, 4, 5); 43

Trace of a Simple Example 1 x = add3(3, 4, 5); Low memory EBP High memory 44

Trace of a Simple Example 2 x = add3(3, 4, 5); Low memory # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx Old EDX Old ECX Old EAX EBP High memory 45

Trace of a Simple Example 3 x = add3(3, 4, 5); Low memory # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl $5 pushl $4 pushl $3 3 4 5 Old EDX Old ECX Old EAX EBP High memory 46

Trace of a Simple Example 4 x = add3(3, 4, 5); Low memory # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl $5 pushl $4 pushl $3 # Call add3 call add3 Old EIP 3 4 5 Old EDX Old ECX Old EAX EBP High memory 47

Trace of a Simple Example 5 int add3(int a, int b, int c) { int d; d = a + b + c; return d; } Low memory # Save old EBP pushl %ebp Prolog Old EBP Old EIP 3 4 5 Old EDX Old ECX Old EAX EBP High memory 48

Trace of a Simple Example 6 int add3(int a, int b, int c) { int d; d = a + b + c; return d; } Low memory # Save old EBP pushl %ebp # Change EBP movl %esp, %ebp Prolog EBP Old EBP Old EIP 3 4 5 Old EDX Old ECX Old EAX High memory 49

Trace of a Simple Example 7 int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Save old EBP pushl %ebp # Change EBP movl %esp, %ebp # Save caller-save registers if necessary pushl %ebx pushl %esi pushl %edi Unnecessary here; add3 will not change the values in these registers Low memory EBP Old EDI Old ESI Old EBX Old EBP Old EIP 3 4 5 Old EDX Old ECX Old EAX High memory 50

Trace of a Simple Example 8 int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Save old EBP pushl %ebp # Change EBP movl %esp, %ebp # Save caller-save registers if necessary pushl %ebx pushl %esi pushl %edi # Allocate space for local variable subl $4, %esp Low memory EBP Old EDI Old ESI Old EBX Old EBP Old EIP 3 4 5 Old EDX Old ECX Old EAX High memory 51

Trace of a Simple Example 9 int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Save old EBP pushl %ebp # Change EBP movl %esp, %ebp # Save caller-save registers if necessary pushl %ebx pushl %esi pushl %edi # Allocate space for local variable subl $4, %esp # Perform the addition movl 8(%ebp), %eax addl 12(%ebp), %eax addl 16(%ebp), %eax movl %eax, -16(%ebp) Low memory EBP Access params as positive offsets relative to EBP Access local vars as negative offsets relative to EBP High memory 12 Old EDI Old ESI Old EBX Old EBP Old EIP 3 4 5 Old EDX Old ECX Old EAX 52

Trace of a Simple Example 10 int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Copy the return value to EAX movl -16(%ebp), %eax # Restore callee-save registers if necessary movl -12(%ebp), %edi movl -8(%ebp), %esi movl -4(%ebp), %ebx Low memory EBP 12 Old EDI Old ESI Old EBX Old EBP Old EIP 3 4 5 Old EDX Old ECX Old EAX High memory 53

Trace of a Simple Example 11 int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Copy the return value to EAX movl -16(%ebp), %eax # Restore callee-save registers if necessary movl -12(%ebp), %edi movl -8(%ebp), %esi movl -4(%ebp), %ebx # Restore movl %ebp, %esp Epilog Low memory EBP 12 Old EDI Old ESI Old EBX Old EBP Old EIP 3 4 5 Old EDX Old ECX Old EAX High memory 54

Trace of a Simple Example 12 int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Copy the return value to EAX movl -16(%ebp), %eax # Restore callee-save registers if necessary movl -12(%ebp), %edi movl -8(%ebp), %esi movl -4(%ebp), %ebx # Restore movl %ebp, %esp # Restore EBP Epilog popl %ebp Low memory 12 Old EDI Old ESI Old EBX Old EBP Old EIP 3 4 5 Old EDX Old ECX Old EAX EBP High memory 55

Trace of a Simple Example 13 int add3(int a, int b, int c) { int d; d = a + b + c; return d; } # Copy the return value to EAX movl -16(%ebp), %eax # Restore callee-save registers if necessary movl -12(%ebp), %edi movl -8(%ebp), %esi movl -4(%ebp), %ebx # Restore movl %ebp, %esp # Restore EBP popl %ebp # Return to calling function ret Low memory 12 Old EDI Old ESI Old EBX Old EBP Old EIP 3 4 5 Old EDX Old ECX Old EAX EBP High memory 56

Trace of a Simple Example 14 x = add3(3, 4, 5); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl $5 pushl $4 pushl $3 # Call add3 call add3 # Pop parameters addl $12, %esp Low memory 12 Old EDI Old ESI Old EBX Old EBP Old EIP 3 4 5 Old EDX Old ECX Old EAX EBP High memory 57

Trace of a Simple Example 15 x = add3(3, 4, 5); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl $5 pushl $4 pushl $3 # Call add3 call add3 # Pop parameters addl %12, %esp # Save return value movl %eax, wherever Low memory 12 Old EDI Old ESI Old EBX Old EBP Old EIP 3 4 5 Old EDX Old ECX Old EAX EBP High memory 58

Trace of a Simple Example 16 x = add3(3, 4, 5); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl $5 pushl $4 pushl $3 # Call add3 call add3 # Pop parameters addl %12, %esp # Save return value movl %eax, wherever # Restore caller-save registers if necessary popl %edx popl %ecx popl %eax Low memory EBP High memory 12 Old EDI Old ESI Old EBX Old EBP Old EIP 3 4 5 Old EDX Old ECX Old EAX 59

Trace of a Simple Example 17 x = add3(3, 4, 5); Low memory # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl $5 pushl $4 pushl $3 # Call add3 call add3 # Pop parameters addl %12, %esp # Save return value movl %eax, wherever # Restore caller-save registers if necessary popl %edx popl %ecx popl %eax # Proceed! EBP High memory 60

Summary Calling and returning Call instruction: push EIP onto stack and jump Ret instruction: pop stack to EIP Passing parameters Caller pushes onto stack Callee accesses as positive offsets from EBP Caller pops from stack 61

Summary (cont.) Storing local variables Callee pushes on stack Callee accesses as negative offsets from EBP Callee pops from stack Handling registers Caller saves and restores EAX, ECX, EDX if necessary Callee saves and restores EBX, ESI, EDI if necessary Returning values Callee returns data of integral types and pointers in EAX 62