Displaying two digit numbers in assembly?

13,405

I'm also new to assembly. But I think this will help you.

 
.model small
.stack 100h
.data 
    msg1 db "Enter number 1:$"
    msg2 db "Enter number 2:$"
    msg3 db "Sum is:$"
    no1 db 0
    no2 db 0
    mysum db 0
    rem db 0


.code 
    mov ax,@data 
    mov ds,ax

;print msg 1
    mov dx,offset msg1 
    mov ah,09h
    int 21h

;read input no1
    mov ah,01h
    int 21h
    sub al,48
    mov no1,al

;print new line
    mov dl,10
    mov ah,02h
    int 21h

;print msg2
    mov dx,offset msg2
    mov ah,09h
    int 21h

;read input 2
    mov ah,01h
    int 21h
    sub al,48
    mov no2,al

;print new line 
    mov dl,10
    mov ah,02h
    int 21h

;print msg3
    mov dx,offset msg3
    mov ah,09h
    int 21h

;add two numbers
    mov dl,no1
    add dl,no2
    ;moving the sum to mysum
    mov mysum,dl

    ;clear AH to use for reminder
    mov ah,00
    ;moving sum to al
    mov al,mysum
    ;take bl=10
    mov bl,10
    ;al/bl --> twodigit number/10 = decemel value
    div bl
    ;move reminder to rim
    mov rem,ah
    ;to print (al) we move al to dl
    mov dl,al
    add dl,48
    mov ah,02h
    int 21h

    ;to print the reminder
    mov dl,rem
    add dl,48
    mov ah,02h
    int 21h

    mov ax,4c00h
    int 21h
end

here what I've done is I took the total and move it to al that can keep it. then I divide it by 10 and print the quotient and reminder. If you feel problem. you can ask. Thank you !

Share:
13,405
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I am completely new to assembly programming. In of the examples at classwork its required to add two numbers and display the sum, what I find cryptic is display the sum when its a two digit number. Here's my code.

        mov al,num1
        mov bl,num2
    
        add al,bl
    
        add ax,3030h
    
        mov dl,ah
        mov ah,02h
        int 21h
    
        mov dl,al
        mov ah,02h
        int 21h
    
        mov ah,4ch
        int 21h
    

    While the addition might result in a packed number, how do I unpack it and display as two different numbers in decimal?

  • Peter Cordes
    Peter Cordes about 5 years
    div bl calculates AX / BL, not AL / BL. That's why you have to zero AH first. Storing the remainder in AH is a write-only operation. And BTW, you can just add into AL in the first place; you don't need a mysum static storage location. Use registers to hold your data, that's what they're for.