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

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

Assembly Language: Function Calls" Goals of this Lecture"

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

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

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

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

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

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

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

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

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

CPEG421/621 Tutorial

x86 assembly CS449 Fall 2017

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

Process Layout and Function Calls

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

Process Layout, Function Calls, and the Heap

Intel assembly language using gcc

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

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 Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Assignment 11: functions, calling conventions, and the stack

CS213. Machine-Level Programming III: Procedures

Machine-level Programming (3)

Lab 10: Introduction to x86 Assembly

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

CPS104 Recitation: Assembly Programming

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

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

X86 Stack Calling Function POV

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

Assembly I: Basic Operations. Jo, Heeseung

CPSC W Term 2 Problem Set #3 - Solution

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

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

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

Subprograms: Local Variables

W4118: PC Hardware and x86. Junfeng Yang

CS , Fall 2001 Exam 1

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

Machine Programming 1: Introduction

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

CS241 Computer Organization Spring 2015 IA

Computer Systems Organization V Fall 2009

Reverse Engineering II: The Basics

The x86 Architecture

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

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

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

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

University of Washington

Sungkyunkwan University

Machine Programming 3: Procedures

UW CSE 351, Winter 2013 Midterm Exam

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

Instruction Set Architecture

Instruction Set Architecture

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Instructor: Alvin R. Lebeck

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

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

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

CIT Week13 Lecture

CSE 351: Week 4. Tom Bergan, TA

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

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 Loops & Arrays

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Systems I. Machine-Level Programming V: Procedures

Machine-Level Programming III: Procedures

An Introduction to x86 ASM

X86 Assembly -Procedure II:1

Stack Discipline Jan. 19, 2018

Lecture 4 CIS 341: COMPILERS

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CS61 Section Solutions 3

x86 assembly CS449 Spring 2016

Second Part of the Course

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Compiler Construction D7011E

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

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

System Programming and Computer Architecture (Fall 2009)

Machine-Level Programming II: Control and Arithmetic

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

Function Calls and Stack

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

Instruction Set Architecture

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

CS , Fall 2002 Exam 1

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

Code Generation. Lecture 30

Introduction Selected details Live demos. HrwCC. A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner

Transcription:

CS220 Logic Design Outline Calling Conventions Multi-module Programs 1 Calling and Returning We have already seen how the call instruction is used to execute a subprogram. call pushes the address of the instruction after the call onto the stack (the urn address) and loads the instruction pointer (EIP) register with the operand (an address): call foobar # Jump to foobar In the subprogram, a loads the EIP with the urn address from the stack. 2 Calling Conventions For subprograms with no arguments and no urn value, call and are sufficient. With arguments, the calling program and subprogram must agree on how the arguments are passed. This is known as a calling convention. The calling convention may differ from compiler to compiler. To interface assembly with C/C++ programs, we must know the calling convention. 3 Calling Conventions The CS220 library routines use a register based calling convention. All arguments and urn values are passed in registers. This method is very fast and easy, but only permits a limited number of arguments. The arguments are also limited in size. (They have to fit into a register.) The GNU C and C++ compilers (as well as MS VS C/C++) use the C calling (cdecl) convention for free (non-member) functions. 4 The cdecl Calling Convention In the cdecl calling convention arguments are passed on the stack. They are pushed (before the CALL) in right to left order. All integers (char, short, int) are pushed onto the stack as 32 bit values, as are pointers and floats. A long long int is pushed as two 32-bit values (least significant word pushed last!) A double occupies 8 bytes. A long double is pushed as extended precision (10 bytes) with two bytes of padding (12 bytes). 5 The cdecl Calling Convention The arguments are popped off the stack by the calling program after the urn. After a call to a subprogram that takes a single argument the stack would appear as: 4(%esp) (%esp) Parameter Return Address 6

The cdecl Calling Convention The argument can be accessed in the subprogram using the address 4(%esp). If the stack is used for local variable storage in the subprogram the value of the offset from the ESP to the arguments changes: 12(%esp) 8(%esp) 4(%esp) (%esp) Parameter 2 Parameter 1 Return Address Subprogram Data Accessing Parameters To solve this problem, the subprogram saves the value of EBP on the stack and then sets EBP equal to ESP. The EBP register is used to refer to data on the stack. : 12(%ebp) 16(%esp) 8(%ebp) 12(%esp) Parameter 1 4(%ebp) 8(%esp) Return Address (%ebp) 4(%esp) Saved EBP -4(%ebp) Parameter 2 (%esp) Subprogram Data 7 8 Accessing Parameters The first argument will always be at 8(%ebp), the second at 12(%ebp), etc. Local variables are at -4(%ebp), -8(%ebp) and so on. (The offsets depend on the data types.) A subprogram should have the form: pushl %ebp # save original EBP movl %esp,%ebp # new EBP = ESP movl %ebp,%esp # restore SP popl %ebp # restore EBP 9 Removing Parameters After the subprogram completes, the arguments that were pushed on the stack must be removed. The cdecl calling convention requires that the calling program do this: pushl %eax # push parameter on stack call foobar # call subprogram addl $4,%esp # adjust ESP The subprogram is permitted to change the value on the stack! 10 A reentrant subprogram can be called at any place, including from the subprogram itself. (Reentrant subprograms can be called recursively.) Subprograms that use only automatic variables are always reentrant. The general form of a subprogram with local variables is: pushl %ebp # save EBP movl %esp, %ebp # EBP = ESP subl $BYTES,%esp # room for locals 11 movl %ebp, %esp # deallocate locals popl %ebp # restore EBP 12

Local variables are stored on the stack right after the saved EBP value. Local variables are accessed using negative offsets to EBP. IMPORTANT: (%ebp) is the old EBP value, 4(%ebp) is the urn address, 8(%ebp), 12(%ebp), 16(%ebp),..., are subprogram parameters and -4(%ebp), -8(%ebp), -12(%ebp),..., are local variables. 13 The following C++ subroutine: void calcsum( int n, int *sump ) { int i, sum = 0; for ( i=1; i <= n; i++ ) sum += i; *sump = sum; } can be implemented in assembly as (Note: This subprogram requires that the calling program push *sump on the stack before n): 14 calcsum: push %ebp movl %esp,%ebp subl $4,%esp # room for sum movl $0,-4(%ebp) # sum = 0 movl $1,%ebx # ebx (i) = 1 for_loop: cmpl 8(%ebp),%ebx # is i <= n? jnle end_for addl %ebx,-4(%ebp) # sum += i incl %ebx jmp for_loop end_for: movl 12(%ebp),%ebx # ebx = *sump movl -4(%ebp),%eax # eax = sum movl %eax,(%ebx) # *sump = sum movl %ebp,%esp popl %ebp 15 The enter instruction is equivalent to the three instructions at the beginning of the subprogram. enter takes two operands. The second operand is 0 in the cdecl calling convention. The first operand is the number of bytes needed by the local variables. The instruction is equivalent to the two instructions just before the. The instruction has no operands. 16 Using enter and the general form of a subprogram with local variables is: enter $BYTES,0 # push EBP, EBP = ESP # make room for locals # ESP = EBP, pop EBP The skeleton.s code uses the enter and instructions. (It does not reserve any room for local variables however.) 17 Using Registers The cdecl calling convention permits the EAX, ECX, and EDX registers to be used freely by the subprogram. If they are being used by the calling program, they should be pushed onto the stack (or otherwise saved) before calling the subprogram and then restored after the urn. All other registers (EBX, EBP, EDI, ESI) must be preserved (typically by pushing onto the stack) if used by the subprogram. 18

Using Registers So a subprogram should have the form: enter $BYTES,0 # Saves EBP for us pushl %ebx # If used below pushl %edi # If used below pushl %esi # If used below popl %esi # Only if pushed above popl %edi # Only if pushed above popl %ebx # Only if pushed above # Restores EBP 19 Using Registers The calling program should have the form: pushl %eax # Only if being used pushl %ecx # Only if being used pushl %edx # Only if being used # PUSH SUBPROGRAM ARGUMENTS HERE call foobar # ADJUST ESP FOR ARGUMENTS HERE popl %edx # Only if pushed above popl %ecx # Only if pushed above popl %eax # Only if pushed above 20 Return Values According to the cdecl calling convention, the char, short, int and pointer types are urned in EAX. The long long type is urned in EDX:EAX. The float, double and long double types are urned in %st(0). All other FPU registers should be empty (free). The calling convention also dictates how structures and classes are passed and urned. (The details are left for research by the interested student.) 21 Consider a function that urns the sum of its two int arguments. The urn is an int:.globl _sumtwo.section.text _sumtwo: enter $0, $0 movl 8(%ebp),%eax addl 12(%ebp),%eax 22 Here is a C++ driver: #include <iostream> using namespace std; extern "C" int sumtwo(int a, int b); int main() { cout << sumtwo(2, 4) << \n ; } 23 The program would be built using: g++ -o sumtwo driver.cpp sumtwo.s Note: The MinGW C/C++ compiler prepends an underscore to all function names and global variables (for compatibility with MS Visual Studio). We must manually add this to the name in assembly. (The Linux C/C++ compiler does not do this and the underscore should be omitted in assembly.) 24

The extern C qualifier tells the C++ compiler that we are calling a C type function. C++ allows function overloading (C does not) and achieves this through name mangling. (The function names at the assembly level are different than those used at the C++ level.) The extern C qualifier tells the C++ compiler that the routine name is not name mangled. It is not necessary to use the extern C qualifier in C. 25 Here is the corresponding assembly language driver: pushl $4 # Push args pushl $2 call _sumtwo addl $8, %esp # Pop args call print_int call print_nl To compile (assemble): g++ -o sumtwo driver.s sumtwo.s \ -L cs220lib -lcs220 26 Here is a similar function but now the first argument is a double and the second is an int. The urn is a double:.globl _sumtwo.section.text _sumtwo: enter $0, $0 # setup stack fldl 8(%ebp) # FPU: a fiaddl 16(%ebp)# FPU: a+b # restore stack 27 Here is the new C++ driver: #include <iostream> using namespace std; extern "C" double sumtwo(double a, int b); int main() { cout << sumtwo(2.3, 4) << \n ; } 28 And the new corresponding assembly driver: # Push 4 on stack pushl $4 # Move 2.3 onto stack fldl CON1 subl $8, %esp fstpl (%esp) call _sumtwo addl $12, %esp # Pop args call print_double ffree %st(0) 29 Multi-Module Programs A multi-module program consists of more than one object file. All of the programs that we have written are multi-module programs that consist of our own module and modules from the C library. The linker combines the modules into an executable. It matches up references in one module to definitions in a different module. 30

Multi-Module Programs The global (or globl) directive makes a label in the current module visible to other modules:.globl _asm_main.globl _sumtwo, _sumthree Global variables defined in the data or bss sections that are to be shared with other modules must also be declared global. 31 Other Calling Conventions The standard calling convention (stdcall) is similar to cdecl, but requires the subprogram to pop the arguments off of the stack. This is done by using an operand with. The ESP is incremented by the operand value after popping the urn address: $40 stdcall does not allow variable length argument lists. It is used by functions in the MS Windows API. 32 Other Calling Conventions Other Calling Conventions The thiscall convention is used when calling C++ member functions. The object address is pushed on the stack after all of the arguments (in the first argument position). In MS C++ the address is passed in ECX. In the fastcall convention the first and second arguments are passed in the ECX and EDX registers respectively. All other arguments are pushed on the stack. GCC/G++ allows you to specify a calling convention other than the default with the attribute keyword: int foo(int a) attribute ((stdcall)); Calling convention details differ from compiler to compiler. This makes it difficult to interface code compiled with different compilers (GNU C++ and VS C++). 33 34 Name Mangling C++ uses name mangling to allow overloaded functions and methods. For example foo::bar(int, long) const is mangled as bar C3fooil while foo::bar(int, double) is mangled as bar 3fooid. Since our primary goal is understanding how function calls work, the details of name mangling will not be covered. 35