🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.msdos.programmer
Article #853

Re: This is strange, div doesn't work....

#853
From: Richard Czegledi
Date: Sun, 20 Dec 1992 18:00
48 lines
2147 bytes
>>In article <1992Dec18.094825.14710@miavx1.acs.muohio.edu> sjmadsen@apsvax.aps.muohio.edu (Steve Madsen) writes:
>>>Michael Duffy (mduffy@aludra.usc.edu) wrote:
>>>: The following code gives me a divide error:
>>>: 
>>>:  mov  ax,0035h
>>>:  mov  dx,01h
>>>:  div  dx
>>>:
[deleted text]

As Joi Ellis pointed out:
>The problem here is that it is illegal to use DX for the operand in a
>sixteen-bit divide, because the microcode will mangle it in the course
>of the operation.  If he uses
>   DIV CX
>instead of 
>   DIV DX
>it will work just fine.
>
>BTW:  How would he check what the quotient will be over 16 bits, on a 16
>bit CPU??  It doesn't *care*.

I believe that another problem results from a 32-bit division by a 16-bit
divisor.  You could use a variant of the following code to perform an
unsigned 32-bit by 16-bit division:

 
Assume  (1) dividend is in DX:AX 
        (2) TEMP is some effective address (not AX or DX)
        (3) DIVISOR is some effective address (not AX or DX)
 
    mov    TEMP, ax	; save lower 16 bits
    mov    ax, dx   ; DX:AX >>= 16
    xor    dx, dx   ; DX:AX &= 0xFFFF
    div    DIVISOR  ; AX = DX:AX/DIVISOR; DX = DX:AX % DIVISOR
    xchg   ax, TEMP ; t=AX; AX=TEMP; TEMP=t

    cmp    dx, DIVISOR  ; check if an overflow will occur
    jae    overflow
 
    div    DIVISOR
    xchg   dx, TEMP
	
    ; dividend is in DX:AX
    ; remainder is in TEMP 

I haven't tested this routine but I think that will perform the necessary
division or branch to the overflow label if an error will occur.
Technical Details
Message-ID: <BzKKo7.n43@undergrad.math.waterloo.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!noc.near.net!hri.com!spool.mu.edu!sdd.hp.com!cs.utexas.edu!torn!watserv2.uwaterloo.ca!watmath!undergrad.math.waterloo.edu!rtczegle
References: <1992Dec18.094825.14710@miavx1.acs.muohio.edu> <BzIxL9.28B@undergrad.math.waterloo.edu> <BzJnMz.Gq4@uxa.ecn.bgu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!noc.near.net!hri.com!spool.mu.edu!sdd.hp.com!cs.utexas.edu!torn!watserv2.uwaterloo.ca!watmath!undergrad.math.waterloo.edu!rtczegle
From: rtczegle@undergrad.math.waterloo.edu (Richard Czegledi)
Newsgroups: alt.msdos.programmer
Subject: Re: This is strange, div doesn't work....
Message-ID: <BzKKo7.n43@undergrad.math.waterloo.edu>
Date: 20 Dec 92 18:00:06 GMT
References: <1992Dec18.094825.14710@miavx1.acs.muohio.edu> <BzIxL9.28B@undergrad.math.waterloo.edu> <BzJnMz.Gq4@uxa.ecn.bgu.edu>
Organization: University of Waterloo
Lines: 48