Reputation: 63
As far as I have heard, if we use div instruction with a 8-bit number then the quotient is a 8-bit number stored in AL and the remainder is again a 8-bit number stored in AH
But what if we divide a 16-bit number by 1?
I ended up having my instruction pointer getting misplaced whenever the below code is executed.
MOV AX, 9999H
MOV BL, 1
DIV BL
Is there any way we can force 8086 to use ax for quotient and do for remainder while dividing by AX?
Upvotes: 3
Views: 4008
Reputation: 39411
On the 8086 processor you can choose between a byte-sized division and a word-sized division.
The byte-sized division will divide the word in AX
by whichever byte-sized operand you specify. Your example code uses this kind of division. The quotient goes to the AL
register and the remainder goes to the AH
register.
In your example dividing 9999h by 1 yields a quotient of 9999h. This result is too big to fit in AL
and therefore you got a #DE division exception.
The word-sized division will divide the doubleword in DX:AX
by whichever word-sized operand you specify. The quotient goes to the AX
register and the remainder goes to the DX
register.
Your example:
XOR DX, DX
MOV AX, 9999H
MOV BX, 1
DIV BX
This time there's no problem and the AX
register will hold the quotient 9999h and the DX
register will hold the remainder 0.
EDIT after comment
I'm sorry to be unclear...What I am actually trying to do is divide a 8 bit register by a 16-bit register in order to obtain a 16-bit quotient...i dont need the remainder
An example would then be BL
/ CX
producing AX
If that is what you want to do, then be aware that if the 16-bit divider (CX
) holds a value greater than the value in the 8-bit dividend (BL
), the quotient from this integer division (AX
) will always be zero. Not very spectacular!
The trick is to extend the dividend so we can use the word-sized division because that's the one that uses the desired 16-bit divider.
mov al, bl ; \
mov ah, 0 ; / sets AX=BL
cwd ; sets DX=0
div cx ; divides DX:AX by CX producing quotient AX
As an alternative and provided that CH
is zero:
mov al, bl ; \
mov ah, 0 ; / sets AX=BL
div cl ; divides AX by CL producing quotient AL
mov ah, 0 ; desired 16-bit quotient AX
EDIT 2 after comment
AX is a 16 bit number...BL is the 8-bit number...Perform some instructions DIV BL.. AX is now the 16-bit quotient
If you absolutely want to perform DIV BL
, it will always calculate AX / BL
producing an 8-bit quotient in AL
and an 8-bit remainder in AH
. There's nothing you can do to change that.
Now, as you've said before, if you don't need that remainder and desire the quotient in the whole AX
register, all you have to add is zeroing the AH
register.
div bl
mov ah, 0 ; Quotient is now AX
Upvotes: 5