Reading An Input from the keyboard and displaying
To read input from the keyboard using assembly language, we need to understand first how memory is alocated for a string. Basically, each character of the string is stored in one memory location, the string is basically ordered the way you have entered it. But the begining of that alocation is not the beginning of the string. In actuality, the first two alocation in the memory for your string is actually reserved. The second alocation, specifically index 1 (the first alocation starts with index 0), is stored with the length of the string you have entered. The first character of your string would therefore be the third memory alocation, therefore index 2. So here is the sample code.
For example, we have an identifier able to accept 10 characters.
STR1 DB 11, 12 DUP('$')
STR2 DB 0AH, 0DH, '$'
So here is the sample code.
MOV AX, dseg ;initialize the dseg (data segment) to DS register
MOV DS, AX
LEA DX, STR1 ;load the effective address of str to dx to be ready for reading
MOV AH, 0AH ;prepare the function for reading an input(string)
INT 21H ;trigger the function
LEA DX, STR2 ;print str2 for new line
MOV AH, 09H
INT 21H
LEA DX, STR1[2] ;load the effective address of str but starting at the index 2
MOV AH, 09H ;prepare the fun
ction for printing a string
INT 21H ;trigger the function
MOV AH, 4CH ;exit
INT 21H
Sample output:
0 comments:
Post a Comment