Inputting A String of Data From Keyboard (Int 21h Option 0ah)
Inputting A String of Data From Keyboard (Int 21h Option 0ah)
This function enables input a string of data from the keyboard and to store it in the data
segment.
The register settings are:
AH=0AH
DX= offset address of the string to be stored (called as the buffer area)
Buffer area must be defined in the data segment
Ex:
.data
strinput db 6,?,6 dup (‘*’)
To read the input from keyboard you must add the following code:
lea dx, strinput
mov ah,0ah
int 21h
Sample Program:
dosseg
.model small
.stack 100h
.data
dsp1 db 'Enter a string: $'
strinput db 6,?, 6 dup ('*')
strinput db 6,?,6 dup (‘*’)
.code
main:
mov ax,@data
mov ds,ax
mov ah,4ch
int 21h
end main
If you run this program and you type “usa” as the string input you will get output like below.
Before you enter a string, the content of your memory in address “strinput” is
0 1 2 3 4 5 6 7
6 ? * * * * * *
strinput
0 1 2 3 4 5 6 7
6 3 75h 73h 61h 0dh * *
u s a CR
Explanation:
At index zero of strinput, the content of the memory is the maximum number
characters to be entered
At index 1, from ‘?’, it changed to 3 because you only entered three characters ‘u’,’s’
and ‘a’. In this index, the content is always the actual length of the string inputted.
Your input is always starts at index 2. And the characters you entered changed to its
equivalent ASCII code. In this case, 75h for character ‘u’, 73h for character ‘s’ and 61h
for character ‘a’
At index 5 is the carriage return or the “enter”. The ASCII code of “enter”is 13 or 0dh.
dosseg
.model small
.stack 100h
.data
dsp1 db 'Enter a string: $'
strinput db 6,?, 6 dup ('*')
strinput db 6,?,6 dup (‘*’)
.code
main:
mov ax,@data
mov ds,ax
;execute newline
mov dl, 10
mov ah,02h
int 21h
mov dl,strinput[2]
mov ah,02h
int 21h
mov ah,4ch
int 21h
end main
The above code displays only the first character of the inputted string because the inputted
string stores in the memory starting at index 2 of the strinput.
If you want to display the second character, just change the index from 2 to 3 then assemble,
link and run the program again.
;execute newline
mov dl, 10
mov ah,02h
int 21h
mov bh,0
mov bl,strinput[1]
add bx,1
mov ah,4ch
int 21h
end main
The code below is used to copy the value at strinput[1] to register BX. But because BX and
strinput[1] is not match in size, BL is used to hold the value at strinput[1] and BH is set to 0. So
BX is equal to 0003h.
mov bh,0
mov bl,strinput[1]
add bx,1
We add 1 to BX to point the BX to last inputted character. In this case, BX is pointed to
character ‘a’. Now, BX = 0004h
bck: mov dl,strinput[bx]
mov ah,02h
int 21h
dec bx
cmp bx,1
ja bck
In the above code, it displays each character from the last inputted character up to the first
inputted character.
0 1 2 3 4 5 6 7
6 3 75h 73h 61h 0dh * *
BX u s (bx>
Condition a 1) ifCR strinput[bx] Display
true, jump to bck
0004 true strinput[4] = ‘a’ a
0003 true strinput[3] = ‘s’ as
0002 true strinput[2] = ‘u’ asu
0001 false -------------- asu
Exercises
1. Make a program that will accept string and an output like below.
sample output:
Enter a string: hello
h
e
l
l
o
2. Make a program that accepts a word or phrase and display in uppercase letters.
Sample output:
Enter a string: ComputeR
COMPUTER
3. Make a program that accepts a word or phrase and display in lowercase letters.
Sample output:
Enter a string: CoMpuTeR
computer
4. Make a program that accepts string and replace all the vowels with corresponding
numbers. a = 1, e = 2, i = 3, o = 4, u = 5. Enter lower case letters only.
sample output:
Enter a string: computer engineering
c4mp5t2r 2ng3n22r3ng
5. Make a program that will accept a string and displays only the vowel letters.
sample output:
Enter a string: make your mom proud
aeouoou
6. Make a program that accepts two string and display whether the two inputted strings
are equal.
sample output:
Enter a string: abc
Enter a string: cba
Two strings are not equal.