- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The following snippet illustrates code that should raise 2 exceptions.
Obviously only one exception throwing line will throw an exception . And indeed it does in Visual C++. Division by 0 is caught and derferencing a NULL pointer is caught. int test_exception(void) { int x = 1; int y = 0; int z; char c; char *ptr = NULL; try{ z = x/y; /* This line is commented or the next to have one source of exception */ c = *ptr; } catch(...){ printf("Caught an exception \n"); } return 0; } The same code does not behave the same way in the Altera IDE. One could do the following, which works : try{ if(y == 0) throw MyZeroDivException; else z = x/y; if(ptr == NULL) throw MyDereferenceException; else c = *ptr; } catch(MyZeroDivException) { // Do something } catch(MyDereferenceException) { // Do something } The questions are: How does NIOS handle division by 0 ? How does dereference of an incorrect address behave ? What are the cases when software can generate an exception ? If there are similar questions about exception handling please refer me to those. Thank you AntonLink Copied
1 Reply
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nios II can't catch NULL pointer dereferences because that requires a memory management or memory protection unit. On the x86 processor the CPU catches the NULL pointer dereference and the OS tells the runtime library to throw the exception.
If you use hardware divide then the Nios II CPU won't trap divide by 0 so you have to write your code as specified. If you use software divide then an easier solution is possible since the compiler calls functions like __divsi3 to do the division. You can create your own __divsi3 file, by copying the standard version of nios2-gnutools/src/gcc/gcc/config/nios2/lib2-divmod.c to your system library project and modifying it to throw the exception if appropriate.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page