0% found this document useful (0 votes)
16 views9 pages

IO Irvine

Uploaded by

scarfock56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views9 pages

IO Irvine

Uploaded by

scarfock56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Subject: Microprocessors Lecture No.

: 10 Babylon University
Stage: Second Input-Output College of Information Technology
Class Room No.: 2 Department of Software
procedures in Irvine
Lecture Time: 8.30-10.30
Instructor: Dr. Ali Hadi Hasan
library

Input-Output procedures in x86 Assembly language


There are some of usefully standard Assembly language procedures which work
for x86 Architecture. These procedures are built-in Irvine library and can categorize
for:
1. Standard input procedures used for input data.
2. Standard output procedures used for output or display data.
3. Other procedures dealing with console window and test/converting numbers.
1. Standard input procedures
1.1. ReadChar
The ReadChar procedure reads a single character from the keyboard and returns
the character in the AL register. The character is not echoed in the console window.
Sample call:
.data
char BYTE ?
.code
call ReadChar
mov char,al
If the user presses an extended key such as a function key, arrow key, Ins, or Del,
the procedure sets AL to zero, and AH contains a keyboard scan code. The upper half
of EAX is not preserved. The following pseudo code describes the possible outcomes
after calling ReadChar:
if an extended key was pressed
AL = 0
AH = keyboard scan code
else
AL = ASCII key value
Endif

1.2. ReadDec
The ReadDec procedure reads a 32-bit unsigned decimal integer from the
keyboard and returns the value in EAX. Leading spaces are ignored. The return value
is calculated from all valid digits found until a non-digit character is encountered. For
example, if the user enters123ABC, the value returned in EAX is 123. Following is a
sample call:
.data
intVal DWORD ?
.code
call ReadDec
mov intVal,eax

ReadDec affects the Carry flag in the following ways:

1
Subject: Microprocessors Lecture No. : 10 Babylon University
Stage: Second Input-Output College of Information Technology
Class Room No.: 2 Department of Software
procedures in Irvine
Lecture Time: 8.30-10.30
Instructor: Dr. Ali Hadi Hasan
library

 If the integer is blank, EAX = 0 and CF = 1


 If the integer contains only spaces, EAX = 0 and CF = 1
 If the integer is larger than 232-1, EAX = 0 and CF = 1
 Otherwise, EAX holds the converted integer and C F= 0

1.3. ReadHex
The ReadHex procedure reads a 32-bit hexadecimal integer from the keyboard
and returns the corresponding binary value in EAX. The programmer can use both
uppercase and lowercase letters for the digits A through F. A maximum of eight digits
may be entered (additional characters are ignored). Leading spaces are ignored.
Sample call:
.data
hexVal DWORD ?
.code
call ReadHex
mov hexVal,eax

1.4. ReadInt
The ReadInt procedure reads a 32-bit signed integer from the keyboard and
returns the value in EAX. The user can type an optional leading plus or minus sign,
and the rest of the number may only consist of digits. ReadInt sets the Overflow flag
and display an error message if the value entered cannot be represented as a 32-bit
signed integer (range: -2,147,483,648 to +2,147,483,647). The return value is
calculated from all valid digits found until a no digit character is encountered. For
example, if the user enters +123ABC, the value returned is +123.
Sample call:
.data
IntVal SDWORD ?
.code
call ReadInt
mov intVal,eax

1.5. ReadKey
The ReadKey procedure performs a no-wait keyboard check. In other words, it
inspects the keyboard input buffer to see if a key has been pressed by the user. If no
keyboard data is found, the Zero flag is set. If a key press is found by ReadKey, the
Zero flag is cleared and AL is assigned either zero or an ASCII code. If AL contains
zero, the user may have pressed a special key (function key, arrow key, etc.) The AH
register contains a virtual scan code, DX contains a virtual key code, and EBX
contains the keyboard flag bits. The following pseudo code describes the various
outcomes when calling ReadKey:

2
Subject: Microprocessors Lecture No. : 10 Babylon University
Stage: Second Input-Output College of Information Technology
Class Room No.: 2 Department of Software
procedures in Irvine
Lecture Time: 8.30-10.30
Instructor: Dr. Ali Hadi Hasan
library

If no_keyboard_data then
ZF = 1
else
ZF = 0
if AL = 0 then
extended key was pressed, and AH = scan code, DX =
virtual
key code, and EBX = keyboard flag bits
else
AL = the key's ASCII code
endif
endif
The upper halves of EAX and EDX are overwritten when ReadKey is called.

1.6. ReadString
The ReadString procedure reads a string from the keyboard, stopping when the
user presses the Enter key. Pass the offset of a buffer in EDX and set ECX to the
maximum number of characters the user can enter, plus 1 (to save space for the
terminating null byte).The procedure returns the count of the number of characters
typed by the user in EAX.
Sample call:
.data
buffer BYTE 21 DUP(0) ; input buffer
byte Count DWORD ? ; holds counter
.code
mov edx,OFFSET buffer ; point to the buffer
mov ecx,SIZEOF buffer ; specify max characters
call ReadString ; input the string
mov byteCount,eax ; number of characters
ReadString automatically inserts a null terminator in memory at the end of the
string. The following is a hexadecimal and ASCII dump of the first 8 bytes of buffer
after the user has entered the string “ABCDEFG”:

The variable byte Count equals 7.

2. Standard output procedures

3
Subject: Microprocessors Lecture No. : 10 Babylon University
Stage: Second Input-Output College of Information Technology
Class Room No.: 2 Department of Software
procedures in Irvine
Lecture Time: 8.30-10.30
Instructor: Dr. Ali Hadi Hasan
library

2.1. WriteBin
The WriteBin procedure writes an integer to the console window in ASCII binary
format. Pass the integer in EAX. The binary bits are displayed in groups of four for
easy reading.
Sample call:
mov eax,12346AF9h
call WriteBin
The following output would be displayed by our sample code:
0001 0010 0011 0100 0110 1010 1111 1001

2.2. WriteBinB
The WriteBinB procedure writes a 32-bit integer to the console window in ASCII
binary format. Pass the value in the EAX register and let EBX indicate the display
size in bytes (1, 2, or 4). The bits are displayed in groups of four for easy reading.
Sample call:
mov eax,00001234h
mov ebx,TYPE WORD ; 2 bytes
call WriteBinB ; displays 0001 0010 0011 0100

2.3. WriteChar
The WriteChar procedure writes a single character to the console window. Pass
the character (or its ASCII code) in AL.
Sample call:
mov al,'A'
call WriteChar ; displays: "A"

2.4. WriteDec
The WriteDec procedure writes a 32-bit unsigned integer to the console window
in decimal format with no leading zeros. Pass the integer in EAX.
Sample call:
mov eax,295
call WriteDec ; displays: "295"

2.5. WriteHex
The WriteHex procedure writes a 32-bit unsigned integer to the console window
in8-digit hexadecimal format. Leading zeros are inserted if necessary. Pass the integer
in EAX.
Sample call:
mov eax,7FFFh
call WriteHex ; displays: "00007FFF"
2.6. WriteHexB

4
Subject: Microprocessors Lecture No. : 10 Babylon University
Stage: Second Input-Output College of Information Technology
Class Room No.: 2 Department of Software
procedures in Irvine
Lecture Time: 8.30-10.30
Instructor: Dr. Ali Hadi Hasan
library

The WriteHexB procedure writes a 32-bit unsigned integer to the console


window in hexadecimal format. Leading zeros are inserted if necessary. Pass the
integer in EAX and let EBX indicate the display format in bytes (1, 2, or 4). Sample
call:
mov eax,7FFFh
mov ebx,TYPE WORD ; 2 bytes
call WriteHexB ; displays: "7FFF"

2.7. WriteInt
The WriteInt procedure writes a 32-bit signed integer to the console window in
decimal format with a leading sign and no leading zeros. Pass the integer in EAX.
Sample call:
mov eax,216543
call WriteInt ; displays: "+216543"

2.8. WriteString
The WriteString procedure writes a null-terminated string to the console window.
Pass the string’s offset in EDX.
Sample call:
.data
prompt BYTE "Enter your name: ",0
.code
mov edx,OFFSET prompt
call WriteString

2.9. DumpMem
The DumpMem procedure writes a range of memory to the console window in
hexadecimal.Pass it the starting address in ESI, the number of units in ECX, and the
unit size in EBX (1 = byte, 2 = word, 4 = double word). The following sample call
displays an array of 11 double words in hexadecimal:
.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 ; double word format
call DumpMem
The following output is produced:
00000001 00000002 00000003 00000004 00000005 00000006
00000007 00000008 00000009 0000000A 0000000B

5
Subject: Microprocessors Lecture No. : 10 Babylon University
Stage: Second Input-Output College of Information Technology
Class Room No.: 2 Department of Software
procedures in Irvine
Lecture Time: 8.30-10.30
Instructor: Dr. Ali Hadi Hasan
library

2.10. DumpRegs
The DumpRegs procedure displays the EAX, EBX, ECX, EDX, ESI, EDI, EBP,
ESP, EIP, and EFL (EFLAGS) registers in hexadecimal. It also displays the values
of the Carry, Sign, Zero, Overflow, Auxiliary Carry, and Parity flags. Sample
call:
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 AF=0 PF=1
The displayed value of EIP is the offset of the instruction following the call to
DumpRegs.
DumpRegs can be useful when debugging programs because it displays a
snapshot of the CPU. It has no input parameters and no return value.

2.11. StrLength
The StrLength procedure returns the length of a null-terminated string. Pass the
string’s offset in EDX. The procedure returns the string’s length in EAX. Sample
call:
.data
buffer BYTE "abcde",0
bufLength DWORD ?
.code
mov edx,OFFSET buffer ; point to string
call StrLength ; EAX = 5
mov bufLength,eax ; save length

3. Other procedures dealing with console window


and test/converting numbers.
3.1. WaitMsg
The WaitMsg procedure displays the message “Press any key to continue...” and
waits for the user to press a key. This procedure is useful when the programmer wants
to pause the screen display before data scrolls off and disappears. It has no input
parameters.
Sample call:
Call WaitMsg

3.2. Clrscr
The Clrscr procedure clears the console window. This procedure is typically
called at the beginning and end of a program. If you call it at other times, you may

6
Subject: Microprocessors Lecture No. : 10 Babylon University
Stage: Second Input-Output College of Information Technology
Class Room No.: 2 Department of Software
procedures in Irvine
Lecture Time: 8.30-10.30
Instructor: Dr. Ali Hadi Hasan
library

need to pause the program by first calling WaitMsg. Doing this allows the user to
view information already on the screen before it is erased.
Sample call:
call WaitMsg ; "Press any key..."
call Clrscr

3.3. Crlf
The Crlf procedure advances the cursor to the beginning of the next line in the
console window. It writes a string containing the ASCII character codes 0Dh and
0Ah.
Sample call:
Call Crlf

3.4. Delay
The Delay procedure pauses the program for a specified number of milliseconds.
Before calling Delay, set EAX to the desired interval.
Sample call:
mov eax,1000 ; 1 second
call Delay

3.5. IsDigit
The IsDigit procedure determines whether the value in AL is the ASCII code for a
valid decimal digit. When calling it, pass an ASCII character in AL. The procedure
sets the Zero flag if AL contains a valid decimal digit; otherwise, it clears Zero flag.
Sample call:
Mov AL,somechar
call IsDigit

3.6. MsgBox
The MsgBox procedure displays a graphical popup message box with an optional
caption. Pass it the offset of a string in EDX, which will appear in the inside the box.
Optionally, pass the offset of a string for the box’s title in EBX. To leave the title
blank, set EBX to zero.
Sample call:
.data
Caption db "Dialog Title", 0
HelloMsg BYTE "This is a pop-up message box.", 0dh,0ah
BYTE "Click OK to continue...", 0
.code
Mov ebx,OFFSET caption
mov edx,OFFSETHelloMsg

7
Subject: Microprocessors Lecture No. : 10 Babylon University
Stage: Second Input-Output College of Information Technology
Class Room No.: 2 Department of Software
procedures in Irvine
Lecture Time: 8.30-10.30
Instructor: Dr. Ali Hadi Hasan
library

call MsgBox
Sample output:

3.7. MsgBoxAsk
The MsgBoxAsk procedure displays a graphical popup message box with Yes and
No buttons. Pass it the offset of a question string in EDX, which will appear in the
inside the box. Optionally, pass the offset of a string for the box’s title in EBX. To
leave the title blank, set EBX to zero.
MsgBoxAsk returns an integer in EAX that tells the programmer which button
was selected by the user. The value will be one of two predefined Windows constants:
IDYES (equal to 6) or IDNO (equal to 7).
Sample call:
.data
caption BYTE "Survey Completed",0
question BYTE "Thank you for completing the survey."
BYTE 0dh,0ah
BYTE "Would you like to receive the results?",0
.code
mov ebx,OFFSET caption
mov edx,OFFSET question
call MsgBoxAsk ;(check return value in EAX)
Sample output:

8
Subject: Microprocessors Lecture No. : 10 Babylon University
Stage: Second Input-Output College of Information Technology
Class Room No.: 2 Department of Software
procedures in Irvine
Lecture Time: 8.30-10.30
Instructor: Dr. Ali Hadi Hasan
library

3.8. ParseDecimal32
The ParseDecimal32 procedure converts an unsigned decimal integer string to
32-bit binary. All valid digits occurring before a nonnumeric character are converted.
Leading spaces are ignored. Pass it the offset of a string in EDX and the string’s
length in ECX. The binary value is returned in EAX.
Sample call:
.data
buffer BYTE "8193"
bufSize = ($ - buffer)
.code
movedx,OFFSET buffer
movecx,bufSize
call ParseDecimal32 ; returns EAX
 If the integer is blank, EAX = 0 and CF = 1
 If the integer contains only spaces, EAX = 0 and CF = 1
 If the integer is larger than 232 -1, EAX = 0 and CF = 1
 Otherwise, EAX contains the converted integer and CF = 0

3.9. ParseInteger32
The ParseInteger32 procedure converts a signed decimal integer string to 32-bit
binary. All valid digits from the beginning of the string to the first nonnumeric
character are converted. Leading spaces are ignored. Pass it the offset of a string in
EDX and the string’s length in ECX. The binary value is returned in EAX.
Sample call:
.data
buffer BYTE "-8193"
bufSize = ($ - buffer)
.code
mov edx,OFFSET buffer
mov ecx,bufSize
call ParseInteger32 ; returns EAX
The string may contain an optional leading plus or minus sign, followed only by
decimal digits. The Overflow flag is set and an error message is displayed on the
console if the value cannot be represented as a 32-bit signed integer (range: -
2,147,483,648 to +2,147,483,647).

You might also like