Division in 8086 Assembly in MASM

user3226056 picture user3226056 · Feb 9, 2014 · Viewed 15.6k times · Source

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

Answer

Michael picture Michael · Feb 9, 2014

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