Search code examples
assemblyx86-16tasm

INT 21h 09h issue


I'm new in assembly and I can't understand why I get so strange output of this simple program. Program reads a line and then just has to print it back on a new line. I intentionally made io cycle infinite to perform the algorithm several times.

.model small
.stack 100h
.data

    msg db "Write something: ", "$"
    buf db 255, 0, 255 dup("$")
    newline db 13, 10, "$"

.code
start:
    mov ax, @data
    mov ds, ax

    io:
    mov ah, 09h
    mov dx, offset newline
    int 21h

    mov ah, 09h
    mov dx, offset msg
    int 21h

    mov ah, 0Ah
    mov dx, offset buf
    int 21h

    mov ah, 09h
    mov dx, offset newline
    int 21h

    mov ah, 09h
    mov dx, offset buf+2
    int 21h

    mov ah, 09h
    mov dx, offset newline
    int 21h

    jmp io

    done:
    mov ah, 4Ch
    int 21h

end start 

But the output I get is: enter image description here


Solution

  • You may want to reload the second byte of buf each time through the loop, the one dictating how many of the previous characters can be processed from the buffer.

    As per the excellent Ralf Brown's interrupt list, the int 21, fn 0a call modifies that byte to be the actual count of characters read (sans carriage return).

    Hence, the next time you call it, it's not going to be zero as you originally set it to.

    In fact, you probably want to reload the entire input buffer area since that call won't automatically terminate the string with $ - a shorter second input line may well still have the end of the first input line at the end.


    So basically, you need to modify the snippet which accepts input as follows (I've used C-like pseudo-code in case this is classwork):

    mov ah, 0Ah
    mov dx, offset buf
    ; store zero byte into buf re-use count: buf[1] = '\0'
    int 21h
    ; put '$' byte after end of string: buf[2 + buf[1]] = '$'