Reading a String 

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.
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 form the keyboard (see table).

BLALXXXXXXXXXXXXXXXXXXXXXXXXXXXX

where BL = Buffer Length and AL = Actual Length

Figure: Keyboard buffer structure

The following table summarizes string input and output functions:

Function 0AH Read from Keyboard
Entry AH = 0AH ; DX = address of keyboard input buffer
First byte of buffer contains the size of the buffer
(up to 255).
Exit Second byte of buffer contains the number of characters
read.
The string reading operation continues until the buffer is
full, or a carriage return (CR = 0DH) is hit.

Table: Functions 0AH of DOS interrupt


Example:

Here is an example that shows the use of function 0AH. The first part shows how to declare the buffer that will hold the string to be input throught the keyboard, in this case the string "hello".

Declaration part
.data
	buffer  db 8
	        db 9 dup(?)

After this declaration, the buffer would look like:

08XXXXXXXXXXXXXXXXXX

If after the following code:

Read from keyboard the string “hello”
	MOV AH, 0AH
	MOV DX, offset buffer	
	INT 21H

the user enters the string "hello", the output will be:

080568656C6C6F0DXXXX