Assembly Language for Intel
Assembly Language for Intel - - Based Based Computers, 4
Computers, 4
ththEdition Edition
Chapter 5: Procedures
(c) Pearson Education, 2002. All rights reserved.
Kip R. Irvine
Chapter Overview Chapter Overview
• Linking to an External Library
• The Book's Link Library
• Stack Operations
• Defining and Using Procedures
• Program Design Using Procedures
3
Link Library Overview 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
Calling a Library Procedure Calling a Library Procedure
INCLUDE Irvine32.inc .code
mov eax,1234h ; input argument call WriteHex ; show hex number
call Crlf ; end of line
• Call a library procedure using the CALL instruction. Some procedures require input arguments. The INCLUDE directive copies in the procedure prototypes (declarations).
• The following example displays "1234" on the console:
5
Library Procedures
Library Procedures – – Overview [1/7] Overview [1/7]
Clrscr - Clears the console and locates the cursor at the upper left corner.
• This is typically done at the beginning and ending of a program.
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
Library Procedures
Library Procedures – – Overview [2/7] Overview [2/7]
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 DWORD 1,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
7
Library Procedures
Library Procedures – – Overview [3/7] Overview [3/7]
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 command-line arguments (called the command tail) into an array of bytes.
• It permits the user of a program to pass information on the command line.
GetMseconds - Returns the number of milliseconds that have elapsed since midnight.
• To measure the time between events (the return value is in EAX)
.data
cmdTail BYTE 129 DUP (0) .code
mov edx,OFFSET cmdTail call GetCommandtail
Encrypt file1.txt file2.txt
Library Procedures
Library Procedures – – Overview [4/7] Overview [4/7]
Gotoxy - Locates cursor at row (0~24) and column (0~79) on the console.
Random32 - Generates a 32-bit
pseudorandom integer in the range 0 to FFFFFFFFh.
Randomize - Seeds the random number generator.
RandomRange - Generates a pseudorandom integer within a specified range.
mov dh,10 ; row 10 mov dl,20 ; column 20 call Gotoxy ; locate cursor
.data
randVal DWORD ? .code
call Random32 mov randVal,eax
call Randomize mov exc,10
L1: call Random32
; use or display random value in EAX here Loop L1
.data
randVal DWORD ? .code
mov eax,5000
call RandomRange mov randVal,eax
9
Library Procedures
Library Procedures – – Overview [5/7] Overview [5/7]
ReadChar - Reads a single character from standard input.
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.
ReadString - Reads a string from standard input, terminated by the Enter key.
.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
.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.
41 42 43 44 45 46 47 00 ABCDEFG
Library Procedures
Library Procedures – – Overview [6/7] Overview [6/7]
SetTextColor - Sets the foreground and background colors of all subsequent text output to the console.
• EAX
WaitMsg - Displays message, waits for Enter key to be pressed.
WriteBin - Writes an unsigned 32-bit integer to standard output in ASCII binary format.
• EAX
WriteChar - Writes a single character to standard output.
• AL
white=15 lightCyan=11
lightGray=7 cyan=3
yellow=14 lightGreeb=10
brown=6 green=2
lightMagenta=13 lightBlue=9
magenta=5 blue=1
lightRed=12 gray=8
red=4 black=0
11
Library Procedures
Library Procedures – – Overview [7/7] Overview [7/7]
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
Example 1 Example 1
.code
call Clrscr mov eax,500 call Delay call DumpRegs
Clear the screen, delay the program for 500 milliseconds, and dump the registers and flags.
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
Sample output:
13
Example 2 Example 2
.data
str1 BYTE "Assembly language is easy!",0 .code
mov edx,OFFSET str1 call WriteString call Crlf
Display a null-terminated string and move the cursor to the
beginning of the next screen line.
Example 3 Example 3
IntVal = 35 ; constant
.code
mov eax,IntVal
call WriteBin ; display binary call Crlf
call WriteDec ; display decimal call Crlf
call WriteHex ; display hexadecimal call Crlf
Display the same unsigned integer in binary, decimal, and hexadecimal. Each number is displayed on a separate line.
0000 0000 0000 0000 0000 0000 0010 0011 35
23
Sample output:
15
Example 4 Example 4
.data
fileName BYTE 80 DUP(0) .code
mov edx,OFFSET fileName
mov ecx,SIZEOF fileName – 1 call ReadString
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.
Example 5 Example 5
.code
mov ecx,10 ; loop counter L1: mov eax,100 ; ceiling value
call RandomRange ; generate random int call WriteInt ; display signed int
call Crlf ; goto next display line
loop L1 ; repeat loop
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.
17
Example 6 Example 6
.data
str1 BYTE "Color output is easy!",0 .code
mov eax,yellow + (blue * 16) call SetTextColor
mov edx,OFFSET str1 call WriteString
call Crlf
Display a null-terminated string with yellow characters on a blue background.
The background color must be multiplied by 16 before you add it to the foreground color.
Stack Operations Stack Operations
• Runtime Stack
• PUSH Operation
• POP Operation
• PUSH and POP Instructions
• Using PUSH and POP
• Example: Reversing a String
• Related Instructions
19
Runtime Stack Runtime Stack
• Managed by the CPU, using two registers
• SS (stack segment)
• ESP (stack pointer)
00000006 00001000 ESP
Offset
00000FF8 00000FF4 00000FF0 00000FFC
PUSH Operation [1/2]
PUSH Operation [1/2]
• A 32-bit push operation decrements the stack pointer by 4 and copies a value into the location pointed to by the stack pointer.
00000006 00000006
ESP 00001000
00000FFC 00000FF8 00000FF4 00000FF0
000000A5 ESP
00001000
BEFORE
00000FFC 00000FF8 00000FF4 00000FF0
AFTER
21
PUSH Operation [2/2]
PUSH Operation [2/2]
• This is the same stack, after pushing two more integers:
00000006
ESP 00001000
Offset
00000FFC 00000FF8 00000FF4 00000FF0
000000A5 00000001 00000002
The stack grows downward. The area below ESP is always
available (unless the stack has overflowed).
POP Operation POP Operation
• Copies value at stack[ESP] into a register or variable.
• Adds n to ESP, where n is either 2 or 4.
• depends on the attribute of the operand receiving the data
BEFORE AFTER
00000006 000000A5 00000001
00000002 ESP
00000006 000000A5
00000001 ESP 00001000
00000FFC 00000FF8 00000FF4 00000FF0
00001000 00000FFC 00000FF8 00000FF4 00000FF0
23
PUSH and POP Instructions PUSH and POP Instructions
• PUSH syntax:
• PUSH r/m16
• PUSH r/m32
• PUSH imm32
• POP syntax:
• POP r/m16
• POP r/m32
Using PUSH and POP Using PUSH and POP
push esi ; push registers
push ecx push ebx
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 ; opposite order
pop ecx pop esi
Save and restore registers when they contain important values.
Note that the PUSH and POP instructions are in the opposite
order:
25
Example: Reversing a String 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
• Source code
• Q: Why must each character be put in EAX before it is pushed?
Because only word (16-bit) or doubleword (32-bit) values
can be pushed on the stack.
Your turn . . . Your turn . . .
• Using the String Reverse program as a starting point,
• Modify the program so the user can input a string of
up to 50 characters, and then displays the characters
in reverse order.
27
Related Instructions 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