Search code examples
assemblybufferdosdosboxx86-16

Read buffer from keyboard in Assembly


I need to read string from a keyboard with a buffer.

With some help of examples and literature, I managed to write this code, but when I try to print out the string that I've inserted, it just gives me message "Input your string again:".

What should I change in my code, in order to get printed what I've inserted?

.model small

stack 100h

.data

    ;reading buffer
    buffSize DB 255       ;Number of maximum characters than can be read
    read DB ?             ;Number of characters that was read
    buffer  DB 255 dup (?) ;Read characters are placed here

    ;Other data
    input DB "Input your string: $",13,10
.code

Start:

    MOV ax,@data
    MOV ds,ax

    MOV ah,9
    MOV dx, OFFSET input
    INT 21h                     ;Prints input string

    MOV ah, 0Ah
    MOV dx, OFFSET buffSize
    INT 21h                     ;Text is read

    MOV bx, OFFSET buffer       ;Address of buffer is inserted to bx
    MOV cl, read                ;Content of read is inserted to cl
    MOV ch, 0                   ;In cl there is a number of inserted characters

    MOV byte ptr [ds:bx], '$'

    MOV ah, 9
    MOV dx, OFFSET buffer
    INT 21h

    MOV ah,4Ch
    INT 21h
END Start

Solution

  • input DB "Input your string: "
    nextLine db "$",13,10
    

    The nextLine won't do much. Put the "$" behind the 13,10.
    Best also delimit the input text on the same line.

    input    db "Input your string: $"
    nextLine db 13,10,"$"
    

    buffSize DB 255       ;Number of maximum characters than can be read
    read DB ?             ;Number of characters that was read
    buffer  DB 25 dup (?) ;Read characters are placed here
    

    If you allow for an input of 255 characters, you should also define a buffer large enough! 25 dup (?) is much smaller than the stated size of 255.


    To re-print the input, put a $ behind the text:

    mov  bx, OFFSET buffer     ;Start of the text
    add  bl, read              ; plus the number of characters in the text
    adc  bh, 0                 ;Maybe there was a carry from previous addition!
    mov  byte ptr [bx], "$"    ;DOS needs string $-terminated
    mov  ah, 09h               ;DOS.DisplayString
    mov  dx, OFFSET buffer
    int  21h