Re: DIV overflow



On Sat, 31 Mar 2007 18:22:12 +0200, "Wolfgang Kern" <nowhere@xxxxxxxx>
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
.