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

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

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. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

CS213. Machine-Level Programming III: Procedures

Assembly Language: Function Calls

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

Machine-Level Programming III: Procedures

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

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

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

Giving credit where credit is due

CS241 Computer Organization Spring 2015 IA

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

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

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

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

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

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

Sungkyunkwan University

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

CPSC W Term 2 Problem Set #3 - Solution

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

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

Systems I. Machine-Level Programming V: Procedures

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

CIT Week13 Lecture

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

X86 Stack Calling Function POV

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

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

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

Link 2. Object Files

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

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

Machine Programming 3: Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures

Process Layout and Function Calls

Lecture 4 CIS 341: COMPILERS

Link 2. Object Files

W4118: PC Hardware and x86. Junfeng Yang

CPEG421/621 Tutorial

Assignment 11: functions, calling conventions, and the stack

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

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

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

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

Process Layout, Function Calls, and the Heap

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

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

CS61 Section Solutions 3

CPS104 Recitation: Assembly Programming

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

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

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

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

An Elegant Weapon for a More Civilized Age

Stack Discipline Jan. 19, 2018

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Machine- Level Programming III: Switch Statements and IA32 Procedures

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

Data Representa/ons: IA32 + x86-64

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

Assembly I: Basic Operations. Jo, Heeseung

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

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

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

University of Washington

211: Computer Architecture Summer 2016

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

Lab 10: Introduction to x86 Assembly

x86 assembly CS449 Spring 2016

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

Sungkyunkwan University

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

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

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

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

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

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

Instructor: Alvin R. Lebeck

System Programming and Computer Architecture (Fall 2009)

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Intel assembly language using gcc

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

hnp://

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.

Transcription:

Procedure Calls Young W. Lim 2016-11-05 Sat Young W. Lim Procedure Calls 2016-11-05 Sat 1 / 27

Outline 1 Introduction References Stack Background Transferring Control Register Usage Conventions Procedure Call Example Recursive Procedure Call Example Young W. Lim Procedure Calls 2016-11-05 Sat 2 / 27

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 2016-11-05 Sat 3 / 27

Stack Frame (1) procedure calls pass procedure arguments store the return info save registers stack frame: the portion of the stack allocated for single procedure call frame pointer (%ebp) stack pointer (%esp) Young W. Lim Procedure Calls 2016-11-05 Sat 4 / 27

Stack Frame : A Caller s Viewpoint (2) HIGH ADDRESS - frame pointer (%ebp) saved registers local variables temporaries arguments for a funcion call to the callee return address stack pointer (%esp) LOW ADDRESS Young W. Lim Procedure Calls 2016-11-05 Sat 5 / 27

Stack Frame : A Callee s Viewpoint (3) %ebp+c: argument 2 from the caller %ebp+8: argument 1 from the caller %ebp+4: return address of the caller HIGH ADDRESS - %ebp : caller s frame pointer stored saved registers of the callee local variables of the callee temporaries of the callee... LOW ADDRESS Young W. Lim Procedure Calls 2016-11-05 Sat 6 / 27

Stack Frames & Pointers Frame Pointer (%ebp) : High : bottom of a stack frame Stack Pointer (%esp) : Low : top of a stack frame stack grows toward lower addresses pushl decrease %esp (growing stack) popl increase %esp (shrinking stack) data accessed via %ebp Young W. Lim Procedure Calls 2016-11-05 Sat 7 / 27

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 (some local vars) Local variables Temporaries Q s arguments to other functions Young W. Lim Procedure Calls 2016-11-05 Sat 8 / 27

Stack Frames & Heap HIGH ADDRESS - STACK (stack frame grows toward lower addresses) Stack Frame #1 Stack Frame #2... Stack Frame #n Currently Unused Heap (heap grows toward higher addresses) Global Variables (BSS Seg) Static Variables (Data Seg) Machine Code (Text(ELF)) LOW ADDRESS - Young W. Lim Procedure Calls 2016-11-05 Sat 9 / 27

Stack Frames & Memory Map HIGH ADDRESS - STACK (toward lower addresses) Heap (toward higher addresses) Global Variables (BSS Seg) Static Variables (Data Seg) Machine Code (Text(ELF)) LOW ADDRESS - Young W. Lim Procedure Calls 2016-11-05 Sat 10 / 27

Procedure Instructions Procedure Call call label (direct call) call *operand (indirect call) Procedure Return leave (stack preparation) ret Young W. Lim Procedure Calls 2016-11-05 Sat 11 / 27

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 2016-11-05 Sat 12 / 27

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 2016-11-05 Sat 13 / 27

Return Value register %eax is used for returning the value of any function that returns an integer or pointer Young W. Lim Procedure Calls 2016-11-05 Sat 14 / 27

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 2016-11-05 Sat 15 / 27

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 2016-11-05 Sat 16 / 27

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 2016-11-05 Sat 17 / 27

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 2016-11-05 Sat 18 / 27

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 2016-11-05 Sat 19 / 27

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 2016-11-05 Sat 20 / 27

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 2016-11-05 Sat 21 / 27

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 2016-11-05 Sat 22 / 27

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 2016-11-05 Sat 23 / 27

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 2016-11-05 Sat 24 / 27

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 2016-11-05 Sat 25 / 27

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 2016-11-05 Sat 26 / 27

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 2016-11-05 Sat 27 / 27