Assembly Language for Intel-Based Computers, 4 th Edition

Similar documents
Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 5: Procedures. Chapter Overview. The Book's Link Library

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 5: Procedures

Assembly Language. Lecture 5 Procedures

Assembly Language. Lecture 5 Procedures

Assembly Language for Intel-Based Computers, 4 th Edition

Libraries and Procedures

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 8:Advanced Procedures

COE 205. Computer Organization and Assembly Language Dr. Aiman El-Maleh

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

Libraries and Procedures

Assembly Language for Intel-Based Computers, 4 th Edition. Lecture 25: Interface With High-Level Language

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 2: IA-32 Processor Architecture Included elements of the IA-64 bit

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

Procedure. Computer Organization and Assembly Languages Yung-Yu Chuang 2007/12/24. with slides by Kip Irvine

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

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Advanced Procedures. Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/24. with slides by Kip Irvine

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

COMPUTER ENGINEERING DEPARTMENT

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

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

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

GLOBAL EDITION. Assembly Language. for x86 Processors SEVENTH EDITION. Kip R. Irvine

Assembly Language Lab # 9

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

Lab 2: Introduction to Assembly Language Programming

Lab 5: Input/Output using a Library of Procedures

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

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

16.317: Microprocessor Systems Design I Fall 2013

Stack, subprograms. procedures and modular programming role of stack while using procedures stack implementation (Pentium)

Lab 6: Conditional Processing

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 8. Conditional Processing

Program Exploitation Intro

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

The x86 Architecture

Conditional Processing

Assembly Language for Intel-Based Computers, 4 th Edition

Assembly Language: Function Calls

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

AM2: Programming with Contemporary Instruction Set

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

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

Assembly Language: Function Calls" Goals of this Lecture"

UMBC. 1 (Feb. 9, 2002) seg_base + base + index. Systems Design & Programming 80x86 Assembly II CMPE 310. Base-Plus-Index addressing:

Assembly Language: Function Calls" Goals of this Lecture"

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

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Chapter Overview. Welcome to Assembly Language

Chapter 3: Addressing Modes

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 6: Conditional Processing. Chapter Overview. Boolean and Comparison Instructions

AM2: Protected-Mode Programming

Computer Systems Lecture 9

In order to run the program, go to the folder Release and run project.exe.

16.317: Microprocessor Systems Design I Fall 2015

16.317: Microprocessor Systems Design I Fall 2014

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

Module 3 Instruction Set Architecture (ISA)

CS 16: Assembly Language Programming for the IBM PC and Compatibles

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

Function Call Convention

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

Assembler Programming. Lecture 10

Practical Malware Analysis

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

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

Computer Science Final Examination Wednesday December 13 th 2006

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 3: Assembly Language Fundamentals

COMPUTER ENGINEERING DEPARTMENT

An Introduction to x86 ASM

Assembly Language for Intel-Based Computers, 5 th Edition

Assembly Language for Intel-Based Computers, 5 th Edition. Chapter 9: Strings and Arrays

16.317: Microprocessor Systems Design I Spring 2015

Assembly Language for Intel-Based Computers, 5 th Edition. Chapter 3: Assembly Language Fundamentals

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

Introduction to Assembly Language

IA-32 Architecture. CS 4440/7440 Malware Analysis and Defense

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

Lab 3. The Art of Assembly Language (II)

Assembly Language. Lecture 2 x86 Processor Architecture

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

EECE.3170: Microprocessor Systems Design I Summer 2017

Introduction to Assembly Language

The x86 Instruction Set

Lab 3: Defining Data and Symbolic Constants

Assembly Language for Intel-Based Computers, 5 th Edition. Kip R. Irvine. Chapter 6: Conditional Processing

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

CS61 Section Solutions 3

Build a program in Release mode and an executable file project.exe is created in Release folder. Run it.

Digital Forensics Lecture 3 - Reverse Engineering

Branching and Looping

W4118: PC Hardware and x86. Junfeng Yang

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

Course Syllabus [1/2]

Program Control Instructions

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

Lecture 4 CIS 341: COMPILERS

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 2: IA-32 Processor Architecture. Chapter Overview.

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

Transcription:

Assembly Language for Intel-Based Computers, 4 th Edition Kip R Irvine Chapter 5: Procedures Lecture 19: Procedures Procedure s parameters Slides prepared by Kip R Irvine Revision date: 08/22/2002 Modified by Dr Nikolay Metodiev Sirakov Chapter corrections (Web) Assembly language sources (Web) (c) Pearson Education, 2002 All rights reserved You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed

Defining and Using Procedures Creating Procedures Documenting Procedures Example: SumOf Procedure CALL and RET Instructions Nested Procedure Calls Local and Global Labels Procedure Parameters Flowchart Symbols USES Operator 2

Creating Procedures Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java or C++ function Following is an assembly language procedure named sample: sample PROC ret sample ENDP 3

Documenting Procedures Suggested documentation for each procedure: A description of all tasks accomplished by the procedure Receives: A list of input parameters; state their usage and requirements Returns: A description of values returned by the procedure Requires: Optional list of requirements called preconditions that must be satisfied before the procedure is called If a procedure is called without its preconditions having been satisfied, the procedure's creator makes no promise that it will work 4

Example: SumOf Procedure ;--------------------------------------------------------- SumOf PROC ; ; Calculates and returns the sum of three 32-bit integers ; Receives: EAX, EBX, ECX, the three integers May be ; signed or unsigned ; Returns: EAX = sum, and the status flags (Carry, ; Overflow, etc) are changed ; Requires: nothing ;--------------------------------------------------------- add eax,ebx add eax,ecx ret SumOf ENDP 5

CALL and RET Instructions The CALL instruction calls a procedure pushes offset of next instruction on the stack copies the address of the called procedure into EIP The RET instruction returns from a procedure pops top of stack into EIP 6

CALL-RET Example (1 of 2) 0000025 is the offset of the instruction immediately following the CALL instruction 00000040 is the offset of the first instruction inside MySub main PROC 00000020 call MySub 00000025 mov eax,ebx main ENDP MySub PROC 00000040 mov eax,edx ret MySub ENDP 7

CALL-RET Example (2 of 2) The CALL instruction pushes 00000025 onto the stack, and loads 00000040 into EIP 00000025 ESP 00000040 EIP The RET instruction pops 00000025 from the stack into EIP 00000025 ESP 00000025 EIP 8

Nested Procedure Calls main PROC call Sub1 exit main ENDP Sub1 PROC call Sub2 ret Sub1 ENDP By the time Sub3 is called, the stack contains all three return addresses: (ret to main) (ret to Sub1) Sub2 PROC call Sub3 ret Sub2 ENDP Sub3 PROC ret Sub3 ENDP (ret to Sub2) ESP 9

Local and Global Labels A local label is visible only to statements inside the same procedure A global label is visible everywhere main PROC jmp L2 L1:: exit main ENDP ; error! ; global label sub2 PROC L2: ; local label jmp L1 ; ok ret sub2 ENDP 10

Procedure Parameters (1 of 3) A good procedure might be usable in many different programs but not if it refers to specific variable names Parameters help to make procedures flexible because parameter values can change at runtime 11

Procedure Parameters (2 of 3) The ArraySum procedure calculates the sum of an array It makes two references to specific variable names: ArraySum PROC mov esi,0 mov eax,0 ; array index ; set the sum to zero L1: add eax,myarray[esi] ; add each integer to sum add esi,4 ; point to next integer loop L1 ; repeat for array size mov thesum,eax ret ArraySum ENDP ; store the sum What if you wanted to calculate the sum of two or three arrays within the same program? 12

Procedure Parameters (3 of 3) This version of ArraySum returns the sum of any doubleword array whose address is in ESI The sum is returned in EAX: ArraySum PROC ; Receives: ESI points to an array of doublewords, ; ECX = number of array elements ; Returns: EAX = sum ;----------------------------------------------------- mov eax,0 ; set the sum to zero L1: add eax,[esi] ; add each integer to sum add esi,4 ; point to next integer loop L1 ; repeat for array size ret ArraySum ENDP 13

Flowchart Symbols The following symbols are the basic building blocks of flowcharts: begin / end manual input process (task) display procedure call decision no yes (Includes two symbols not listed on page 166 of the book) 14

ArraySum Procedure Flowchart for the ArraySum Procedure begin push esi, ecx eax = 0 add eax,[esi] add esi, 4 cx = cx - 1 push esi push ecx mov eax,0 AS1: add eax,[esi] add esi,4 loop AS1 pop ecx pop esi yes CX > 0? no pop ecx, esi end 15

Your turn Draw a flowchart that expresses the following pseudocode: input exam grade from the user if( grade > 70 ) display "Pass" else display "Fail" endif 16

(Solution) begin input exam grade no grade > 70? yes display "Fail" display "Pass" end 17

Your turn Modify the flowchart in the previous slide to allow the user to continue to input exam scores until a value of 1 is entered 18

USES Operator Lists the registers that will be saved ArraySum PROC USES esi ecx mov eax,0 etc ; set the sum to zero MASM generates the following code: ArraySum PROC push esi push ecx pop ecx pop esi ret ArraySum ENDP 19

When not to push a register The sum of the three registers is stored in EAX on line (3), but the POP instruction replaces it with the starting value of EAX on line (4): SumOf PROC ; sum of three integers push eax ; 1 add eax,ebx ; 2 add eax,ecx ; 3 pop eax ; 4 ret SumOf ENDP 20

Program Design Using Procedures Top-Down Design (functional decomposition) involves the following: design your program before starting to code break large tasks into smaller ones use a hierarchical structure based on procedure calls test individual procedures separately 21

Integer Summation Program (1 of 4) Description: Write a program that prompts the user for multiple 32-bit integers, stores them in an array, calculates the sum of the array, and displays the sum on the screen Main steps: Prompt user for multiple integers Calculate the sum of the array Display the sum 22

Procedure Design (2 of 4) Main Clrscr PromptForIntegers WriteString ReadInt ArraySum DisplaySum WriteString WriteInt ; clear screen ; display string ; input integer ; sum the integers ; display string ; display integer 23

Structure Chart (3 of 4) Summation Program (main) Clrscr PromptForIntegers ArraySum DisplaySum WriteString ReadInt WriteString WriteInt gray indicates library procedure View the stub program View the final program 24

Sample Output (4 of 4) Enter a signed integer: 550 Enter a signed integer: -23 Enter a signed integer: -96 The sum of the integers is: +431 25