www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/28/19:07:20

Date: Sun, 28 Sep 1997 15:03:40 -0700 (PDT)
Message-Id: <199709282203.PAA14912@adit.ap.net>
Mime-Version: 1.0
To: votava AT worldnet DOT fr, djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: problem with nasm and djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019