Assembly Language: Function Calls" Goals of this Lecture"

Similar documents
Assembly Language: Function Calls" Goals of this Lecture"

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

Assembly Language: Function Calls

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

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)

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

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

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)

CPEG421/621 Tutorial

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

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

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

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

Machine Programming 3: Procedures

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

X86 Assembly -Procedure II:1

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

CSE 351: Week 4. Tom Bergan, TA

Machine-Level Programming III: Procedures

Sungkyunkwan University

Intel assembly language using gcc

Giving credit where credit is due

Machine Program: Procedure. Zhaoguo Wang

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

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.

University of Washington

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

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

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

Process Layout and Function Calls

Digital Forensics Lecture 3 - Reverse Engineering

CPSC W Term 2 Problem Set #3 - Solution

x86 assembly CS449 Fall 2017

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

CPS104 Recitation: Assembly Programming

Lecture 4 CIS 341: COMPILERS

Function Call Convention

Machine-Level Programming II: Control and Arithmetic

W4118: PC Hardware and x86. Junfeng Yang

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

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

Instructor: Alvin R. Lebeck

CS61 Section Solutions 3

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

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

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

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

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

ANITA S SUPER AWESOME RECITATION SLIDES

Process Layout, Function Calls, and the Heap

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

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

x86 Assembly Crash Course Don Porter

x86 Assembly Tutorial COS 318: Fall 2017

CS642: Computer Security

Stack Discipline Jan. 19, 2018

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

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

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

Program Exploitation Intro

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

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

Instruction Set Architecture

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

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

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

Subprograms: Local Variables

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

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

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

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

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

Processes (Intro) Yannis Smaragdakis, U. Athens

Data Representa/ons: IA32 + x86-64

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

Compiler Construction D7011E

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

CS429: Computer Organization and Architecture

Second Part of the Course

Transcription:

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

Function Call Problems" 1. Calling and urning" 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?". Storing local variables" Where does callee function store its local variables?!. Handling registers" How do caller and callee functions use same registers without interference?". Returning a value" How does callee function send urn value back to caller function?" 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" 2

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

Attempted Solution: Use Register" Attempted solution 2: Store urn address in register" P: # Function P movl $Rtn_point1, %eax jmp R # Call R R: # Function R jmp *%eax # Return Rtn_point1: 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-2 Solution: Use the Stack" May need to store many urn addresses" The number of nested functions is not known in advance" A urn address must be saved for as long as the invocation of this function is live, and discarded thereafter" Addresses used in reverse order " E.g., function P calls Q, which then calls R" Then R urns to Q which then urns to P" Last-in-first-out data structure (stack)" Caller pushes urn address on the stack" and callee pops urn address off the stack" EIP for Q EIP for P IA 2 solution: Use the stack via call and " 9 IA-2 Call and Ret Instructions" Ret instruction knows the urn address" P: # Function P call R call Q 1" 2" R: # Function R Q: # Function Q call R 1

IA-2 Call and Ret Instructions" Ret instruction knows the urn address" P: # Function P call R call Q Q: # Function Q call R 6" " " " R: # Function R 11 Implementation of Call" (stack pointer register) points to top of stack" Instruction" pushl src popl dest Effective Operations" subl $, %esp movl src, (%esp) movl (%esp), dest addl $, %esp 12 6

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

Implementation of Ret" Instruction" pushl src popl dest Effective Operations" subl $, %esp movl src, (%esp) movl (%esp), dest Note: can t really access EIP directly, but this is implicitly what is doing. call addr addl $, %esp pushl %eip jmp addr popl %eip before Ret instruction pops stack, thus placing urn address (old EIP) into EIP 1 Implementation of Ret" Instruction" pushl src popl dest call addr Effective Operations" subl $, %esp movl src, (%esp) movl (%esp), dest addl $, %esp pushl %eip jmp addr popl %eip after 16 8

Problem 2: Passing Parameters" Problem: How does caller function pass parameters to callee function?" int add(int a, int b, int c) { int d; d = a + b + c; urn d; int f(void) { urn add(,, ); 17 Attempted Solution: Use Registers" Attempted solution: Pass parameters in registers" " f: movl $, %eax movl $, %ebx movl $, %ecx call add add: # Use EAX, EBX, ECX 18 9

Attempted Solution: Use Registers" Problem: Cannot handle nested function calls" f: movl $, %eax movl $, %ebx movl $, %ecx call add add: movl $6, %eax call g # Use EAX, EBX, ECX # But EAX is corrupted! Also: How to pass parameters that are longer than bytes?" " 19 IA-2 Solution: Use the Stack" Caller pushes parameters before executing the call instruction" before pushing params 2 1

IA-2 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" before call Param 1 Param Param N 21 IA-2 Parameter Passing" Then call the callee" Callee addresses params relative to : Param 1 as (%esp)" " after call Param 1 Param Param N 22 11

IA-2 Parameter Passing" After urning to the caller" after urn Param 1 Param Param N 2 IA-2 Parameter Passing" the caller pops the parameters from the stack" after popping params 2 12

IA-2 Parameter Passing" For example:" " f: # Push parameters pushl $ pushl $ pushl $ call add # Pop parameters addl $12, %esp add: movl (%esp), wherever movl 8(%esp), wherever movl 12(%esp), wherever 2 Base Pointer Register: " 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 a register called to hold what stack pointer was" doesn t move during callee s execution" Use as fixed reference point to access params" " after call Param 1 Param Param N 26 1

Using " Need to save old value of " Before overwriting register" Callee executes prolog " pushl %ebp movl %esp, %ebp Old Param 1 Param Param N 27 Base Pointer Register: " Callee executes prolog " pushl %ebp movl %esp, %ebp Regardless of, callee can reference param 1 as 8(%ebp), param 2 as 12(%ebp), etc.", Old Param 1 Param Param N 28 1

Base Pointer Register: " Before urning, callee must restore and to their old values" Callee executes epilog " movl %ebp, %esp popl %ebp Old Param 1 Param Param N 29 Base Pointer Register: " Callee executes epilog " movl %ebp, %esp popl %ebp, Old Param 1 Param Param N 1

Base Pointer Register: " Callee executes epilog " movl %ebp, %esp popl %ebp Param 1 Param Param N 1 Base Pointer Register: " Callee executes epilog " movl %ebp, %esp popl %ebp Param 1 Param Param N 2 16

Problem : Storing Local Variables" Where does callee function store its local variables?! int add(int a, int b, int c) { int d; d = a + b + c; urn d; int foo(void) { urn add(,, ); IA-2 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 urns" int add(int a, int b, int c) { int d; d = a + b + c; urn d; int foo(void) { urn add(,, ); 17

IA-2 Local Variables" Local variables of the callee are allocated on the stack" Allocation done by moving the stack pointer" Example: allocate memory for two integers" subl $, %esp" subl $, %esp" (or equivalently, subl $8, %esp)" Reference local variables as negative offsets relative to " -(%ebp)" -8(%ebp)" Var 2 Var 1 Old Param 1 Param Param N IA-2 Local Variables" For example:" add: # Allocate space for d subl $, %esp # Initialize d movl whatever, -(%ebp) " 6 18

Problem : 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 registers" In reality: All functions share same small set of registers" Callee may use register that the caller also is using" When callee urns control to caller, old register contents may have been lost" Caller function cannot continue where it left off" 7 IA-2 Solution: Use the Stack" Save the registers on the stack" Someone must save old register contents " Someone must later restore the register contents" " Define a convention for who (caller or callee) saves and restores which registers" 8 19

IA-2 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" 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" Var 2 Var 1 Saved EBX, ESI,EDI Old Param 1 Param Param N Saved EAX, EDX,ECX 9 Problem : Return Values" Problem: How does callee function send urn value back to caller function?" In principle:" Store urn value in stack frame of caller" Or, for efficiency:" Known small size => store urn value in register" Other => store urn value in stack" int add(int a, int b, int c) { int d; d = a + b + c; urn d; int foo(void) { urn add(,, ); 2

IA-2 Return Values" IA-2 Convention:" Integral type or pointer:" Store urn value in EAX" char, short, int, long, pointer" Floating-point type:" Store urn value in floatingpoint register" (Beyond scope of COS 217)" Structure:" Store urn value on stack" (Beyond scope of COS 217)" int add(int a, int b, int c) { int d; d = a + b + c; urn d; int foo(void) { urn add(,, ); 1 Stack Frames" Summary of IA-2 function handling:" Stack has one stack frame per active function invocation" points to top (low memory) of current stack frame" points to bottom (high memory) of current stack frame" Stack frame contains:" Return address ()" Old " Saved register values" Local variables" Parameters to be passed to callee function" 2 21

A Simple Example" int add(int a, int b, int c) { int d; d = a + b + c; urn d; /* In some calling function */ x = add(,, ); Trace of a Simple Example 1" x = add(,, ); 22

Trace of a Simple Example 2" x = add(,, ); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx Old EDX Old ECX Old EAX Trace of a Simple Example " x = add(,, ); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl $ pushl $ pushl $ Old EDX Old ECX Old EAX 6 2

Trace of a Simple Example " x = add(,, ); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl $ pushl $ pushl $ # Call add call add Old EDX Old ECX Old EAX 7 Trace of a Simple Example " int add(int a, int b, int c) { int d; d = a + b + c; urn d; # Save old pushl %ebp Prolog Old Old EDX Old ECX Old EAX 8 2

Trace of a Simple Example 6" int add(int a, int b, int c) { int d; d = a + b + c; urn d; # Save old pushl %ebp # Change movl %esp, %ebp Prolog Old Old EDX Old ECX Old EAX 9 Trace of a Simple Example 7" int add(int a, int b, int c) { int d; d = a + b + c; urn d; # Save old pushl %ebp # Change movl %esp, %ebp # Save callee-save registers if necessary pushl %ebx pushl %esi pushl %edi Unnecessary here; add will not change the values in these registers Old EDI Old ESI Old EBX Old Old EDX Old ECX Old EAX 2

Trace of a Simple Example 8" int add(int a, int b, int c) { int d; d = a + b + c; urn d; # Save old pushl %ebp # Change movl %esp, %ebp # Save caller-save registers if necessary pushl %ebx pushl %esi pushl %edi # Allocate space for local variable subl $, %esp Old EDI Old ESI Old EBX Old Old EDX Old ECX Old EAX 1 Trace of a Simple Example 9" int add(int a, int b, int c) { int d; d = a + b + c; urn d; # Save old pushl %ebp # Change movl %esp, %ebp # Save caller-save registers if necessary pushl %ebx pushl %esi pushl %edi # Allocate space for local variable subl $, %esp # Perform the addition movl 8(%ebp), %eax addl 12(%ebp), %eax addl 16(%ebp), %eax movl %eax, -16(%ebp) Access params as positive offsets relative to Access local vars as negative offsets relative to 12 Old EDI Old ESI Old EBX Old Old EDX Old ECX Old EAX 2 26

Trace of a Simple Example 1" int add(int a, int b, int c) { int d; d = a + b + c; urn d; # Copy the urn value to EAX movl -16(%ebp), %eax # Restore callee-save registers if necessary movl -12(%ebp), %edi movl -8(%ebp), %esi movl -(%ebp), %ebx 12 Old EDI Old ESI Old EBX Old Old EDX Old ECX Old EAX Trace of a Simple Example 11" int add(int a, int b, int c) { int d; d = a + b + c; urn d; # Copy the urn value to EAX movl -16(%ebp), %eax # Restore callee-save registers if necessary movl -12(%ebp), %edi movl -8(%ebp), %esi movl -(%ebp), %ebx # Restore movl %ebp, %esp Epilog 12 Old EDI Old ESI Old EBX Old Old EDX Old ECX Old EAX 27

Trace of a Simple Example 12" int add(int a, int b, int c) { int d; d = a + b + c; urn d; # Copy the urn value to EAX movl -16(%ebp), %eax # Restore callee-save registers if necessary movl -12(%ebp), %edi movl -8(%ebp), %esi movl -(%ebp), %ebx # Restore movl %ebp, %esp # Restore Epilog popl %ebp 12 Old EDI Old ESI Old EBX Old Old EDX Old ECX Old EAX Trace of a Simple Example 1" int add(int a, int b, int c) { int d; d = a + b + c; urn d; # Copy the urn value to EAX movl -16(%ebp), %eax # Restore callee-save registers if necessary movl -12(%ebp), %edi movl -8(%ebp), %esi movl -(%ebp), %ebx # Restore movl %ebp, %esp # Restore popl %ebp # Return to calling function 12 Old EDI Old ESI Old EBX Old Old EDX Old ECX Old EAX 6 28

Trace of a Simple Example 1" x = add(,, ); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl $ pushl $ pushl $ # Call add call add # Pop parameters addl $12, %esp 12 Old EDI Old ESI Old EBX Old Old EDX Old ECX Old EAX 7 Trace of a Simple Example 1" x = add(,, ); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl $ pushl $ pushl $ # Call add call add # Pop parameters addl %12, %esp # Save urn value movl %eax, wherever 12 Old EDI Old ESI Old EBX Old Old EDX Old ECX Old EAX 8 29

Trace of a Simple Example 16" x = add(,, ); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl $ pushl $ pushl $ # Call add call add # Pop parameters addl %12, %esp # Save urn value movl %eax, wherever # Restore caller-save registers if necessary popl %edx popl %ecx popl %eax 12 Old EDI Old ESI Old EBX Old Old EDX Old ECX Old EAX 9 Trace of a Simple Example 17" x = add(,, ); # Save caller-save registers if necessary pushl %eax pushl %ecx pushl %edx # Push parameters pushl $ pushl $ pushl $ # Call add call add # Pop parameters addl %12, %esp # Save urn value movl %eax, wherever # Restore caller-save registers if necessary popl %edx popl %ecx popl %eax # Proceed! 6

Summary" Calling and urning" 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 " Caller pops from stack" 61 Summary (cont.)" Storing local variables" Callee pushes on stack" Callee accesses as negative offsets from " 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 urns data of integral types and pointers in EAX" 62 1