The accepted answer is wrong in that it displays the result reversed! That's why I write this answer today. My answer demonstrates the better technique of outputting the result in one gulp (via DOS.function 09h).
Your code
mov ah,2 ; 2 is the function number of output char in the DOS Services.
mov dl, ax ; DL takes the value.
int 21h ; calls DOS Services
Always load the DOS function number in the instruction right before the int 21h
instruction.
Always check your sizes: you cannot move from a 16-bit register to an 8-bit register.
I think that dl
value should be in ASCII code, but I'm not sure how to convert ax
value after addition into ASCII.
Your numbers allow the use of the DOS.PrintCharacter function. To convert the value 3 into the character "3", you just need to add 48 so as to have an ASCII code in DL.
When I add two values in 16-bit assembly, what is the best way to print the result to console?
Your example for a single-digit result
; Sum
mov al, 1
add al, 2
; Print
add al, '0'
mov dl, al
mov ah, 02h ; DOS.PrintCharacter
int 21h
; Exit
mov ax, 4C00h ; DOS.TerminateWithExitcode
int 21h
Solution for a multi-digit result
; Sum
mov ax, 7346
add ax, 913
; Print
mov bx, Buffer + 5 ; Position of the mandatory '$' terminator
mov cx, 10
More:
xor dx, dx
div cx
dec bx
add dl, '0' ; Convert to ASCII
mov [bx], dl
test ax, ax
jnz More
mov dx, bx
mov ah, 09h ; DOS.PrintString
int 21h
; Exit
mov ax, 4C00h ; DOS.TerminateWithExitcode
int 21h
; Storage
Buffer: db '.....$'
NASM use mov bx, Buffer + 5
MASM use mov bx, OFFSET Buffer + 5