You are on page 1of 5

Lab Manual #7 Page 1 of 5

Name
CMS ID
Section
Marks

Input and Output Operations of 8086/8088 Assembly Language Cont’d

Introduction

The objective of this lab session is to make you comfortable with performing
some input/output operations using DOS INT 21H function calls.

After this lab you will be able to

 Take input from key board using functions calls 01H,08H and 0AH of INT 21H
 Show output on the screen using function calls 02H,06H and 09H of INT 21H
 Work with character strings

Last time we have seen how to output a string and a character on screen further we learned how to get a
character input from the user. A brief summary of I/O operation using INT 21H is given below:

Function Value of Output in Functionality


call No. AH
01h 01h AL Reads a character from keyboard, stores it in AL and
display it (echo it ) on screen.
02h,06h 02h,06h Screen Display the content of register DL on screen in ASCII
form.
08h 08h AL Read character from keyboard without echoing it on
Screen
09h 09h Screen Display the string that is terminated by”$” sign.
0Ah 0Ah Offset in Read a string of characters from keyboard.
DX

Now we are ready to learn how to get a string of characters as input from the user.

Reading a String:

Reading a string is accomplished by function 0AH, INT 21H. DOS function 0AH will accept a string of
text entered at the keyboard and copy that string into a memory buffer. DOS 0AH is invoked with DS: DX
pointing to an input buffer, whose size should be at least three bytes longer than the largest input string
anticipated.
Lab Manual #2 Page 2 of 5

Before invoking DOS function 0AH, you must set the first byte of the buffer with the number of character
spaces in the buffer. After returning from DOS function 0AH, the second byte of the buffer will contain a
value giving the number of characters actually read from the keyboard (Table 2.2).

Buffer Actual XX XX XX XX XX XX XX XX
Length Length
Anticipated
Figure 2.1: Keyboard buffer structure

Function 0AH Read from Keyboard


Before starting AH = 0AH ; DX = address of keyboard input buffer First byte of buffer contains the
size of the buffer (up to 255)
After ending Second byte of buffer contains the number of characters read. Reading operation
continues until buffer full, or a carriage return (CR = 0DH) is typed.
Table 2.2: Function 0AH of DOS Interrupt.

Example:
Below is an example on the use of function 0AH, when the user enters the word “hello”.

Buffer before input:

08 XX XX XX XX XX XX XX XX XX

MOV AH, 0AH


INT 21H
; Read from keyboard the word “hello”

Buffer after input:

08 05 68 65 6C 6C 6F 0D XX XX

Program 1:

.MODEL SMALL
.STACK 200
.DATA

PROMPT DB 'Enter your name(max. 9 characters): ', 0DH, 0AH, '$'


BUFFER DB 10, 11 DUP (?) ; Allocate 13 bytes for BUFFER ;
and put value 10 in the 1st byte.

.CODE
.STARTUP ; this directive initializes the DS and CS segments.

LEA DX, PROMPT ; display prompt


Microprocessor Systems laboratory (EE 351)
Lab Manual #2 Page 3 of 5

MOV AH, 09H


INT 21H

MOV AH, 0AH ; read into buffer


LEA DX,
BUFFER INT 21H

MOV AH, 4CH


INT 21H
END

Compile and run program 1 then go to view-›variables and select BUFFER from the window of
variables to see contents of this buffer are in the memory before and after running the program. What do
you see there? Write your observations here:

Program 2:

.MODEL SMALL
.STACK 100H
.DATA

CRLF DB 0DH, 0AH, '$'


PROMPT DB 'Enter your name (max. 9 characters):„, 0DH, 0AH, '$'
STRING1 DB 'Mr./Miss ','$'
STRING2 DB ' has ambitions to do something great for his/her country.','$'
BUFFER DB 10, 12 DUP (?)

.CODE
.STARTUP
LEA DX, PROMPT ; display prompt
MOV AH, 09H
INT 21H
Lab Manual #2 Page 4 of 5

MOV AH, 0AH ; read into buffer


LEA DX, BUFFER
INT 21H

LEA DX, CRLF ; move cursor to next line


MOV AH, 09H
INT 21H

LEA DX, STRING1 ; display string1


MOV AH, 09H
INT 21H

; Now display the buffer i.e. what has been read.

MOV AH, 09H


MOV BH, 00H
MOV BL, BUFFER [1] ; BX gets actual buffer length
MOV BUFFER [BX+2],'$' ; put a $ sign at the end of buffer
LEA DX, BUFFER [2] ; load actual start of string
INT 21H

LEA DX, STRING2 ; display string2


MOV AH, 09H
INT 21H

LEA DX, CRLF ; move cursor to next line


MOV AH, 09H
INT 21H

MOV AH, 02H ; display number of characters read if less than 10


MOV DL, BUFFER [1] ; read second byte of buffer
ADD DL, 30H ; convert to number
INT 21H

MOV AH, 4CH


INT 21H

END
Lab Manual #2 Page 5 of 5
Compile and run program 1 give your comments here:

Lab Assignment:

1. Prompt separately for first and second name of the user, both must be placed inside a
buffer separated by a space, also display that buffer to the user.

2. Read a string of 8 characters from the user then ask user to give you an index (1 to 8). Place your
CMS ID at that index and display the modified string.