From: Brian on
Hello All (und Hallo Herbert der fremdspracher),

What are good solutions to preventing DIV overflow?

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?

--
thanks,
Brian
To the best of my knowledge, I have:
1) asked a question specifically related to this newsgroup
2) not used my email to request answers be sent there
3) not top-posted
4) not used bad grammar that would make me appear more stupider
From: Brian on
On Sat, 31 Mar 2007 10:41:51 -0400, Brian
<stringchopperREMOVEALLCAPS(a)REMOVEALLCAPSgmail.com> wrote:

>Hello All (und Hallo Herbert der fremdspracher),
>
>What are good solutions to preventing DIV overflow?
>
>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
>
perhaps a better way to ask my question... If I were writing a
compiler or a translator from a high level language to assembly,
what's the best way to implement division of a 4 byte integer by a 2
byte integer, not knowing the values beforehand... should I always
upsize the dividend, regardless?

--
thanks,
Brian
To the best of my knowledge, I have:
1) asked a question specifically related to this newsgroup
2) not used my email to request answers be sent there
3) not top-posted
4) not used bad grammar that would make me appear more stupider
From: Wolfgang Kern on

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



From: Wolfgang Kern on

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

Better replace CDQ with XOR edx,edx for UNsigned DIV.

__
wolfgang

:) we always post it twice ?


From: Brian on
On Sat, 31 Mar 2007 18:22:12 +0200, "Wolfgang Kern" <nowhere(a)never.at>
wrote:

>
> 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
>
>Better replace CDQ with XOR edx,edx for UNsigned DIV.
>
>__
>wolfgang
>
>:) we always post it twice ?
>
we are thorough :)

so is the sign extension necessary with unsigned division? Also, in
the above, you say "sign extension" but have "MOVZX"... you meant
"MOVSX", right?


--
thanks,
Brian
To the best of my knowledge, I have:
1) asked a question specifically related to this newsgroup
2) not used my email to request answers be sent there
3) not top-posted
4) not used bad grammar that would make me appear more stupider
 |  Next  |  Last
Pages: 1 2 3
Prev: ascii to tword - help
Next: Is hex an ascii thing?