assemblymasmtasmdosbox

Asssembly code printing symbols instead of numbers


I am using masm/tasm combo in vs code to run this following code to print numbers from 0-9 each in a new line.

.model small
.stack 100h
.data
.code
main proc
    mov cx, 10          ; Loop counter (10 times)
    mov dx, 48          ; ASCII value for '0'

print_digits:
    ; Print the digit
    mov ah, 2           ; DOS function: Print character
    int 21h             ; Call DOS interrupt



    
    mov dl,10
    mov ah,2
    int 21h

    mov dl,13
    mov ah,2
    int 21h

    add dx,1
    loop print_digits   ; Loop until CX is 0


    ; Exit program
    mov ah, 4Ch         ; DOS function: Terminate program
    int 21h             ; Call DOS interrupt

main endp
end main

but it outputs weird symbols instead of numbers.

tried making a function for newline and chaning registers.


Solution

  • DL is not a separate register. It is the low half of the DX register (the high half is named DH). That's why you loose the current digit while outputting the linefeed and the carriage return, which on DOS you would normally output carriage return first followed by linefeed.

    There are more than enough registers available to you, so pick eg. BL to hold the digit:

        mov cx, 10          ; Loop counter (10 times)
        mov bl, 48          ; ASCII value for '0'
    print_digits:
        mov dl, bl
        mov ah, 2           ; DOS function: Print character
        int 21h             ; Call DOS interrupt
        mov dl, 13
        mov ah, 2
        int 21h
        mov dl, 10
        mov ah, 2
        int 21h
        inc bl
        loop print_digits   ; Loop until CX is 0
    

    The better solution does not use a separate loop counter, but iterates based on the current digit:

        mov bl, '0'
        mov ah, 02h         ; DOS function: Print character
    print_digits:
        mov dl, bl
        int 21h             ; Call DOS interrupt
        mov dl, 13
        int 21h
        mov dl, 10
        int 21h
        inc bl
        cmp bl, '9'
        jbe print_digits