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

Similar documents
Assembly Language: Function Calls

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"

CSC 2400: Computing Systems. X86 Assembly: Function Calls

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

X86 Stack Calling Function POV

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

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

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

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

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

Machine-level Programming (3)

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

CS213. Machine-Level Programming III: Procedures

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

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

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: IA-32 Instructions

Instructor: Alvin R. Lebeck

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

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

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

x86 assembly CS449 Fall 2017

Second Part of the Course

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

Process Layout and Function Calls

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

CIT Week13 Lecture

Systems I. Machine-Level Programming V: Procedures

Machine Programming 3: Procedures

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

CS61 Section Solutions 3

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

CS241 Computer Organization Spring 2015 IA

CPEG421/621 Tutorial

X86 Assembly -Procedure II:1

Digital Forensics Lecture 3 - Reverse Engineering

Sungkyunkwan University

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

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

Machine-Level Programming III: Procedures

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

System Programming and Computer Architecture (Fall 2009)

x86 Assembly Tutorial COS 318: Fall 2017

Lab 10: Introduction to x86 Assembly

Intel assembly language using gcc

Compiler Construction D7011E

W4118: PC Hardware and x86. Junfeng Yang

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

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

Machine-Level Programming II: Control and Arithmetic

CSC 8400: Computer Systems. Machine-Level Representation of Programs

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

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

Assembly Language: Overview!

Process Layout, Function Calls, and the Heap

CPS104 Recitation: Assembly Programming

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

CPSC W Term 2 Problem Set #3 - Solution

CS429: Computer Organization and Architecture

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

CSE 351: Week 4. Tom Bergan, TA

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

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Instruction Set Architecture

Program Exploitation Intro

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

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

Towards the Hardware"

Machine Program: Procedure. Zhaoguo Wang

CS642: Computer Security

Goals of Today s Lecture

Instruction Set Architecture

Computer Architecture and Assembly Language. Prof. David August COS 217

Instruction Set Architecture

An Introduction to x86 ASM

Stack Discipline Jan. 19, 2018

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

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

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

ECE 391 Exam 1 Review Session - Spring Brought to you by HKN

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

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

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

University of Washington

Assembly Language: Overview

Machine- Level Programming III: Switch Statements and IA32 Procedures

Giving credit where credit is due

Lecture 4 CIS 341: COMPILERS

Assembly Language: Function Calls

Assignment 11: functions, calling conventions, and the stack

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

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

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

x86 architecture et similia

Credits to Randy Bryant & Dave O Hallaron

Transcription:

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

Goals of Today s Lecture Finishing introduction to assembly language o EFLAGS register and conditional jumps o Addressing modes Memory layout of the UNIX process o Data, BSS, rodata, Text o Stack frames, and the stack pointer ESP Calling functions o Call and ret commands o Placing arguments on the stack o Using the base pointer EBP 2

Detailed Example movl n %edx count %ecx $, %ecx count=; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; }.loop: cmpl jle addl movl andl je movl addl addl addl jmp.else: sarl.endif: jmp.endloop: $1, %edx.endloop $1, %ecx %edx, %eax $1, %eax.else %edx, %eax %eax, %edx %eax, %edx $1, %edx.endif $1, %edx.loop 3

Setting the EFLAGS Register Comparison cmpl compares two integers o Done by subtracting the first number from the second Discarding the results, but setting the eflags register o Example: cmpl $1, %edx (computes %edx 1) jle.endloop (looks at the sign flag and the zero flag) Logical operation andl compares two integers o Example: andl $1, %eax (bit-wise AND of %eax with 1) je.else (looks at the zero flag) Unconditional branch jmp o Example: jmp.endif and jmp.loop 4

EFLAGS Register & Condition Codes 31 22 Reserved (set to ) 21 2 19 18 17 16 15 14 13 12 11 1 I V V A V R N IO I IF PL O D D P C M F T F F 9 I F 8 T F 7 S F 6 Z F 5 4 A F 3 2 P F 1 1 C F Identification flag Virtual interrupt pending Virtual interrupt flag Alignment check Virtual 886 mode Resume flag Nested task flag I/O privilege level Overflow flag Direction flag Interrupt enable flag Trap flag Sign flag Zero flag Auxiliary carry flag or adjust flag Parity flag Carry flag 5

Data Access Methods Immediate addressing: data stored in the instruction itself o movl $1, %ecx Register addressing: data stored in a register o movl %eax, %ecx Direct addressing: address stored in instruction o movl 2, %ecx Indirect addressing: address stored in a register o movl (%eax), %ebx Base pointer addressing: includes an offset as well o movl 4(%eax), %ebx Indexed addressing: instruction contains base address, and specifies an index register and a multiplier (1, 2, or 4) o movl 2(,%ecx,1), %ebx 6

Effective Address Offset = eax ebx ecx edx esp ebp esi edi eax ebx ecx 1 edx 2 + esp * 3 + ebp esi 4 edi None 8-bit 16-bit 32-bit Base Index scale displacement Displacement Base Base + displacement (Index * scale) + displacement Base + (index * scale) + displace. movl foo, %eax movl (%eax), %ebx movl foo(%eax), %ebx movl 1(%eax), %ebx movl (,%eax,4), %ebx movl foo(,%eax,4), %ebx 7

A Simple Assembly Program.section.data # pre-initialized # variables go here.section.bss # zero-initialized # variables go here.section.rodata # pre-initialized # constants go here.section.text.globl _start _start: # Program starts executing # here # Body of the program goes # here # Program ends with an # exit() system call # to the operating system movl $1, %eax movl $, %ebx int $x8 8

Main Parts of the Program Break program into sections (.section) o Data, BSS, RoData, and Text Starting the program o Making _start a global (.global _start) Tells the assembler to remember the symbol _start because the linker will need it o Identifying the start of the program (_start) Defines the value of the label _start Exiting the program o Specifying the exit() system call (movl $1, %eax) Linux expects the system call number in EAX register o Specifying the status code (movl $, %ebx) Linux expects the status code in EBX register o Interrupting the operating system (int $x8) 9

Function Calls Function o A piece of code with well-defined entry and exit points, and a welldefined interface Call and Return abstractions o Call: jump to the beginning of an arbitrary procedure o Return: jump to the instruction immediately following the mostrecently-executed Call instruction The jump address in the return operation is dynamically determined 1

Implementing Function Calls P: # Function P jmp R # Call R Rtn_point1: R: # Function R jmp??? # Return Q: # Function Q jmp R Rtn_point2: # Call R What should the return instruction in R jump to? 11

Implementing Function Calls P: # Proc P movl $Rtn_point1, %eax jmp R # Call R Rtn_point1: R: # Proc R jmp %eax # Return Q: # Proc Q movl $Rtn_point2, %eax jmp R # Call R Rtn_point2: Convention: At Call time, store return address in EAX 12

Problem: 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 13

Need to Use a Stack A return address needs to be saved for as long as the function invocation continues Return addresses are used in the reverse order that they are generated: Last-In-First-Out The number of return addresses that may need to be saved is not statically known Saving return addresses on a Stack is the most natural solution 14

Stack Frames Use stack for all temporary data related to each active function invocation o Return address o Input parameters o Local variables of function o Saving registers across invocations Stack Frame Stack has one Stack Frame per active function invocation 15

High-Level Picture At Call time, push a new Stack Frame on top of the stack At Return time, pop the top-most Stack Frame 16

High-Level Picture main begins executing %ESP Bottom main s Stack Frame 17

High-Level Picture main begins executing main calls P %ESP Bottom P s Stack Frame main s Stack Frame 18

High-Level Picture main begins executing main calls P P calls Q %ESP Bottom Q s Stack Frame P s Stack Frame main s Stack Frame 19

High-Level Picture main begins executing main calls P P calls Q Q calls P %ESP Bottom P s Stack Frame Q s Stack Frame P s Stack Frame main s Stack Frame 2

High-Level Picture main begins executing main calls P P calls Q Q calls P P returns %ESP Bottom Q s Stack Frame P s Stack Frame main s Stack Frame 21

High-Level Picture main begins executing main calls P P calls Q Q calls P P returns Q calls R %ESP Bottom R s Stack Frame Q s Stack Frame P s Stack Frame main s Stack Frame 22

High-Level Picture main begins executing main calls P P calls Q Q calls P P returns Q calls R R returns %ESP Bottom Q s Stack Frame P s Stack Frame main s Stack Frame 23

High-Level Picture main begins executing main calls P P calls Q Q calls P P returns Q calls R R returns Q returns %ESP Bottom P s Stack Frame main s Stack Frame 24

High-Level Picture main begins executing main calls P P calls Q Q calls P P returns Q calls R R returns Q returns P returns %ESP Bottom main s Stack Frame 25

High-Level Picture main begins executing main calls P P calls Q Q calls P P returns Q calls R R returns Q returns P returns main returns Bottom 26

Function Call Details Call and Return instructions Argument passing between procedures Local variables Register saving conventions 27

Call and Return Instructions Instruction pushl src popl dest Function subl $4, %esp movl src, (%esp) movl (%esp), dest call addr addl $4, %esp pushl %eip jmp addr ret pop %eip %ESP before Call 28

Call and Return Instructions Instruction Function pushl src subl $4, %esp movl src, (%esp) popl dest movl (%esp), dest addl $4, %esp call addr ret pushl %eip jmp addr pop %eip %ESP after Call Old EIP 29

Call and Return Instructions Instruction Function pushl src subl $4, %esp movl src, (%esp) popl dest movl (%esp), dest addl $4, %esp call addr ret pushl %eip jmp addr pop %eip %ESP before Return Old EIP Return instruction assumes that the return address is at the top of the stack 3

Call and Return Instructions Instruction pushl src popl dest Function subl $4, %esp movl src, (%esp) movl (%esp), dest call addr addl $4, %esp pushl %eip jmp addr ret pop %eip %ESP after Return instruction assumes that Return the return address is at the top of the stack 31

Input Parameters Caller pushes input parameters before executing the Call instruction Parameters are pushed in the reverse order o Push N th argument first o Push 1 st argument last o So that the first argument is at the top of the stack at the time of the Call %ESP before pushing arguments 32

Input Parameters Caller pushes input parameters before executing the Call instruction Parameters are pushed in the reverse order o Push N th argument first o Push 1 st argument last o So that the first argument is at the top of the stack at the time of the Call %ESP before Call Arg 1 Arg Arg N 33

Input Parameters Caller pushes input parameters before executing the Call instruction Parameters are pushed in the reverse order o Push N th argument first o Push 1 st argument last o So that the first argument is at the top of the stack at the time of the Call Callee can address arguments relative to ESP: Arg 1 as 4(%esp) %ESP after Call Old EIP Arg 1 Arg Arg N 34

Input Parameters Caller pushes input parameters before executing the Call instruction Parameters are pushed in the reverse order o Push N th argument first o Push 1 st argument last o So that the first argument is at the top of the stack at the time of the Call %ESP before Return Old EIP Arg 1 Arg Arg N 35

Input Parameters Caller pushes input parameters before executing the Call instruction Parameters are pushed in the reverse order o Push N th argument first o Push 1 st argument last o So that the first argument is at the top of the stack at the time of the Call %ESP after Return Arg 1 Arg Arg N After the function call is finished, the caller pops the pushed arguments from the stack 36

Input Parameters Caller pushes input parameters before executing the Call instruction Parameters are pushed in the reverse order o Push N th argument first o Push 1 st argument last o So that the first argument is at the top of the stack at the time of the Call After the function call is finished, the caller pops the pushed arguments from the stack %ESP after popping arguments 37

Base Pointer: EBP As Callee executes, ESP may change Use EBP as a fixed reference point to access arguments and other local variables Need to save old value of EBP before using EBP Callee begins by executing pushl %ebp movl %esp, %ebp %ESP after Call %EBP Old EIP Arg 1 Arg Arg N 38

Base Pointer: EBP As Callee executes, ESP may change Use EBP as a fixed reference point to access arguments and other local variables Need to save old value of EBP before using EBP Callee begins by executing pushl %ebp movl %esp, %ebp %ESP, %EBP Old EBP Old EIP Arg 1 Arg Arg N Regardless of ESP, Callee can address Arg 1 as 8(%ebp) 39

Base Pointer: EBP Before returning, Callee must restore EBP to its old value Executes movl %ebp, %esp popl %ebp ret %ESP %EBP Old EBP Old EIP Arg 1 Arg Arg N 4

Base Pointer: EBP Before returning, Callee must restore EBP to its old value Executes movl %ebp, %esp popl %ebp ret %ESP, %EBP Old EBP Old EIP Arg 1 Arg Arg N 41

Base Pointer: EBP Before returning, Callee must restore EBP to its old value Executes movl %ebp, %esp popl %ebp ret %ESP Old EIP Arg 1 Arg Arg N %EBP 42

Base Pointer: EBP Before returning, Callee must restore EBP to its old value Executes movl %ebp, %esp popl %ebp ret %ESP Arg 1 Arg Arg N %EBP 43

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 o subl $4, %esp o subl $4, %esp o (or equivalently, subl $8, %esp) Reference local variables using the base pointer o -4(%ebp) o -8(%ebp) %ESP %EBP Var 2 Var 1 Old EBP Old EIP Arg 1 Arg Arg N 44

Use of Registers Problem: Callee may use a register that the caller is also using o When callee returns control to caller, old register contents may be lost o Someone must save old register contents and later restore Need a convention for who saves and restores which registers 45

GCC/Linux Convention Caller-save registers o %eax, %edx, %ecx o Save on stack prior to calling Callee-save registers o %ebx, %esi, %edi o Old values saved on stack prior to using %esp, %ebp handled as described earlier Return value is passed from Callee to Caller in %eax %ESP %EBP Saved Registers Var 2 Var 1 Old EBP Old EIP Arg 1 Arg Arg N Saved Registers 46

A Simple Example int add3(int a, int b, int c) { int d; d = a + b + c; } return d; int foo(void) { return add3( 3, 4, 5 ); } 47

A Simple Example int add3(int a, int b, int c){ int d; d = a + b + c; return d; } %ESP Var d %EBP old EBP old EIP Arg a Arg b Arg c add3: # Save old ebp and set up new ebp pushl %ebp movl %esp, %ebp # In general, one may need to push # callee-save registers onto the stack # Add the three arguments movl 8(%ebp), %eax addl 12(%ebp), %eax addl 16(%ebp), %eax # Put the sum into d movl %eax, -4(%ebp) # Return value is already in eax # In general, one may need to pop # callee-save registers # Restore old ebp, discard stack frame movl %ebp, %esp popl %ebp # Allocate space for d subl $4, $esp # Return ret 48

A Simple Example int foo(void) { return add3( 3, 4, 5 ); } old EIP Arg a Arg b Arg c %ESP %EBP foo: # Save old ebp, and set-up # new ebp pushl %ebp movl %esp, %ebp # No local variables # No need to save callee-save # registers as we # don t use any registers # No need to save caller- # save registers either # Push arguments in reverse order pushl $5 pushl $4 pushl $3 call add3 # Return value is already in eax # Restore old ebp and # discard stack frame movl %ebp, %esp popl %ebp # Return ret 49

Conclusion Invoking a function o Call: call the function o Ret: return from the instruction Stack Frame for a function invocation includes o Return address, o Procedure arguments, o Local variables, and o Saved registers Base pointer EBP o Fixed reference point in the Stack Frame o Useful for referencing arguments and local variables 5