0

I can't understand how which appropriate registers to use in storing input and how to use the DIV. What is wrong with my code? Would very much appreciate an explanation. Also, I don't know how to input digits more than 9. How should I do that?

Title PROGRAM TO DIVIDE TWO NUMBERS
dosseg
.model small
.stack 100h
.data
        string1 db 10,13,'Enter Dividend:$'
        string2 db 10,13,'Enter Divisor:$'
        string3 db 10,13,'Quotient:$'  
        quo db 0
        rem db 0
.code
main proc
    mov ax, @data
    mov ds, ax

    lea dx,string1  ;display string1
    mov ah, 09
    int 21h

    mov ah,1      ;store dividend in al
    int 21h
    mov bh,al   ;moves data al -> bh

    lea dx,string2  ;display string 2
    mov ah, 09
    int 21h

    mov ah,1      ;store quotient in al
    int 21h
    mov bl,al   ;moves data al -> bl

    div bl      ; ax / bl

    mov quo, al ; quotient is stored in al
    mov rem, ah ; remainder is stored in ah

    mov dl,quo  ; output quotient
    sub dl, 48  ; convert ASCII to symbol
    mov ah, 2   
    int 21h     

    lea dx,string3
    mov ah, 09
    int 21h

    mov dl, 32  ; print space
    mov ah, 2
    int 21h
    

    mov dl,'r'  ; print char r
    mov ah, 2
    int 21h

    mov dl, rem ; output remainder
    add dl, 48
    mov ah, 2
    int 21h

    mov ah, 4ch ; exit
    int 21h     ; interrupt
main endp        
end main

I am using DOSBox to run the code and it assembles, but the output is wrong.

Boann
  • 48,794
  • 16
  • 117
  • 146
Nick
  • 9
  • 5
  • 4
    You write `div bl ; ax / bl` but you didn't initialize ax. – Raymond Chen Feb 21 '22 at 18:28
  • 1
    You can input a number greater than 9 (having several digits) by accumulating it. For each digit entered multiply the accumulator by 10 and add the digit value. Outputting the quotient is a bit more tricky, but a similar reverse process involving repeatedly dividing by 10 until it's zero, storing each remainder. Then output in reverse order (adjusting for ASCII). – Weather Vane Feb 21 '22 at 19:14
  • Before your div bl, you need to set ax: ` mov ah, 0 mov al, bh ` – Thomas Kjørnes Feb 22 '22 at 02:23

0 Answers0