A string can be read from the keyboard via functions / interrupts:
0Ah/21h
: bufferized entrance
03Fh/21h
: reading file or device
08h/21h
: reading character by character (manual loop)
4810h/2Fh
: bufferized input using DOSKEY (demand the DOSKEY TSR
installed)
Given the type of problem reported, I assume you are reading the input through buffer 0Ah/21h
.
For the buffer to be used in this reading there are certain requirements to be followed:
- The first byte will tell you the maximum length of characters to be
readings.
- The second byte will return the number of bytes actually read.
- From the third byte is that actually starts the string read.
A buffer declared in the following format does not serve:
BUFFER db 255 DUP('$')
A good buffer for string reading will have the following form:
BUFFER db 255 ; Número máximo de elemementos a serem lidos.
db ? ; Número de elementos lidos retornado pela interrução sem incluir o enter.
STRING db 255 DUP(0) ; Variável armazenando o texto lido.
Later, in possession of the number of characters read, the substitution of enter will be made chr(13)
end of buffer by string terminator.
The code below performs the reading and printing of a string, commenting on the procedures followed.
DOSSEG
.MODEL SMALL
.STACK
.DATA
BUFFER db 255 ; Número máximo de elemementos a serem lidos.
db ? ; Número de elementos lidos retornado pela interrução sem incluir o enter.
STRING db 255 DUP(0) ; Variável armazenando o texto lido.
TERMINADOR db 10D,13D,'$'
.CODE
inicio:
; Inicializa o segmento de dados.
mov ax,SEG _DATA
mov ds,ax
; Lê entrada no buffer.
mov dx,OFFSET BUFFER
mov ah,0ah
int 21h
; Substitui enter chr(13) por término de string.
mov si, offset BUFFER + 1 ; Carrega em si o número de elementos lidos.
mov cl, [si] ; Armazena em cl o número de elementos lidos.
mov ch, 0 ; Zera ch para poder usar cx.
inc cx ; cx passa a armazenar a posição do enter chr(13) final do buffer
add si, cx ; si aponta para a posição
mov al, '$'
mov [si], al ; substitui o enter chr(13) pelo terminadore de string '$'
; imprime terminador
mov dx, OFFSET TERMINADOR
mov ah,09h
int 21h
; imprime string
mov dx,OFFSET STRING
mov ah,09h
int 21h
mov ah,4ch ; funcao de termino
int 21h ; interrupcao do DOS
END inicio
The code has been tested using the compiler MASM 5.00 in this emulator with Dosbox