Division in 8086 Assembly in MASM

16,509

Solution 1

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

Solution 2

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
Share:
16,509
user3226056
Author by

user3226056

Updated on June 04, 2022

Comments

  • user3226056
    user3226056 almost 2 years

    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
    
  • user3226056
    user3226056 about 10 years
    Oh! Thank you very much, Michael! Resetting (clearing) the Ah solved the problem. I didn't clear it before thinking that it would replace the existing value, though I was wrong. Thank you.