Search code examples
emu8086

how to add two number using emu8086


Below the code add two even number also output would be below 10 but my challenge to know and show the output will be up 10 SO what would be the concept ? how i add two number but output show number extends up 10. can you give some idea? .model small .stack 100h .data msg1 db "The sum of$" msg2 db "and$" msg3 db "is:$" .code main proc

mov ax,@data
mov ds,ax

mov ah,9
lea dx,msg1
int 21h
         
mov ah, 2
mov dl,20h
int 21h

mov ah,1
int 21h
mov bl,al 
    
mov ah,9
lea dx,msg2
int 21h  

mov ah, 2
mov dl,20h
int 21h

mov ah,1
int 21h
mov cl,al

mov ah,9
lea dx,msg3
int 21h

mov ah, 2
mov dl,20h
int 21h

mov ah,2
mov dl,20h
int 21h    

add bl,cl
sub bl,30h

mov ah,2
mov dl,bl
int 21h

main endp
end main

Solution

  • Since you're still dealing with single character inputs ranging from "0" to "9" the largest number the sum can be is 18 (9 + 9). A simple check for a value larger than 9 will do the trick:

        mov  ah, 2    ;DOS display function
        add  bl, cl   ;Sum of 2 characters
        sub  bl, 30h  ;Remove the extra 30h
        cmp  bl, "9"
        jbe  PrintDigit
        mov  dl, "1"
        int  21h
        sub  bl, 10
    PrintDigit:
        mov  dl, bl
        int  21h
    

    Why do you output a space character directly after a string output? You know you can just as easily put this single space character in the messages!

    msg1 db "The sum of $"   <--- See the extra space before the $
    msg2 db "and $"          <--- See the extra space before the $
    msg3 db "is:  $"         <--- See the 2 extra spaces before the $