Re: DIV overflow




Hello Brian,

What are good solutions to preventing DIV overflow?

Best solution would be to avoid DIV at all,
but unfortunately not always very practical.

An easy way is to always have dx just a sign extension of ax,
so for your example better use 32-bit DIV.

MOVZX ebx, word divisor ;if you want a 16-bit divisor
MOV eax, dword dividend
CDQ ;edx=Sign eax 0 or -1 (becomes 0 yet)
DIV ebx ;cannot overflow then


If I have:

INCLUDE Irvine32.inc
.data
dividend DWORD 20001000h
divisor WORD 1000h

.code
main PROC
mov dx, WORD PTR dividend + 2
mov ax, WORD PTR dividend
mov bx, divisor
div bx ;<== overflow here, ax not big enough
exit
main ENDP
END main

... here my quotient register, AX, is not big enough to hold
20001h. Is my best solution to use a 64-bit dividend, EDX:EAX and
make the divisor a DWORD in ebx, instead of WORD in bx... like so:

dividend DWORD 20001000h
divisor DWORD 00001000h

.code
main PROC
mov edx, 0
mov eax, DWORD PTR dividend
mov ebx, divisor
div ebx
exit
main ENDP
END main

Is it best just to pick a large size register combination for all
division to avoid overflow?

Yes, as above.

__
wolfgang



.



Relevant Pages

  • DIV overflow
    ... divisor WORD 1000h ... mov dx, WORD PTR dividend + 2 ... make the divisor a DWORD in ebx, ...
    (alt.lang.asm)
  • Re: ReactOS takes another big hit
    ... mov eax,DVNDHI // hi word of a ... First look to see if the divisor is less than 4194304K. ... // by the divisor and check the result against the orignal dividend ... // subtract the original divisor from the result. ...
    (alt.lang.asm)
  • Re: Really big math problem
    ... we mentally shifted the divisor (the number we ... digit of the dividend. ... We'd multiply the shifted divisor by that number, ...
    (microsoft.public.vc.mfc)
  • Re: Troubleshooting error in VB 6.0
    ... Chances are, you're reading data from a database into a recordset, and when going through the recordset, you're attempting to assign the field's value to a string variable or perhaps a textbox. ... Private Sub ShowRemainder(ByVal Divisor As Long, ByVal Dividend As Long) ...
    (microsoft.public.vb.general.discussion)
  • Re: division of two 4-bit vectors
    ... help, i am suposed to program a simple calculator in vhdl, and program ... repeatedly subtract the divisor from the dividend, ... DOULOS - Developing Design Know-how ...
    (comp.lang.vhdl)