Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Computer System Organization with Assembly Language

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 (‘*’)

Elements of a Buffer Area


 The first entry provides the name of the variable. (strinput)
 The first byte contains your limit for the maximum number of input
characters. The minimum is 0 and, because this is a 1-byte field, the
maximum is FFH, or 255.
 The second byte is for the operation to store the actual number of
characters typed as a binary value.
 The third byte begins a field that is to contain the typed characters, from
left to right.

 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

;display the label first


lea dx, dsp1
mov ah,09h
int 21h

;read the string input


lea dx, strinput
mov ah,0ah
int 21h

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.

Enter a string: usa

Before you enter a string, the content of your memory in address “strinput” is

0 1 2 3 4 5 6 7
6 ? * * * * * *

strinput

After you enter a string, the content now will be

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.

Now let’s try to modify our code with the following:

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

;display the label first


lea dx, dsp1
mov ah,09h
int 21h

;read the string input


lea dx, strinput
mov ah,0ah
int 21h

;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.

Let’s try another code:


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

;display the label first


lea dx, dsp1
mov ah,09h
int 21h

;read the string input


lea dx, strinput
mov ah,0ah
int 21h

;execute newline
mov dl, 10
mov ah,02h
int 21h

mov bh,0
mov bl,strinput[1]
add bx,1

bck: mov dl,strinput[bx]


mov ah,02h
int 21h
dec bx
cmp bx,1
ja bck

mov ah,4ch
int 21h
end main

The above code displays the inputted string in reverse order.


Sample Output:
Enter a string: usa
asu

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.

You might also like