Search code examples
assemblyinputoutput

Why does the text flow one on top of the other when outputting the text in the assembly language?


Task: Write a com-program that first asks for your first name, after entering the name, asks for the last name, and then outputs: Hello, LAST NAME ENTERED FIRST NAME ENTERED.

But when I output the name, it appears at the beginning of the message (Hello). What could be the reason? If you display only the last name, everything is fine

[enter image description here](https://i.sstatic.net/T94zr.png)

org 100h
jmp start

first_name db 255,255,255 dup("$")
last_name db 255,255,255 dup("$")
msg db "Enter first name: $"
msg2 db 10,13,"Enter last name: $"
hello db 10,13, "Hello, $"

start:
; display "Enter first name" message
mov ah, 09h
mov dx, offset msg
int 21h

    ; read first name from user
    mov ah, 0Ah
    lea dx, first_name
    int 21h
    
    ; display "Enter last name" message
    mov ah, 09h
    mov dx, offset msg2
    int 21h
    
    ; read last name from user
    mov ah, 0Ah
    lea dx, last_name
    int 21h
    
    ; display "Hello" message
    mov ah, 09h
    mov dx, offset hello
    int 21h
    
    ; display last name
    mov ah, 09h
    lea dx, last_name
    add dx, 2h
    int 21h
    
    ; display first name
    mov ah, 09h
    lea dx, first_name
    add dx, 2h
    int 21h
    
    ; exit program
    mov ax, 4C00h
    int 21h

I'm new to assembler, so I watched videos on YouTube and read articles, but I didn't understand anything


Solution

  • first_name db 255,255,255 dup("$")
    

    It is always a bad sign to see a buffer getting preloaded with $ characters! It is tell-tale to someone wanting to cut corners in their program.

    When using the DOS.BufferedInput function 0Ah, after entering the name you have to push the Enter key. The ASCII code for this key gets added to the buffer. eg After inputting your name, the buffer would look like:

    255, 5, A, p, t, e, m, 13, $, $, $, $, ...
            ====================
    

    So what you then later print with the DOS.PrintString function 09h is the part that I underlined for you. It contains a carriage return (13) that brings the cursor back to the left side of the screen. Logically it follows that the next output will overwrite this.

    Read about the DOS.BufferedInput function 0Ah in How buffered input works.

    Solution

    Because you have 2 names that follow each other on the screen, it will make sense to replace the 13 that ends the last name with a space character, so there's some separation between the last name and the first name (in that order):

    ; display last name
    lea  si, last_name
    inc  si
    mov  bl, [si]            ; This is 5 if you inputted last name "Aptem"
    mov  bh, 0
    inc  si
    mov  byte [si+bx], " "   ; Replace 13 by 32
    mov  dx, si
    mov  ah, 09h
    int  21h