05 LibraryProcedures
05 LibraryProcedures
COE 205
Computer Organization and Assembly Language
Console Window
Text-only window created by MS-Windows (cmd.exe program)
The Irvine32.lib writes output to the console (standard output)
The Irvine32.lib reads input from the keyboard (standard input)
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 8
Output Procedures
Procedure Description
Clrscr Clears screen, locates cursor at upper left corner.
Crlf Writes end of line sequence (CR,LF) to standard output.
WriteChar Writes character in register AL to standard output.
WriteString Writes a null-terminated string to standard output.
String address should be passed in register EDX.
.data
str1 BYTE "Assembly language is easy!",0
.code
mov dx, OFFSET str1
call WriteString
call Crlf
.data
str1 BYTE "Assembly language is easy!",13,10,0
.code
mov dx, OFFSET str1 CR LF
call WriteString No need to call Crlf
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 10
Example: Displaying an Integer
.code
mov ax, -1000
call WriteBin ; display binary
call Crlf
call WriteHex ; display hexadecimal
call Crlf
call WriteInt ; display signed decimal
call Crlf
call WriteDec ; display unsigned decimal
call Crlf
Sample output
1111 1111 1111 1111 1111 1100 0001 1000
FFFFFC18
-1000
4294966296
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 11
Input Procedures
Procedure Description
ReadChar Reads a char from keyboard and returns it in the AL register.
The character is NOT echoed on the screen.
ReadHex Reads a 32-bit hex integer and returns it in the EAX register.
Reading stops when the user presses the [Enter] key.
No leading spaces. No error checking is performed.
ReadInt Reads a 32-bit signed integer and returns it in EAX.
Leading spaces are ignored. Optional + or – is allowed.
Error checking is performed (error message) for invalid input.
ReadDec Reads a 32-bit unsigned integer and returns it in EAX.
ReadString Reads a string of characters from keyboard.
Additional null-character is inserted at the end of the string.
EDX = address of array where input characters are stored.
ECX = maximum characters to be read + 1 (for null byte)
Return EAX = count of non-null characters read.
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 12
Example: Reading a String
Before calling ReadString …
EDX should have the address of the string.
ECX specifies the maximum number of input chars + 1 (null byte).
.data
inputstring BYTE 21 DUP(0) ; extra 1 for null byte
actualsize DWORD 0
.code
mov dx, OFFSET inputstring
mov cx, SIZEOF inputstring
call ReadString
mov actualsize, ax
DumpMem
Writes a range of memory to standard output in hexadecimal
SI = starting address
CX = number of elements to write
BX = element size (1, 2, or 4)
.code
mov si, OFFSET array
mov cx, LENGTHOF array
mov bx, TYPE array
call DumpMem
Console Output
Dump of offset 00405000
-------------------------------
0000 000A 04D2 3CFF 0000 000A 04D2 3CFF
Random32
Generates an unsigned pseudo-random 32-bit integer
Returns value in EAX = random (0 to FFFFFFFFh)
RandomRange
Generates an unsigned pseudo-random integer from 0 to n – 1
Call argument: EAX = n
Return value in EAX = random (0 to n – 1)
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 16
Example on Random Numbers
Generate and display 5 random numbers from 0 to 999
mov cx, 5 ; loop counter
L1: mov ax, 1000 ; range = 0 to 999
call RandomRange ; ax = random integer
call WriteDec ; display it
call Crlf ; one number per line
loop L1
Console Output
194
702
167
257
607
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 17
Additional Library Procedures
Procedure Description
WaitMsg Displays "Press [Enter] to Continue …" and waits for user.
SetTextColor Sets the color for all subsequent text output.
Bits 0 – 3 of AX = foreground color.
Bits 4 – 7 of AX = background color.
Delay Delay program for a given number of milliseconds.
AX = number of milliseconds.
GetMseconds Return in AX the milliseconds elapsed since midnight.
Gotoxy Locates cursor at a specific row and column on the console.
DH = row number
DL = column number
GetMaxXY Return the number of columns and rows in console window buffer
Return value DH = current number of rows
Return value DL = current number of columns
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 18
Example on TextColor
Display a null-terminated string with
yellow characters on a blue background
.data
str1 BYTE "Color output is easy!",0
.code
mov ax, yellow + (blue * 16)
call SetTextColor
call Clrscr
mov dx, OFFSET str1
call WriteString
call Crlf
BEFORE AFTER
0012FFB4 0012FFB4
BEFORE AFTER
0012FFC4 0012FFC4
0012FFB4 0012FFB4
. . . ; outer loop
pop cx ; restore outer loop count
loop L1 ; repeat the outer loop
popfd
Pop the 32-bit EFLAGS
procedure_name PROC
. . .
; procedure body
. . .
procedure_name ENDP
Preconditions
Must be satisfied before the procedure is called
;------------------------------------------------
; Sumof: Calculates the sum of three integers
; Receives: EAX, EBX, ECX, the three integers
; Returns: EAX = sum, and the flags:CF,OF,ZF,SF
; Requires: nothing
;------------------------------------------------
sumof PROC
add AX, BX ; AX = AX + second number
add AX, CX ; AX = AX + third number
ret ; return to caller
sumof ENDP
Runtime Stack
ESP
Allocated
00401081 03 C3 add EAX, EBX
00401083 03 C1 add EAX, ECX ESP RA=00401036
00401085 C3 ret Free Area
sumof ENDP
END main
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 42
Don’t Mess Up the Stack !
Just before returning from a procedure
Make sure the stack pointer ESP is pointing at return address
Example of a messed-up procedure
Pushes EAX on the stack before returning
Stack pointer ESP is NOT pointing at return address!
main PROC Stack
call messedup
. . . high addr
exit Used
ESP
main ENDP ESP Return Addr
messedup PROC ESP EAX Value Where to return?
push EAX Free Area
EAX value is NOT
ret the return address!
messedup ENDP
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 43
Nested Procedure Calls
main PROC
.
.
call Sub1 By the time Sub3 is called, the stack
exit contains all three return addresses
main ENDP
Sub1 PROC
. return address of call Sub1
.
call Sub2
ret return address of call Sub2
Sub1 ENDP
return address of call Sub3 ESP
Sub2 PROC
.
.
call Sub3
ret
Sub2 ENDP
Sub3 PROC
.
.
ret
Sub3 ENDP
ArraySum PROC
ArraySum PROC USES esi ecx push esi
mov eax,0 push ecx
L1: add eax, [esi] mov eax,0
add esi, 4 L1: add eax, [esi]
loop L1 add esi, 4
ret loop L1
ArraySum ENDP pop ecx
pop esi
ret
ArraySum ENDP
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 49
Next . . .
Structure Chart
Above diagram is called a structure chart
Describes program structure, division into procedure, and call sequence
Link library procedures are shown in grey
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 52
Integer Summation Program – 1 of 4
INCLUDE Irvine32.inc
ArraySize EQU 5
.DATA
prompt1 BYTE "Enter a signed integer: ",0
prompt2 BYTE "The sum of the integers is: ",0
array DWORD ArraySize DUP(?)
.CODE
main PROC
call Clrscr ; clear the screen
mov esi, OFFSET array
mov ecx, ArraySize
call PromptForIntegers ; store input integers in array
call ArraySum ; calculate the sum of array
call DisplaySum ; display the sum
exit
main ENDP
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 53
Integer Summation Program – 2 of 4
;-----------------------------------------------------
; PromptForIntegers: Read input integers from the user
; Receives: ESI = pointer to the array
; ECX = array size
; Returns: Fills the array with the user input
;-----------------------------------------------------
PromptForIntegers PROC USES ecx edx esi
mov edx, OFFSET prompt1
L1:
call WriteString ; display prompt1
call ReadInt ; read integer into EAX
call Crlf ; go to next output line
mov [esi], eax ; store integer in array
add esi, 4 ; advance array pointer
loop L1
ret
PromptForIntegers ENDP
Libraries and Procedures COE 205 – KFUPM
© Muhamed Mudawar – slide 54
Integer Summation Program – 3 of 4
;-----------------------------------------------------
; ArraySum: Calculates the sum of an array of integers
; Receives: ESI = pointer to the array,
; ECX = array size
; Returns: EAX = sum of the array elements
;-----------------------------------------------------
ArraySum PROC USES esi ecx
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