Procedure-Calling Conventions October 30

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

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

Assembly Language: Function Calls

16.317: Microprocessor Systems Design I Spring 2015

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

16.317: Microprocessor Systems Design I Fall 2015

16.317: Microprocessor Systems Design I Fall 2014

SOEN228, Winter Revision 1.2 Date: October 25,

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

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

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

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

Systems I. Machine-Level Programming V: Procedures

Machine-level Programming (3)

Macros in Pentium. Assembly Language. October 25

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

mith College Computer Science CSC231 Assembly Week #11 Fall 2017 Dominique Thiébaut

16.317: Microprocessor Systems Design I Fall 2013

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

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures

CPS104 Recitation: Assembly Programming

Lecture 4 CIS 341: COMPILERS

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

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

CS213. Machine-Level Programming III: Procedures

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

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

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

An Introduction to x86 ASM

System Software Assignment 1 Runtime Support for Procedures

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

CIT Week13 Lecture

CSE P 501 Compilers. x86 Lite for Compiler Writers Hal Perkins Autumn /25/ Hal Perkins & UW CSE J-1

Practical Malware Analysis

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

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

CS61 Section Solutions 3

X86 Stack Calling Function POV

mith College Computer Science CSC231 Assembly Week #13 Fall 2017 Dominique Thiébaut

Computer Systems Lecture 9

X86 Assembly -Procedure II:1

We will first study the basic instructions for doing multiplications and divisions

Subprograms: Arguments

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

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

An Elegant Weapon for a More Civilized Age

Instruction Set Architecture

Subprograms: Local Variables

x86 assembly CS449 Fall 2017

Lecture 16: Passing Parameters on the Stack. Push Examples. Pop Examples. CALL and RET

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

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

Machine Code and Assemblers November 6

CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings.

CSE351 Spring 2018, Midterm Exam April 27, 2018

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

THEORY OF COMPILATION

Concepts Introduced in Chapter 7

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

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

Functions. Ray Seyfarth. August 4, Bit Intel Assembly Language c 2011 Ray Seyfarth

Computer Systems Organization V Fall 2009

Basic Pentium Instructions. October 18

Digital Forensics Lecture 3 - Reverse Engineering

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Machine Program: Procedure. Zhaoguo Wang

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Module 3 Instruction Set Architecture (ISA)

Sungkyunkwan University

System Programming and Computer Architecture (Fall 2009)

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

Microprocessors ( ) Fall 2010/2011 Lecture Notes # 15. Stack Operations. 10 top

Program Exploitation Intro

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

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

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

EECE.3170: Microprocessor Systems Design I Summer 2017

COMPUTER ENGINEERING DEPARTMENT

CPSC W Term 2 Problem Set #3 - Solution

Compiler construction. x86 architecture. This lecture. Lecture 6: Code generation for x86. x86: assembly for a real machine.

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

EN164: Design of Computing Systems Topic 03: Instruction Set Architecture Design

Sample Exam I PAC II ANSWERS

/ 28 HLL assembly Q4: Conditional instructions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

Millions of instructions per second [MIPS] executed by a single chip microprocessor

CS429: Computer Organization and Architecture

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

CSC201, SECTION 002, Fall 2000: Homework Assignment #3

Machine-Level Programming (2)

Lab 3. The Art of Assembly Language (II)

Transcription:

Procedure-Calling Conventions October 30 CSC201 Section 002 Fall, 2000

Saving registers Registers are inevitably used by subroutines; changes their! Registers have global scope; calling procedures also use their contents Conclusion: somebody needs to save registers before the subroutine executes, and restore s afterwards CSC201 Section Copyright 2000, Douglas Reeves 2

Approaches "Caller saves" model caller saves/restores just the registers that it knows have important s "Callee saves" model callee saves just the registers that it knows it will modify "Hybrid" model callee saves / restores the registers it modifies that are "important" to the caller Our convention: "callee saves" model CSC201 Section Copyright 2000, Douglas Reeves 3

Example of Callee Saves main:. call upcase nextinstruction: upcase proc push EAX ; save EAX get_ch add AL, 23 put_ch pop EAX ; restore EAX ret upcase endp CSC201 Section Copyright 2000, Douglas Reeves 4

Just after the push EAX and before the get_ch popping ESP Saved Value of EAX Address of nextinstr uction The stack pushing CSC201 Section Copyright 2000, Douglas Reeves 5

Passing Input Parameters and Return Values Registers are global variables; not a great idea to use for this purpose Better choice: the stack Before calling the procedure allocate room on the stack for the return Push a copy of the input parameter s on the stack Some parameters are modified by the procedure as part of its output In this case, push the parameter *address* instead of its Procedure can modify the parameter using indirect addressing CSC201 Section Copyright 2000, Douglas Reeves 6

Parameter Passing (cont.) Callee reads parameter s on stack, using based-displacement addressing EBP = based register Before using EBP, must save its and restore it on exit! CSC201 Section Copyright 2000, Douglas Reeves 7

"Cleaning Up" the Stack After calling a procedure and returning, the stack should look just like it did before the call The callee must 1. Deallocate memory it allocated 2. Pop saved register s from the stack 3. Pop the return address What about input parameters and the return? "callee" can deallocate input parameters with "ret x" statement "x" = # of bytes to deallocate (i.e., to add to ESP) only "caller" can deallocate the return, after copying CSC201 Section Copyright 2000, Douglas Reeves 8

Example Specification for a procedure: Write a procedure to read up to n characters from the standard input. Each alphabetic character is mapped to upper case and ouput. Stop when anything other than a lower-case alphabetic character is read, and return to the caller the number of lower case characters read and mapped to upper case. CSC201 Section Copyright 2000, Douglas Reeves 9

Activation record for procedure upcase EBP EBP+4 EBP+8 EBP+12 Saved old EBP Return address Input paramet er Space for return EBP-8 EBP-4 Storage for local variables of upcase would go here Saved old ECX Saved old EAX CSC201 Section Copyright The 2000, Douglas stack Reeves 10

main:. sub ESP, 4 ; allocate 4 bytes ; for the return mov maxch, 12 push maxch; maximum number ; of characters to map call upcase nextinstruction: pop nummap ; get return + de- ; allocate return val. space CSC201 Section Copyright 2000, Douglas Reeves 11

Just after call to upcase ESP Address of nextinstr uction 12 Space for return CSC201 Section Copyright The 2000, Douglas stack Reeves 12

upcase proc push EBP mov EBP, ESP push EAX ; save EAX push ECX mov ECX, 0 loop1: cmp ECX, [EBP+8] je endup get_ch cmp AL, a jl endup cmp AL, z jg endup add AL, 23 put_ch inc ECX jmp loop1 endup: mov [EBP+12], ECX pop ECX pop EAX ; restore EAX pop EBP ; restore EBP ret 4 ; deallocate input ; parameter space upcase endp CSC201 Section Copyright 2000, Douglas Reeves 13

Just before loop1 EBP Saved old EBP EBP+4 EBP+8 EBP+12 Address of nextinstr uction 12 Space for return ESP EBP-4 Saved old ECX Saved old EAX CSC201 Section Copyright The 2000, Douglas stack Reeves 14

Just before ret 4 (assuming 6 characters mapped to upper case) ESP Address of nextinstr uction 12 6 CSC201 Section Copyright The 2000, Douglas stack Reeves 15

Just after ret 4 ESP 6 CSC201 Section Copyright The 2000, Douglas stack Reeves 16

Procedure Checklist 1. Caller allocates space for return 2. Caller copies input parameters to stack using push 3. "call" statement 4. Callee pushes EBP 5. Caller copies ESP to EBP 6. Caller saves registers it will modify 7. Caller allocates space for local variables 8. --Body of procedure executes-- CSC201 Section Copyright 2000, Douglas Reeves 17

Programming Checklist (cont.) 9. Callee copies result into return variable space 10. Callee deallocates space for local variables 11. Callee restores registers, pops off stack 12. Callee restores EBP, pops off stack 13. Callee returns, deallocates input parameters from stack 14. Caller copies return 15. Caller deallocates space for return CSC201 Section Copyright 2000, Douglas Reeves 18

Using Registers, Instead How would the above example change if the procedure didn t call any other procedure (no nesting)? + we used registers to hold input parameters? + we used a register to hold the return? + the procedure used registers for all local variables? A lot simpler! If only we had enough registers... CSC201 Section Copyright 2000, Douglas Reeves 19

Example, Using Registers main:. mov EDX, 12 call upcase mov nummap, ECX ; of ECX was ; changed by procedure nextinstruction: CSC201 Section Copyright 2000, Douglas Reeves 20

upcase proc push EAX ; save EAX mov ECX, 0 loop1: cmp ECX, EDX je endup get_ch cmp AL, a jl endup cmp AL, z jg endup add AL, 23 put_ch inc ECX jmp loop1 endup: pop EAX ; restore EAX ret upcase endp CSC201 Section Copyright 2000, Douglas Reeves 21

Recursion Procedure calling itself! Traversing a tree, divide and conquer algorithms, etc. Example in C: Int f, a; a = 10; f = factorial(a); Int factorial(int a) { int b; if (a == 1) return(1); else { b = factorial(a-1); return (a * b); } } CSC201 Section Copyright 2000, Douglas Reeves 22

Example of Recursion main:. sub ESP, 4 ; allocate 4 bytes ; for the return mov a, 10 push a ; push input parameter ; on stack call factorial pop f ; get return off the ; stack CSC201 Section Copyright 2000, Douglas Reeves 23

factorial proc push EBP ; save base pointer mov EBP, ESP ; initialize base pointer ; for this activation record push EAX ; save EAX on stack cmp [EBP+8], 1 ; check if input parameter = 1 jne elsepart ; if not, keep going mov [EBP+12], 1 ; otherwise, return = 1 jmp returnall elsepart: sub esp, 4 ; allocate 4 bytes for return mov EAX, [EBP+8] ; decrement input parameter dec EAX ; and push it on the stack for push EAX ; the recursive call call factorial ; here s the recursion pop EAX ; get the return off stack mul [EBP+8] ; multiply by input, a mov [EBP+12], EAX ; store the result returnall: pop EAX ; restore EAX pop EBP ; restore EBP ret 4 ; deallocate input ; parameter space and return CSC201 Section Copyright 2000, Douglas Reeves 24

During first call to factorial(10) EBP Saved old EBP EBP+4 Return address EBP+8 EBP+12 10 Space for return EBP-4 Saved old EAX CSC201 Section Copyright The 2000, Douglas stack Reeves 25

During recursive call to factorial(9) Saved old EBP Return address 10 Space for return EBP+4 EBP+8 EBP+12 Return address 9 Space for return Saved old EAX CSC201 Section Copyright The 2000, Douglas stack Reeves 26