Mail Archives: djgpp/1997/09/28/19:07:20
At 12:26 9/26/1997 +0200, votava wrote:
>I have a problem with nasm: when I try DIV CX, I have the message
>"division by 0" but Cx have not a null value. Can someone can help me???
This is not NASM's fault; it is a design stupidity on the part of the 8086
and its descendents. The DIV instruction will generate a Divide By Zero
error (INT 0) anytime the quotient of the division is too big to fit in the
result register. Go figure. I.E this code will give a divide by zero, though
that is clearly not the case:
mov dx,1000h
mov ax,0000h
mov cx,2
div cx ; gives a divide by zero.
The best workaround I can think of is to do a 64 by 32 bit divide, instead
of 32 by 16 (assuming you have a 386 or higher). Instead of this code:
dividend dd NNNNNN ; the dividend
mov dx,[dividend] ; load high half
mov ax,[dividend+2] ; load low half
mov cx,DIVISOR
div cx ; leaves quotient in ax and remainder in dx
Use this:
dividend dd NNNNN ; still the dividend
mov eax,[dividend] ; load the dividend (low half of operand)
sub edx,edx ; zero edx, the high half
mov ecx,DIVISOR ; now a 32-bit value
div ecx ; leaves quotient in eax and remainder in edx
Hope this helps.
Nate Eldredge
eldredge AT ap DOT net
- Raw text -