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

Similar documents
mith College Computer Science CSC231 Assembly Week #12 Thanksgiving 2017 Dominique Thiébaut

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

mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut

Introduction to 8086 Assembly

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

mith College Computer Science CSC231 - Assembly Week #9 Dominique Thiébaut

Program Exploitation Intro

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

CS61 Section Solutions 3

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

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

mith College Computer Science CSC231 - Assembly Week #3 Dominique Thiébaut

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

Lab 3. The Art of Assembly Language (II)

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

Assembly Language: Function Calls

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

Assembly Language: Function Calls" Goals of this Lecture"

Conditional Processing

Assembly Language: Function Calls" Goals of this Lecture"

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

CPS104 Recitation: Assembly Programming

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

SOEN228, Winter Revision 1.2 Date: October 25,

mith College Computer Science CSC231 - Assembly Week #4 Dominique Thiébaut

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

16.317: Microprocessor Systems Design I Fall 2014

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

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

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

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

16.317: Microprocessor Systems Design I Fall 2015

Lab 6: Conditional Processing

Jump instructions. Unconditional jumps Direct jump. do not change flags. jmp label

Computer Architecture and Assembly Language. Practical Session 5

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

16.317: Microprocessor Systems Design I Spring 2015

Practical Malware Analysis

An Introduction to x86 ASM

Second Part of the Course

Reverse Engineering Low Level Software. CS5375 Software Reverse Engineering Dr. Jaime C. Acosta

Process Layout and Function Calls

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ

Computer Architecture..Second Year (Sem.2).Lecture(4) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

IFE: Course in Low Level Programing. Lecture 6

mith College Computer Science CSC231 - Assembly Week #8 Dominique Thiébaut

Procedure-Calling Conventions October 30

Subprograms: Arguments

COMPUTER ENGINEERING DEPARTMENT

Assembly Language Lab # 9

Computer Architecture and Assembly Language. Practical Session 3

Subprograms: Local Variables

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

Assembler lecture 4 S.Šimoňák, DCI FEEI TU of Košice

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

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

CSCI 2021: x86-64 Control Flow

Language of x86 processor family

Basic Assembly Instructions

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

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

System Programming and Computer Architecture (Fall 2009)

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

Towards the Hardware"

Intel Instruction Set (gas)

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

CSE351 Spring 2018, Midterm Exam April 27, 2018

Assembly Language: IA-32 Instructions

COMPUTER ENGINEERING DEPARTMENT

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

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Summary: Direct Code Generation

Credits and Disclaimers

Instruction Set Architecture

Chapter 4 Processor Architecture: Y86 (Sections 4.1 & 4.3) with material from Dr. Bin Ren, College of William & Mary

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

Lecture 4 CIS 341: COMPILERS

Assembly II: Control Flow

The x86 Architecture

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 6: Conditional Processing

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

Lecture (08) x86 programming 7

Computer Architecture and Assembly Language. Practical Session 7

Process Layout, Function Calls, and the Heap

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

1 if (a == b) 2 { 3 /* true stuff here */ 4 } 5 else 6 { 7 /* false stuff here */ 8 } 10 /* done either way (i.e., whether true.

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

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

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

Machine-Level Programming II: Control and Arithmetic

Assembly Language II: Addressing Modes & Control Flow

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

16.317: Microprocessor Systems Design I Fall 2013

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

Selection and Iteration. Chapter 7 S. Dandamudi

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

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

Complex Instruction Set Computer (CISC)

Branching and Looping

Transcription:

mith College Computer Science CSC231 Assembly Week #11 Fall 2017 Dominique Thiébaut dthiebaut@smith.edu

Back to Conditional Jumps

Review sub eax, 10 jz there xxx xxx there:yyy yyy

Review cmp eax, 10 jz there xxx xxx there:yyy yyy

Examples ; if eax == ebx: ; count += 1 ; ; if dish[j] == 1: ; dishstring[j] = '#' ; else ; dishstring[j] = ' '

Examples ; if dish[j-1]!= dish[j+1]: ; newdish[j] = '#' ; else ; newdish[j] = ' '

Examples ; # c is a char ; if c >= 'a' and c <= 'z': ; c = chr( ord(c)-32 ) ;

; for (int i=0; i<20; i++ ) { ; print( fib[i] ); ; } Examples

Examples ; if x > 0xF0000000: ; z = x+2 ; else: ; z = x-1

Meet ALL the Conditional Jumps! je, jz je, jz jne, jnz jne, jnz jl jb jle jbe jg ja jge jae

And also jc jnc jp jnp

Functions http://www.minionsallday.com/wp-content/uploads/2016/03/all_different_minions.jpeg

eax ebx eip esp ecx edx esi edi CF PF ZF SF OF DF ALU

The Stack Segment Stack Process Data Code

Simplest use of the Stack: Push & Pop

Push & Pop ecx eip > 003 mov ecx, 10 004 for: push ecx 007 mov ecx, message 00A mov edx, dword[numchar] 00C call _printstring 00F pop ecx 011 loop for

Push & Pop ecx 10 eip > 003 mov ecx, 10 004 for: push ecx 007 mov ecx, message 00A mov edx, dword[numchar] 00C call _printstring 00F pop ecx 011 loop for

Push & Pop ecx 10 10 eip > 003 mov ecx, 10 004 for: push ecx 007 mov ecx, message 00A mov edx, dword[numchar] 00C call _printstring 00F pop ecx 011 loop for

Push & Pop ecx 11a9f7c3 10 eip > 003 mov ecx, 10 004 for: push ecx 007 mov ecx, message 00A mov edx, dword[numchar] 00C call _printstring 00F pop ecx 011 loop for

Push & Pop ecx 11a9f7c3 10 eip > 003 mov ecx, 10 004 for: push ecx 007 mov ecx, message 00A mov edx, dword[numchar] 00C call _printstring 00F pop ecx 011 loop for

Push & Pop ecx 11a9f7c3 10 003 mov ecx, 10 004 for: push ecx 007 mov ecx, message 00A mov edx, dword[numchar] 00C call _printstring 00F pop ecx 011 loop for eip >

Push & Pop ecx 11a9f7c3 10 eip > 003 mov ecx, 10 004 for: push ecx 007 mov ecx, message 00A mov edx, dword[numchar] 00C call _printstring 00F pop ecx 011 loop for

Push & Pop ecx 10 10 eip > 003 mov ecx, 10 004 for: push ecx 007 mov ecx, message 00A mov edx, dword[numchar] 00C call _printstring 00F pop ecx 011 loop for

Push & Pop ecx 10 9 10 eip > 003 mov ecx, 10 004 for: push ecx 007 mov ecx, message 00A mov edx, dword[numchar] 00C call _printstring 00F pop ecx 011 loop for

Push & Pop ecx 10 9 9 eip > 003 mov ecx, 10 004 for: push ecx 007 mov ecx, message 00A mov edx, dword[numchar] 00C call _printstring 00F pop ecx 011 loop for

Push & Pop ecx 11a9f7c3 9 eip > 003 mov ecx, 10 004 for: push ecx 007 mov ecx, message 00A mov edx, dword[numchar] 00C call _printstring 00F pop ecx 011 loop for

Push & Pop ecx Etc 003 mov ecx, 10 004 for: push ecx 007 mov ecx, message 00A mov edx, dword[numchar] 00C call _printstring 00F pop ecx 011 loop for

Always pop in reverse order 003 004 for: push ecx 007 push edx 00A push eax xxx xxx xxx 01F pop eax 021 pop edx 023 pop ecx

We stopped here last time

Back to Functions

001 ; add1(): adds 1 to eax 001 add1: inc eax 003 ret Our first function! 004 ; main() 004 _Start: 004 mov eax, 10 007 call add1 00A sub eax, 5 00C call add1 00F add eax, 3

eax 001 ; add1(): adds 1 to eax 001 add1: inc eax 003 ret eip > 004 ; main() 004 _Start: 004 mov eax, 10 007 call add1 00A sub eax, 5 00C call add1 00F add eax, 3

eax 10 001 ; add1(): adds 1 to eax 001 add1: inc eax 003 ret eip > 004 ; main() 004 _Start: 004 mov eax, 10 007 call add1 00A sub eax, 5 00C call add1 00F add eax, 3

eax eip > 10 001 ; add1(): adds 1 to eax 001 add1: inc eax 003 ret 004 ; main() 004 _Start: 004 mov eax, 10 007 call add1 00A sub eax, 5 00C call add1 00F add eax, 3 00A

eax eip > 10 11 001 ; add1(): adds 1 to eax 001 add1: inc eax 003 ret 004 ; main() 004 _Start: 004 mov eax, 10 007 call add1 00A sub eax, 5 00C call add1 00F add eax, 3 00A

eax 10 11 00A 001 ; add1(): adds 1 to eax 001 add1: inc eax 003 ret eip > 004 ; main() 004 _Start: 004 mov eax, 10 007 call add1 00A sub eax, 5 00C call add1 00F add eax, 3

eax 10 11 6 00A 001 ; add1(): adds 1 to eax 001 add1: inc eax 003 ret eip > 004 ; main() 004 _Start: 004 mov eax, 10 007 call add1 00A sub eax, 5 00C call add1 00F add eax, 3

eax eip > 10 11 6 001 ; add1(): adds 1 to eax 001 add1: inc eax 003 ret 004 ; main() 004 _Start: 004 mov eax, 10 007 call add1 00A sub eax, 5 00C call add1 00F add eax, 3 00F

eax 10 11 6 7 00F eip > 001 ; add1(): adds 1 to eax 001 add1: inc eax 003 ret 004 ; main() 004 _Start: 004 mov eax, 10 007 call add1 00A sub eax, 5 00C call add1 00F add eax, 3

eax eip > 10 11 6 7 001 ; add1(): adds 1 to eax 001 add1: inc eax 003 ret 004 ; main() 004 _Start: 004 mov eax, 10 007 call add1 00A sub eax, 5 00C call add1 00F add eax, 3 00F

eax 10 11 6 7 10 001 ; add1(): adds 1 to eax 001 add1: inc eax 003 ret 00F eip > 004 ; main() 004 _Start: 004 mov eax, 10 007 call add1 00A sub eax, 5 00C call add1 00F add eax, 3 012??

func: inst1 inst2 ret pop into eip call func inst3 { push addr of inst3 jmp func

What about Nested Function Calls?

eax eip > 001 ; add1(): adds 1 to eax 001 add1: inc eax 002 call sub2 003 ret 004 sub2: sub eax, 2 008 ret 00A _Start: 00A mov eax, 10 00F call add1 011 sub eax, 5

eax eip > 10 001 ; add1(): adds 1 to eax 001 add1: inc eax 002 call sub2 003 ret 004 sub2: sub eax, 2 008 ret 00A _Start: 00A mov eax, 10 00F call add1 011 sub eax, 5

eax 10 011 eip > 001 ; add1(): adds 1 to eax 001 add1: inc eax 002 call sub2 003 ret 004 sub2: sub eax, 2 008 ret 00A _Start: 00A mov eax, 10 00F call add1 011 sub eax, 5

eax 10 11 011 eip > 001 ; add1(): adds 1 to eax 001 add1: inc eax 002 call sub2 003 ret 004 sub2: sub eax, 2 008 ret 00A _Start: 00A mov eax, 10 00F call add1 011 sub eax, 5

eax eip > 10 11 001 ; add1(): adds 1 to eax 001 add1: inc eax 002 call sub2 003 ret 004 sub2: sub eax, 2 008 ret 00A _Start: 00A mov eax, 10 00F call add1 011 sub eax, 5 011 003

eax 10 11 9 011 001 ; add1(): adds 1 to eax 001 add1: inc eax 002 call sub2 003 ret 003 eip > 004 sub2: sub eax, 2 008 ret 00A _Start: 00A mov eax, 10 00F call add1 011 sub eax, 5

eax 10 11 9 011 eip > 001 ; add1(): adds 1 to eax 001 add1: inc eax 002 call sub2 003 ret 004 sub2: sub eax, 2 008 ret 00A _Start: 00A mov eax, 10 00F call add1 011 sub eax, 5 003

eax eip > 10 11 9 001 ; add1(): adds 1 to eax 001 add1: inc eax 002 call sub2 003 ret 004 sub2: sub eax, 2 008 ret 00A _Start: 00A mov eax, 10 00F call add1 011 sub eax, 5 011 003

eax 10 11 9 4 001 ; add1(): adds 1 to eax 001 add1: inc eax 002 call sub2 003 ret 004 sub2: sub eax, 2 008 ret 011 003 eip > 00A _Start: 00A mov eax, 10 00F call add1 011 sub eax, 5???,?

Exercise Write a program that uses a function tolower() to return the lowercase version of a string. ecx contains the address, and edx the number of characters in the string.

Exercise Write a program that uses a function printdashes that prints a line of stars, the length of which is passed in eax. Write your function so that it doesn't modify any of the other registers.

Passing Parameters to Functions

Passing through registers Passing through the stack Passing by Value Passing by Reference

Passing Parameters Via Registers 001 ; add1(): adds 1 to eax 001 add1: inc eax 003 call sub2 003 ret 004 ; sub2: subtracts 2 from ex 004 sub2: sub eax, 2 008 ret 00A _Start: 00A mov eax, 10 00F call add1 011 sub eax, 5

eax ebx ecx eip esp ebp edx esi edi CF PF ZF SF OF DF ALU

Passing Parameters Through the Stack void printsumxy( int x, int y ) { print( x+y ); } int a, b; a = 3; b = 5; printsumxy( a, b ); // prints 8

section.data a dd 0 b dd 0 section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy

section.data a dd 0 b dd 0 eax section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy < eip

section.data a dd 0 3 b dd 0 eax section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy < eip

section.data a dd 0 3 b dd 0 5 eax section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy < eip

section.data a dd 0 3 b dd 0 5 eax section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy < eip 3

section.data a dd 0 3 b dd 0 5 eax section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy 027 < eip 3 5

section.data a dd 0 3 b dd 0 5 eax section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 < eip 3 5 027 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy 027

section.data a dd 0 3 b dd 0 5 eax section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy 027 < eip 3 5 027 old ebp

section.data a dd 0 3 b dd 0 5 eax section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy 027 < eip 3 5 027 old ebp ebp

section.data a dd 0 3 b dd 0 5 eax 5 section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy 027 < eip 3 5 027 old ebp ebp

section.data a dd 0 3 b dd 0 5 eax 5 8 section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy 027 < eip 3 5 027 old ebp ebp

somewhere where _printint lives < eip section.data a dd 0 3 b dd 0 5 eax 5 8 section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy 027 3 5 027 old ebp 00B < ebp

section.data a dd 0 3 b dd 0 5 eax 5 8 section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy 027 < eip 3 5 027 old ebp 00B < ebp esp

section.data a dd 0 3 b dd 0 5 section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy 027 < eip eax < ebp 3 5 027 old ebp 00B 5 8

section.data a dd 0 3 b dd 0 5 section.text 001 ; printsumxy(x,y): prints x+y 001 printsumxy: 001 push ebp 003 mov ebp, esp 005 mov eax, dword[ebp+8] 007 add eax, dword[ebp+12] 009 call _printint 00B pop ebp 00C ret 8 01A _Start: 01A mov dword[a], 3 01F mov dword[b], 5 021 push dword[a] 023 push dword[b] 025 call printsumxy 027 < eip eax < ebp 3 5 027 old ebp 00B 5 8

Exercise void func( int x ) { return x*3 + 2; } int a, b, c, d; a = 3; b = 5; c = func( a ); d = func( 30 ); translate this into assembly

;;; FUNCTION SIDE function: push ebp ;save old ebp mov ebp, esp ;make ebp point ;to stack frame Summary xxx xxx dword[ebp+8] ;access paramn dword[ebp+12];access paramn-1 pop ebp ;restore ebp ret 4N ;return and pop ;4N bytes from stack ;;; CALLER SIDE push push push call param1 param2 paramn function

Summary ;;; FUNCTION SIDE function: push ebp ;save old ebp mov ebp, esp ;make ebp point ;to stack frame push-registers-you-will-use (clean function) xxx xxx dword[ebp+8] ;access param1 dword[ebp+12];access param2 ;;; CALLER SIDE pop-register-you-used pop ebp ;restore ebp ret 4N ;return and pop ;4N bytes from stack push push push call param1 param2 paramn function

Useful Instructions for Functions pushad popad ;push EAX, ECX, EDX, ;EBX, original ESP, ; EBP, ESI, and EDI. ;pop them back in ;reverse order

;;; ; ------------------------------------------------------------ ;;; ; _printstring: prints a string whose address is in ;;; ; ecx, and whose total number of chars ;;; ; is in edx. ;;; ; Examples: ;;; ; Assume a string labeled msg, containing "Hello World!", ;;; ; and a constant MSGLEN equal to 12. To print this string: ;;; ; ;;; ; mov ecx, msg ;;; ; mov edx, MSGLEN ;;; ; call _printstring ;;; ; ;;; ; REGISTERS MODIFIED: NONE ;;; ; ------------------------------------------------------------ ;;; ;save eax and ebx so that they are not modified by the function Example 1 _printstring: push push mov mov int pop pop ret eax ebx eax,sys_write ebx,stdout 0x80 ebx eax

Example 2 ;;; ;------------------------------------------------------ ;;; ;------------------------------------------------------ ;;; ; getinput: gets a numerical input from the keyboard. ;;; ; returns the resulting number in eax (32 bits). ;;; ; recognizes - as the first character of ;;; ; negative numbers. Does not skip whitespace ;;; ; at the beginning. Stops on first not decimal ;;; ; character encountered. ;;; ; ;;; ; NO REGISTERS MODIFIED, except eax ;;; ; ;;; ; Example of call: ;;; ; ;;; ; call getinput ;;; ; mov dword[x], eax ; put integer in x ;;; ; ;;; ;------------------------------------------------------ ;;; ;------------------------------------------------------ _getinput: section.bss buffer resb 120 intg resd 1 isneg resb 1 section.text pushad ; save all registers.loop1: mov esi, buffer ; eci --> buffer mov edi, 0 ; edi = counter of chars mov eax, 03 ; input

Dot-Labels ;;; ;------------------------------------------------------ ;;; ;------------------------------------------------------ printline: mov esi, buffer ; esi --> buffer mov edi, 0 ; edi = counter of chars.loop1: mov eax, 03 ; input mov ebx, 0 ; stdin mov ecx, esi ; where to put the next char loop.loop1.for1: mov.for2: mov... ret ;;; ;------------------------------------------------------ ;;; ;------------------------------------------------------ printreg: mov esi, buffer mov edi, 0.for1: mov.for2: mov...... ret

Dot-Labels ;;; ;------------------------------------------------------ ;;; ;------------------------------------------------------ printline: mov esi, buffer ; esi --> buffer mov edi, 0 ; edi = counter of chars.loop1: mov eax, 03 ; input mov ebx, 0 ; stdin mov ecx, esi ; where to put the next char loop.loop1.for1: mov.for2: mov... ret ;;; ;------------------------------------------------------ ;;; ;------------------------------------------------------ printreg: mov esi, buffer mov edi, 0.for1: mov printline.for2:.for2: mov...... ret printreg.for2:

Passing through registers Passing through the stack Passing by Value Passing by Reference