nils pipenbrinck
asked on
Integer Divide Exception
Hi.
I'm writing some assembly code which should run under windows. for speed I converted one of my float routines to integer (I want to use mmx).
The problem is, that my integer code sometimes (every 5 minutes or so) generates a divide by zero or divide overflow exception.
Under dos I usually solved the problem with a simple interrrupt-handler which just returns to the caller.
Now I want to do exactly this under windows. How do I catch this exception and continue my code?
I'm using c for the OS stuff btw..
Nils
I'm writing some assembly code which should run under windows. for speed I converted one of my float routines to integer (I want to use mmx).
The problem is, that my integer code sometimes (every 5 minutes or so) generates a divide by zero or divide overflow exception.
Under dos I usually solved the problem with a simple interrrupt-handler which just returns to the caller.
Now I want to do exactly this under windows. How do I catch this exception and continue my code?
I'm using c for the OS stuff btw..
Nils
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi ikr..
your code works on a small testfile I wrote.. the problem is, that if I have initialized directx and switched to fullscreen graphics the __try stuff is the fastest way to reboot my computer :)
I guess it's not your fault, but I leave this question open some more hours...
Since I don't want to use my clib (the __try implementation differs from compiler to compiler) I tried it using an unhandled exception filter..
The problem is, that this code causes a stack overflow.. (no matter how many stack i have..) Any ideas?
#include <windows.h>
LONG gpffilter(EXCEPTION_POINTE RS *pExp)
{
if (EXCEPTION_INT_DIVIDE_BY_Z ERO == pExp->ExceptionRecord->Exc eptionCode )
return(EXCEPTION_CONTINUE_ EXECUTION) ;
return(EXCEPTION_EXECUTE_H ANDLER);
}
void main (void)
{
SetUnhandledExceptionFilte r(gpffilte r);
; make a div/zero error...
_asm {
pushad
mov eax, 0
mov edx, 1
mov ecx, 0
idiv ecx
popad
}
MessageBox (0,"cool, it workes","debug", 0);
}
your code works on a small testfile I wrote.. the problem is, that if I have initialized directx and switched to fullscreen graphics the __try stuff is the fastest way to reboot my computer :)
I guess it's not your fault, but I leave this question open some more hours...
Since I don't want to use my clib (the __try implementation differs from compiler to compiler) I tried it using an unhandled exception filter..
The problem is, that this code causes a stack overflow.. (no matter how many stack i have..) Any ideas?
#include <windows.h>
LONG gpffilter(EXCEPTION_POINTE
{
if (EXCEPTION_INT_DIVIDE_BY_Z
return(EXCEPTION_CONTINUE_
return(EXCEPTION_EXECUTE_H
}
void main (void)
{
SetUnhandledExceptionFilte
; make a div/zero error...
_asm {
pushad
mov eax, 0
mov edx, 1
mov ecx, 0
idiv ecx
popad
}
MessageBox (0,"cool, it workes","debug", 0);
}
Hmm, I'm not too good at assembler ;-)
Have you tried sth. simple like that
int a,b,c;
a = 1;
b = 0;
c = a / b;
?
Have you tried sth. simple like that
int a,b,c;
a = 1;
b = 0;
c = a / b;
?
ASKER
no.. I haven't tried it..
my compiler (watcom) won't compile this code because it's obviously buggy (it does nothing but a divide by zero).
that code I used does nothing but a 64bit / 32bit divide where the 64 bit value is 0x100000000 and the 32 bit value is 0. (just for your information).
I think your answer is correct, even if it doesn't work for me.
I now check the values before I divide. This works around my problem (and is cleaner than the div/zero hack).
Nils
my compiler (watcom) won't compile this code because it's obviously buggy (it does nothing but a divide by zero).
that code I used does nothing but a 64bit / 32bit divide where the 64 bit value is 0x100000000 and the 32 bit value is 0. (just for your information).
I think your answer is correct, even if it doesn't work for me.
I now check the values before I divide. This works around my problem (and is cleaner than the div/zero hack).
Nils
Thanx!
Well, Watcom is a bit picky sometimes, isn't it <g>?
BTW: Your name sounds qiute German - am I right?
Well, Watcom is a bit picky sometimes, isn't it <g>?
BTW: Your name sounds qiute German - am I right?
ASKER
yep.. it doesn't only sound german.. it actually _is_ german. hallo :)
watcom might be picky, but it lets me do what I want (even strangest things like writing programs without linking any clib objects and stuff like that).
I simply love the compiler.. I've never seen anything flexible as watcom c. (to bad there will most likely never be a release 12 with p6 codegen).
Nils
watcom might be picky, but it lets me do what I want (even strangest things like writing programs without linking any clib objects and stuff like that).
I simply love the compiler.. I've never seen anything flexible as watcom c. (to bad there will most likely never be a release 12 with p6 codegen).
Nils
Kann ich nur zustimmen - der Optimizer is' mit vom Feinsten. Wir haben hier auch noch einen in der Version 10.0 rumstehen, aber leider is' ja mittlerweile VC++ ein Muß :-(
Gruß aus dem Schwabenland ;-)
Jürgen
Gruß aus dem Schwabenland ;-)
Jürgen
ASKER
Gruß aus Hamburg :)
LONG WINAPI __XceptFilter ( EXCEPTION_POINTERS* pExp)
{
LONG lnRC = EXCEPTION_EXECUTE_HANDLER;
#ifdef _DEBUG
char acXcept[ 1024];
sprintf ( acXcept,
"__XceptFilter(): got exception, code == 0x%x @ 0x%x, flags == 0x%x",
pExp->ExceptionRecord->Exc
( DWORD) pExp->ExceptionRecord->Exc
pExp->ExceptionRecord->Exc
);
#endif
if ( EXCEPTION_INT_DIVIDE_BY_ZE
{
return( EXCEPTION_CONTINUE_EXECUTI
}
return( lnRC);
}
// ...
SetUnhandledExceptionFilte