Re: DIV overflow
- From: "Wolfgang Kern" <nowhere@xxxxxxxx>
- Date: Sat, 31 Mar 2007 17:59:37 +0200
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
.
- Follow-Ups:
- Re: DIV overflow
- From: Wolfgang Kern
- Re: DIV overflow
- References:
- DIV overflow
- From: Brian
- DIV overflow
- Prev by Date: DIV overflow
- Next by Date: Re: cFASM (calling FASM as a C function)
- Previous by thread: DIV overflow
- Next by thread: Re: DIV overflow
- Index(es):
Relevant Pages
|