[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Integer Divide Exception

Posted on 2000-02-01
9
Medium Priority
?
614 Views
Last Modified: 2013-12-03
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

0
Comment
Question by:nils pipenbrinck
  • 5
  • 4
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 450 total points
ID: 2478025
The best thing to handle this kind of situation would be using SEH aka 'Structured Exception Handling' - it mainly works like this:

__try
{
 // malicious code
}
__except ( EXCEPTION_INT_DIVIDE_BY_ZERO = GetExceptionCode()) // make sure to only handle exceptions we want
{
 // exception handling code here
}

Feel free to ask if you need more information!
0
 
LVL 86

Expert Comment

by:jkr
ID: 2478058
Or you could alternatively install a top-level SEH exception handler by calling 'SetUnhandledExceptionFilter()' to replace the global 'UnhandledExceptionFilter()' (which will usually signal the exception to the user with one of these 'nice' message boxes and terminate the program) with your own, e.g.

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->ExceptionCode,
                ( DWORD)    pExp->ExceptionRecord->ExceptionAddress,
                            pExp->ExceptionRecord->ExceptionFlags
            );
#endif

    if  (   EXCEPTION_INT_DIVIDE_BY_ZERO    ==  pExp->ExceptionRecord->ExceptionCode)
        {
            return( EXCEPTION_CONTINUE_EXECUTION);
        }

    return( lnRC);
}

// ...

      SetUnhandledExceptionFilter      (      __XceptFilter);
0
 
LVL 4

Author Comment

by:nils pipenbrinck
ID: 2478170
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_POINTERS *pExp)
{
  if (EXCEPTION_INT_DIVIDE_BY_ZERO  ==  pExp->ExceptionRecord->ExceptionCode)
    return(EXCEPTION_CONTINUE_EXECUTION);

  return(EXCEPTION_EXECUTE_HANDLER);
}


void main (void)
{

  SetUnhandledExceptionFilter(gpffilter);

  ; 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);
}



0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 86

Expert Comment

by:jkr
ID: 2478294
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;

?
0
 
LVL 4

Author Comment

by:nils pipenbrinck
ID: 2478828
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

0
 
LVL 86

Expert Comment

by:jkr
ID: 2478840
Thanx!

Well, Watcom is a bit picky sometimes, isn't it <g>?

BTW: Your name sounds qiute German - am I right?
0
 
LVL 4

Author Comment

by:nils pipenbrinck
ID: 2479300
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


0
 
LVL 86

Expert Comment

by:jkr
ID: 2479461
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
0
 
LVL 4

Author Comment

by:nils pipenbrinck
ID: 2479692
Gruß aus Hamburg :)

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question