Question

I am writing this assembly program in 8086, but it is not working properly. The quotient and remainder prints out as some random symbols even though I use single digit numbers. Can someone please point out the errors/mistakes in the program? Thank you.

.model small
.stack 50h

.data
Divisor db ?
Dividend db ?
Quotient db ?
Remainder db ?

.code
main_method   proc
              mov    ax, @data
              mov    ds, ax

              mov    ah, 01
              int    21h
              sub    al, 48
              mov    Divisor, al

              mov    ah, 01
              int    21h
              sub    al, 48
              mov    Dividend, al
              mov    bl, 00
              mov    al, 00
              mov    bl, Divisor
              mov    al, Dividend
              div    bl

              mov    Quotient, al
              mov    Remainder, ah

              mov    dl, Quotient
              add    dl, 48
              mov    ah, 02
              int    21h

              mov    dl, Remainder
              add    dl, 48
              mov    ah, 02
              int    21h
              mov    ah, 4ch
              int    21h
main_method   endp
              end    main_method
Était-ce utile?

La solution

DIV BL divides the 16-bit value in AX by BL, so you should clear those bits of AX that you're not using (in this case the entire upper byte). So right before the DIV, add either:

MOV AH,0

or

XOR AH,AH  ; XORing something with itself clears all bits


Or, if you're targetting 80386 or above you can replace Mov Al, Dividend with MOVZX AX, BYTE PTR Dividend

Autres conseils

Basically DIV function divide AX and then put quotient in AL and remainder in AH. AX consists of AH and AL. So if you only want to divide AL then you have to make sure that AH is 0 . You can do below method to make something zero.

MOV AH, 0

OR

AND AH, 0

OR

XOR AH, AH
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top