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

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

Assembly Language. Lecture 5 Procedures

Assembly Language for Intel-Based Computers, 4 th Edition

Assembly Language. Lecture 5 Procedures

Libraries and Procedures

Libraries and Procedures

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

Lab 5: Input/Output using a Library of Procedures

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

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

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

AM2: Programming with Contemporary Instruction Set

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

AM2: Protected-Mode Programming

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

Assembly Language Lab # 9

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

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

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

Lab 2: Introduction to Assembly Language Programming

COMPUTER ENGINEERING DEPARTMENT

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 Language for Intel-Based Computers, 4 th Edition. Chapter 3: Assembly Language Fundamentals

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

The x86 Architecture

Computer Architecture and Assembly Language. Practical Session 3

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

Chapter 3: Addressing Modes

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

Assembly Language for Intel-Based Computers, 5 th Edition

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

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

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

Chapter 3: Assembly Language Fundamentals. Cristina G. Rivera

Practical Malware Analysis

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

Assembly Language Programming

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

Macros. Introducing Macros Defining Macros Invoking Macros Macro Examples Nested Macros Example Program: Wrappers

Introduction to Assembly Language

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

Assembly Language for Intel-Based Computers, 4 th Edition

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

Lab 4: Basic Instructions and Addressing Modes

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

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

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

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

Lab 6: Conditional Processing

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

Program Exploitation Intro

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

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

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

Assembly Language Fundamentals

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

Module 3 Instruction Set Architecture (ISA)

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

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

Experiment 8 8 Subroutine Handling Instructions and Macros

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

Lab 3: Defining Data and Symbolic Constants

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

Assembly Language Fundamentals

Introduction to Assembly Language

EEM336 Microprocessors I. Addressing Modes

COMPUTER ENGINEERING DEPARTMENT

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 for Intel-Based Computers, 4 th Edition. Chapter 6: Conditional Processing. Chapter Overview. Boolean and Comparison Instructions

Digital Forensics Lecture 3 - Reverse Engineering

Computer Systems Lecture 9

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

Complex Instruction Set Computer (CISC)

UMBC. A register, an immediate or a memory address holding the values on. Stores a symbolic name for the memory location that it represents.

Assembly Fundamentals

ELEC 242 Using Library Procedures

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

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

Lab 3. The Art of Assembly Language (II)

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

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

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

EEM336 Microprocessors I. Data Movement Instructions

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

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

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

ORG ; TWO. Assembly Language Programming

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

x86 assembly CS449 Fall 2017

An Introduction to x86 ASM

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

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

Basic Execution Environment

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

Introduction to IA-32. Jo, Heeseung

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

INTRODUCTION TO IA-32. Jo, Heeseung

Assembly Language. Lecture 3 Assembly Fundamentals

x86 Assembly Tutorial COS 318: Fall 2017

Transcription:

Assembly Language for Intel-Based Computers, 4 th Edition Kip R. Irvine Chapter 5: Procedures Slides prepared by the author Revision date: June 4, 2006 (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.

Chapter Overview 5.1 Introduction 5.2 Linking to an External Library 5.3 The Book's Link Library 5.4 Stack Operations 5.5 Defining and Using Procedures 5.6 Program Design Using Procedures Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 2

Link Library Overview A file containing procedures that have been compiled into machine code constructed from one or more OBJ files To build a library,... start with one or more ASM source files assemble each into an OBJ file create an empty library file (extension.lib) add the OBJ file(s) to the library file, using the Microsoft LIB utility Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 3

Calling a Library Procedure Call a library procedure using the CALL instruction. Syntax: call procedure_name The following example displays "1234" on the console: The INCLUDE directive copies in the procedure prototypes (declarations). INCLUDE Irvine32.inc.code mov eax,1234h call WriteHex call Crlf ; input argument ; show hex number ; end of line Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 4

Linking to a Library command: link hello.obj irvine32.lib kernel32.lib In the batch file named make32.bat, it has been included the assembler and linker commands Notice the two LIB files: Irvine32.lib, and kernel32.lib The latter is part of the Microsoft Win32 Software Development Kit (SDK) Y our program can link to links to Irvine32.lib links to kernel32.lib executes kernel32.dll Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 5

Library Procedures - Overview (1 of 9) Clrscr - Clears the console and locates the cursor at the upper left corner. Crlf - Writes an end of line sequence to standard output. To advance the cursor to the beginning of the next line of standard output. (0Dh and 0Ah) Delay - Pauses the program execution for a specified n millisecond interval. Set EAX to the desired interval, in millisecond mov eax,1000 ; 1 second call Delay Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 6

Library Procedures - Overview (2 of 9) DumpMem - Writes a block of memory to standard output in hexadecimal. Pass the starting address of memory in ESI, the number of units in ECX, and the unit size in EBX.data array DWORD1,2,3,4,5,6,7,8,9,0Ah,0Bh.code main PROC mov esi,offset array ; starting OFFSET mov ecx,lengthof array ; number of units mov ebx,type array ; doubleword format call DumpMem 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 0000000A 0000000B Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 7

Library Procedures - Overview (3 of 9) DumpRegs - Displays the EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP, EFLAGS, and EIP registers in hexadecimal. Also displays the Carry, Sign, Zero, and Overflow flags. Be useful for program debugging GetCommandtail - Copies the program s commandline arguments (called the command tail) into an array of bytes. It permits the user of a program to pass information on the command line..data cmdtail BYTE 129 DUP (0) Encrypt file1.txt file2.txt.code mov edx, OFFSET cmdtail call GetCommandtail Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 8

Library Procedures - Overview (4 of 9) GetMseconds - Returns the number of milliseconds that have elapsed since midnight. To measure the time between events (the return value is in EAX) Gotoxy - Locates cursor at row and column on the console. mov dh, 10 ; row 10 mov dl, 20 ; column 20 call Gotoxy ; locate cursor Random32 - Generates a 32-bit pseudorandom integer in the range 0 to FFFFFFFFh..data randval DWORD?.code call Random32 mov randval, eax Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 9

Library Procedures - Overview (5 of 9) Randomize - Seeds the random number generator. call Randomize mov exc,10 L1: call Random32 ; use or display random value in EAX here Loop L1 RandomRange - Generates a pseudorandom integer within a specified range..data randval DWORD?.code mov eax, 5000 call RandomRange mov randval, eax Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 10

Library Procedures - Overview (6 of 9) ReadChar - Reads a single character from standard input..data char BYTE?.code call ReadChar mov char, al.data hexval DWORD?.code call ReadHex mov hexval, eax.data intval SWORD?.code call ReadInt mov intval, eax ReadHex - Reads a 32-bit hexadecimal integer from standard input, terminated by the Enter key. ReadInt - Reads a 32-bit signed decimal integer from standard input, terminated by the Enter key. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 11

Library Procedures - Overview (7 of 9) ReadString - Reads a string from standard input, terminated by the Enter key. 41 42 43 44 45 46 47 00 ABCDEFG.data buffer BYTE 50 DUP(0) ; holds the char. bytecount DWORD? ; holds counter.code mov edx, OFFSET buffer ; point to the buffer mov ecx, (SIZEOF buffer) -1 ; specify max char. call ReadString ; input the string mov bytecount,eax ; no. of char. WaitMsg - Displays message, waits for Enter key to be pressed. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 12

Library Procedures - Overview (8 of 9) SetTextColor - Sets the foreground and background colors of all subsequent text output to the console. EAX black=0 red=4 gray=8 lightred=12 blue=1 magenta=5 lightblue=9 lightmagenta=13 green=2 brown=6 lightgreeb=10 yellow=14 cyan=3 lightgray=7 lightcyan=11 white=15 WriteBin - Writes an unsigned 32-bit integer to standard output in ASCII binary format EAX WriteChar - Writes a single character to standard output. AL Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 13

Library Procedures - Overview (9 of 9) WriteDec - Writes an unsigned 32-bit integer to standard output in decimal format. EAX WriteHex - Writes an unsigned 32-bit integer to standard output in hexadecimal format. EAX WriteInt - Writes a signed 32-bit integer to standard output in decimal format. EAX WriteString - Writes a null-terminated string to standard output..data prompt BYTE Enter your name:,0.code mov edx, OFFSET prompt call WriteString Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 14

Example 1 Clear the screen, delay the program for 500 milliseconds, and dump the registers and flags..code call Clrscr mov eax,500 call Delay call DumpRegs Sample output: EAX=00000613 EBX=00000000 ECX=000000FF EDX=00000000 ESI=00000000 EDI=00000100 EBP=0000091E ESP=000000F6 EIP=00401026 EFL=00000286 CF=0 SF=1 ZF=0 OF=0 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 15

Example 2 Display a null-terminated string and move the cursor to the beginning of the next screen line..data str1 BYTE "Assembly language is easy!",0.code mov edx,offset str1 call WriteString call Crlf Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 16

Example 3 Display the same unsigned integer in binary, decimal, and hexadecimal. Each number is displayed on a separate line. IntVal = 35.code mov eax,intval call WriteBin call Crlf call WriteDec call Crlf call WriteHex call Crlf ; constant ; display binary ; display decimal ; display hexadecimal Sample output: 0000 0000 0000 0000 0000 0000 0010 0011 35 23 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 17

Example 4 Input a string from the user. EDX points to the string and ECX specifies the maximum number of characters the user is permitted to enter..data filename BYTE 80 DUP(0).code mov edx,offset filename mov ecx,sizeof filename 1 call ReadString Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 18

Example 5 Generate and display ten pseudorandom signed integers in the range 0 99. Each integer is passed to WriteInt in EAX and displayed on a separate line..code mov ecx,10 L1:mov eax,100 call RandomRange call WriteInt call Crlf loop L1 ; loop counter ; ceiling value ; generate random int ; display signed int ; goto next display line ; repeat loop Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 19

Example 6 Display a null-terminated string with yellow characters on a blue background..data str1 BYTE "Color output is easy!",0.code mov eax,yellow + (blue * 16) call SetTextColor mov edx,offset str1 call WriteString call Crlf The background color must be multiplied by 16 before you add it to the foreground color. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 20

What's Next 5.2 Linking to an External Library 5.3 The Book's Link Library 5.4 Stack Operations 5.5 Defining and Using Procedures 5.6 Program Design Using Procedures Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 21

5.4 Stack Operations Runtime Stack PUSH Operation POP Operation PUSH and POP Instructions Using PUSH and POP Example: Reversing a String Related Instructions STACK: LIFO (last-in, first-out) structure Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 22

Runtime Stack A memory managed by the CPU using two registers SS (stack segment): hold a segment descriptor ESP (stack pointer): 32-bit offset into the stack * Note: both SS and ESP are directly modified by push, pop, call, ret instructions Offset 00001000 00000FFC 00000FF8 00000FF4 00000FF0 00000006 ESP * SP in Real-address mode Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 23

PUSH Operation (1 of 2) 1. decrements the stack pointer by 4 or 2 2. copy a value into the location pointed to by the stack pointer. Ex: push eax BEFORE AFTER 00001000 00000006 ESP 00001000 00000006 00000FFC 00000FFC 000000A5 ESP 00000FF8 00000FF8 00000FF4 00000FF4 00000FF0 00000FF0 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 24

PUSH Operation (2 of 2) This is the same stack, after pushing two more integers: Offset 00001000 00000FFC 00000FF8 00000FF4 00000FF0 00000006 000000A5 00000001 00000002 ESP The stack grows downward. The area below ESP is always available (unless the stack has overflowed). Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 25

POP Operation 1. Copies value at stack[esp] into a register or variable. 2. Adds n to ESP, where n is either 2 or 4. Depends on the attribute of the operand receiving the data Ex: pop eax BEFORE AFTER 00001000 00000006 00001000 00000006 00000FFC 000000A5 00000FFC 000000A5 00000FF8 00000001 00000FF8 00000001 ESP 00000FF4 00000002 ESP 00000FF4 00000FF0 00000FF0 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 26

STACK Applications Temporary save area for registers CPU save the current procedure s return address on the stack when calling a procedure The input values (called arguments) are pushed on the stack when calling a procedure Local variables inside a procedure are created on the stack and are discarded when the procedure ends Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 27

PUSH and POP Instructions PUSH syntax: PUSH r/m16 PUSH r/m32 PUSH imm32 POP syntax: POP r/m16 POP r/m32 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 28

Using PUSH and POP Save and restore registers when they contain important values. Note that the PUSH and POP instructions are in the opposite order: push esi push ecx push ebx ; push registers mov esi,offset dwordval ; starting OFFSET mov ecx,lengthof dwordval ; number of units mov ebx,type dwordval ; size of a doubleword call DumpMem ; display memory pop ebx pop ecx pop esi ; opposite order Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 29

Example: Reversing a String Use a loop with indexed addressing Push each character on the stack Start at the beginning of the string, pop the stack in reverse order, insert each character into the string See page 158 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 30

Your turn... Using the String Reverse program as a starting point, #1: Modify the program so the user can input a string of up to 50 characters. #2: Modify the program so it inputs a list of 32-bit integers from the user, and then displays the integers in reverse order. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 31

Related Instructions PUSHFD and POPFD push and pop the EFLAGS register PUSHAD pushes the 32-bit general-purpose registers on the stack order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI POPAD pops the same registers off the stack in reverse order PUSHA and POPA do the same for 16-bit registers.data saveflags DWORD?.code pushfd ; push flags on stack pop saveflags ; copy into a variable push saveflags ; push saved flag values popfd ; copy into a variable Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 32

Your Turn... Write a program that does the following: Assigns integer values to EAX, EBX, ECX, EDX, ESI, and EDI Uses PUSHAD to push the general-purpose registers on the stack Using a loop, the program pops each integer from the stack and displays it on the screen Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 33

What's Next 5.2 Linking to an External Library 5.3 The Book's Link Library 5.4 Stack Operations 5.5 Defining and Using Procedures 5.6 Program Design Using Procedures Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 34

5.5 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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 35

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 main is also a procedure, but ends with a exit call Following is an assembly language procedure named sample: sample PROC.. ret sample ENDP Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 36

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. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 37

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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 38

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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 39

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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 40

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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 41

Nested Procedure Calls main PROC.. call Sub1 exit main ENDP By the time Sub3 is called, the stack contains all three return addresses: Sub1 PROC.. call Sub2 ret Sub1 ENDP Sub2 PROC.. call Sub3 ret Sub2 ENDP (ret to main) (ret to Sub1) (ret to Sub2) ESP Sub3 PROC.. ret Sub3 ENDP Thus, stack is suitable for procedure handling Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 42

Local and Global Labels A local label is visible only to statements inside the same procedure. A global label is visible everywhere. Local label: Global label: main PROC jmp L2 L1:: exit main ENDP Label_name: Global_name:: ; error! ; global label sub2 PROC L2: ; local label jmp L1 ; ok ret sub2 ENDP Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 43

Procedure Parameters (1 of 3) A good procedure might be usable in many different programs Parameters help to make procedures flexible because parameter values can change at runtime One method to pass arguments is using generalpurpose registers.data thesum DWORD?.code main PROC mov eax, 10000h mov ebx, 20000h mov ecx, 30000h call SumOf mov thesum eax ; argument ; argument ; argument ; EAX=EAX+EBX+ECX ; save the sum Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 44

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? Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 45

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 ; Recevies: 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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 46

Flowchart Symbols The following symbols are the basic building blocks of flowcharts: begin / end manual input process (task) display procedure call decision no yes Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 47

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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 48

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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 49

... (Solution) begin input exam grade no grade > 70? yes display "Fail" display "Pass" end Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 50

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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 51

USES Operator Coupled with the PROC directive, lists the registers that will be saved Once encounter with USES, the assembler will generate PUSH instructions at the beginning POP instructions at the end Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 52

USES Example 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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 53

What's Next 5.2 Linking to an External Library 5.3 The Book's Link Library 5.4 Stack Operations 5.5 Defining and Using Procedures 5.6 Program Design Using Procedures Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 54

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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 55

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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 56

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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 57

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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 58

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 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 59