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

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

Stack Tutorial. Young W. Lim Sat. Young W. Lim Stack Tutorial Sat 1 / 15

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

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

Machine-level Programming (3)

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

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

Arrays. Young W. Lim Mon. Young W. Lim Arrays Mon 1 / 17

CS213. Machine-Level Programming III: Procedures

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

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

Assembly Language: Function Calls" Goals of this Lecture"

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

Machine-Level Programming III: Procedures

Arrays. Young W. Lim Wed. Young W. Lim Arrays Wed 1 / 19

CS241 Computer Organization Spring 2015 IA

CPSC W Term 2 Problem Set #3 - Solution

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

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

Giving credit where credit is due

Stack Debugging. Young W. Lim Sat. Young W. Lim Stack Debugging Sat 1 / 40

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

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

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

X86 Assembly -Procedure II:1

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

Sungkyunkwan University

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

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

CPS104 Recitation: Assembly Programming

Systems I. Machine-Level Programming V: Procedures

x86 assembly CS449 Fall 2017

Process Layout and Function Calls

Machine Program: Procedure. Zhaoguo Wang

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

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

CIT Week13 Lecture

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

Machine Programming 3: Procedures

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

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

Process Layout, Function Calls, and the Heap

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

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

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

W4118: PC Hardware and x86. Junfeng Yang

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

CPEG421/621 Tutorial

Machine- Level Programming III: Switch Statements and IA32 Procedures

Control. Young W. Lim Mon. Young W. Lim Control Mon 1 / 16

X86 Stack Calling Function POV

Stack Discipline Jan. 19, 2018

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

Assignment 11: functions, calling conventions, and the stack

Link 2. Object Files

Lecture 4 CIS 341: COMPILERS

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

THEORY OF COMPILATION

Data Representa/ons: IA32 + x86-64

Lab 10: Introduction to x86 Assembly

Link 2. Object Files

CSE 351: Week 4. Tom Bergan, TA

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

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

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

CS241 Computer Organization Spring Loops & Arrays

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

Sungkyunkwan University

An Elegant Weapon for a More Civilized Age

CS61 Section Solutions 3

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Machine- Level Programming III: Switch Statements and IA32 Procedures

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Link 4. Relocation. Young W. Lim Thr. Young W. Lim Link 4. Relocation Thr 1 / 26

Assembly I: Basic Operations. Jo, Heeseung

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

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

211: Computer Architecture Summer 2016

University of Washington

x86 assembly CS449 Spring 2016

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

System Programming and Computer Architecture (Fall 2009)

Instructor: Alvin R. Lebeck

Compiler Construction D7011E

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

Code Generation. Lecture 30

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

Machine-Level Programming II: Control and Arithmetic

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.

CSC 2400: Computing Systems. X86 Assembly: Function Calls

hnp://

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

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

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

The x86 Architecture

Instruction Set Architecture

Transcription:

Procedure Calls Young W. Lim 2017-08-21 Mon Young W. Lim Procedure Calls 2017-08-21 Mon 1 / 29

Outline 1 Introduction Based on Stack Background Transferring Control Register Usage Conventions Procedure Call Example Recursive Procedure Call Example Young W. Lim Procedure Calls 2017-08-21 Mon 2 / 29

Based on "Self-service Linux: Mastering the Art of Problem Determination", Mark Wilding "Computer Architecture: A Programmer s Perspective", Bryant & O Hallaron I, the copyright holder of this work, hereby publish it under the following licenses: GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License. CC BY SA This file is licensed under the Creative Commons Attribution ShareAlike 3.0 Unported License. In short: you are free to share and make derivative works of the file under the conditions that you appropriately attribute it, and that you distribute it only under a license compatible with this one. Young W. Lim Procedure Calls 2017-08-21 Mon 3 / 29

Stack Frame procedure calls passing procedure arguments receiving return informations saving registers stack frame: the portion of the stack allocated for single procedure call frame pointer (%ebp) stack pointer (%esp) Young W. Lim Procedure Calls 2017-08-21 Mon 4 / 29

Caller s Viewpoint H.I.G.H. A.D.D.R.E.S.S. frame pointer (%ebp) saved registers local variables temporaries arguments for a funcion call to the callee return address stack pointer (%esp) L.O.W. A.D.D.R.E.S.S. local variables > function arguments > return address Young W. Lim Procedure Calls 2017-08-21 Mon 5 / 29

Callee s Viewpoint......... %ebp+c: argument 2 from the caller %ebp+8: argument 1 from the caller %ebp+4: return address of the caller H.I.G.H. A.D.D.R.E.S.S. %ebp : caller s %ebp stored saved registers of the callee local variables of the callee temporaries of the callee......... L.O.W. A.D.D.R.E.S.S. function arguments > return address > caller s %ebp > local variables Young W. Lim Procedure Calls 2017-08-21 Mon 6 / 29

Stack Frame Pointers Frame Pointer (%ebp) : High : bottom of a stack frame Stack Pointer (%esp) : Low : top of a stack frame stack grows toward lower addresses push decreases %esp (growing stack) pop increases %esp (shrinking stack) contains a valid data at %esp address filled descending stack read access via %ebp Young W. Lim Procedure Calls 2017-08-21 Mon 7 / 29

Stack Frames & Arguments Caller P Callee Q Caller P s Stack Frame Argument values to Q Return address to P Callee Q Stack Frame P s frame pointer (%ebp) Saved registers Local variables Temporaries Q s arguments to other functions Young W. Lim Procedure Calls 2017-08-21 Mon 8 / 29

Stack Frames & Heap ------------------- H.I.G.H. A.D.D.R.E.S.S. ------------------- STACK (stack frame grows toward lower addresses)... stack Frame #1 v v v v... stack Frame #2 v v v v... v v v v... stack Frame #n v v v v ---------------------------------------------------------------- ---------------------------------------------------------------- ^ ^ ^ ^ ^ ^ ^ ^ - HEAP (heap grows toward higher addresses) ^ ^ ^ ^ ---------------------------------------------------------------- Young W. Lim Procedure Calls 2017-08-21 Mon 9 / 29

Stack Frames & Memory Map - H.I.G.H. A.D.D.R.E.S.S. - STACK (toward lower addresses) HEAP (toward higher addresses) Global Variables (BSS Seg) Static Variables (Data Seg) Machine Code (Text(ELF)) - L.O.W. A.D.D.R.E.S.S. - Young W. Lim Procedure Calls 2017-08-21 Mon 10 / 29

Procedure Instructions Procedure Call call label (direct call) call *operand (indirect call) Procedure Return leave (stack preparation) ret Young W. Lim Procedure Calls 2017-08-21 Mon 11 / 29

Call Instructions push a return address jump to the start the called function return address the address of the instruction immediately following the call instruction Direct Call call label Indirect call call *operand offset Imm a base reg Eb an index reg Ei a scale factor s Young W. Lim Procedure Calls 2017-08-21 Mon 12 / 29

Ret Instructions ret instruction pops the return address from the stack jump to the return address location stack pointer must points to the return address leave instruction prepare the stack for returning equivalent to the following mov %ebp, %esp Set stack ptr to the beginning of the stack) pop %ebp Restore saved %ebp Set the stack ptr to the end of caller s stack Young W. Lim Procedure Calls 2017-08-21 Mon 13 / 29

Return Value register %eax is used for returning the value of any function that returns an integer or pointer Young W. Lim Procedure Calls 2017-08-21 Mon 14 / 29

Direct and Indirect Call Examples (1) 1 int direct() { int i, b = 0; for (i = 0; i < INT_MAX; ++i) { b = foo(b); } int indirect(int (*fn)(int)) { int i, b = 0; for (i = 0; i < INT_MAX; ++i) { b = fn(b); } } return b; } return b; 1 https://gist.github.com/rianhunter/0be8dc116b120ad5fdd4#file-call_ overhead-c-l17 Young W. Lim Procedure Calls 2017-08-21 Mon 15 / 29

Direct and Indirect Call Examples (2) 1 int foo(int a) { return a; } int main(int argc, char *argv[]) { if (argc == 2 && argv[1][0] == d ) { return direct(); } else { return indirect(&foo); } } Young W. Lim Procedure Calls 2017-08-21 Mon 16 / 29

IA32 conventions for register usage the callee should not overwrite some registers that the caller is going to use later Caller Save Registers %eax, %edx, %ecx the callee can overwrite these registers Callee Save Registers %ebx, %esi, %edi the callee must save these registers before using must restore them before returning %ebp for the frame pointer %esp for the stack pointer Young W. Lim Procedure Calls 2017-08-21 Mon 17 / 29

GCC Example for a procedure call pushl %edi pushl %esi pushl %ebx movl 24(%ebp), %eax imull 15(%ebp), %eax leal 0(,%eax,4), %ecx addl 8(%ebp), %ecx movl %ebx, %edx Callee Save Registers %edi, %esi, %ebx restore these at the end by popl instructions Caller Save Registers other registers %eax, %ecx, %edx no need to restore Young W. Lim Procedure Calls 2017-08-21 Mon 18 / 29

P() calls Q() int P() { int a1 = 55; int a2 = 77; int sum = Q( &a1, &a2 ); int diff = a1 - a2; return sum * diff; } int Q(int *xp, int *yp) { int x = *xp; int y = *yp; *xp = y; *yp = x; return x+y; } Young W. Lim Procedure Calls 2017-08-21 Mon 19 / 29

Stack Frames for P() & Q() %ebp+0 : saved %ebp %ebp-4 : a2 %ebp-8 : a1 %ebp-12: &a2 %ebp-16: &a1 just beforecall to Q() %ebp+24 : saved %ebp (of P) %ebp+20 : a2 %ebp+16 : a1 %ebp+12 : &a2 %ebp+ 8 : &a1 %ebp+ 4 : return address %ebp+ 0 : saved %ebp (<-FP) %ebp- 4 : saved %ebx (<-SP) in the body of Q() Young W. Lim Procedure Calls 2017-08-21 Mon 20 / 29

Calling Code of P() leal -4(%ebp), %eax pushl %eax leal -8(%ebp), %eax pushl %eax call Q - compute &a2 - push &a2 - compute &a1 - push &a1 - call Q() function Young W. Lim Procedure Calls 2017-08-21 Mon 21 / 29

Setup Code for Q() %ebx is used in Q %ebx is a callee save register %ebx is pushed onto the stack Q: pushl %ebp movl %esp, %ebp pushl %ebx %ebp : frame pointer of P save this old %ebp set %ebp as a new frame pointer save %ebx Young W. Lim Procedure Calls 2017-08-21 Mon 22 / 29

Body Code for Q() return value is at %eax movl 8(%ebp), %edx movl 12(%ebp), %ecx movl (%edx), %ebx movl (%ecx), %eax movl %ecx, (%edx) movl %ebx, (%ecx) addl %ebx, %eax %edx holds xp %ecx holds yp %ebx holds x %eax holds y assign y to *xp assign x to *yp %eax holds x+y Young W. Lim Procedure Calls 2017-08-21 Mon 23 / 29

Finish Code for Q() popl %ebx movl %ebp, %esp popl %ebp ret restore %ebx restore %esp restore %ebp return to the caller Young W. Lim Procedure Calls 2017-08-21 Mon 24 / 29

Fibonacci Sequence int fibo(int n) { int prev, val; if (n <= 2) return 1; prev = fibo(n-2); val = fibo(n-1); return prev + val; } multiple outstanding calls each call has its own local variables allocated only when the procedure is called deallocated when it returns Young W. Lim Procedure Calls 2017-08-21 Mon 25 / 29

Stack Frames for the caller and the callee %ebp+8 : n %ebp+4 : return address %ebp+0 : saved %ebp...... %ebp-20: saved %esi %ebp-24: saved %ebp after initial setup %ebp+8 : n %ebp+4 : return address %ebp+0 : saved %ebp...... %ebp-20: saved %esi %ebp-24: saved %ebp...... %ebp-40: n-2 just before the 1st recusive call Young W. Lim Procedure Calls 2017-08-21 Mon 26 / 29

Setup Code for fibo() fibo: pushl %ebp movl %esp, %ebp subl $16, %esp pushl %esi pushl %ebx Set up code %ebp: frame pointer alloc 16 bytes on stack save %esi (-20) save %ebx (-24) Young W. Lim Procedure Calls 2017-08-21 Mon 27 / 29

Body Code for fibo() movl 8(%ebp), %ebx cmpl $2, %ebx jle.l24 addl $-12, %esp leal -2(%ebx), %eax pushl %eax call fibo movl %eax, %esi addl $-12, %esp leal -1(%ebx), %eax pushl %eax call fibo addl %esi, %eax jmp.l25 Young W. Lim Procedure Calls 2017-08-21 Mon 28 / 29

Finish Code for Q() popl %ebx movl %ebp, %esp popl %ebp ret restore %ebx restore %esp restore %ebp return to the caller Young W. Lim Procedure Calls 2017-08-21 Mon 29 / 29