CS520
Fall 2010
Programming Assignment 4
Due Sunday October 24


The goal of this assignment is to support exception handling for C programs on the Intel IA-32 architecture.

The public interface to the exception handling mechanism is the following three functions:

catchException enables an exception handler for the current function. This function returns 0 on its initial return but will "re-awake" and return the number of the exception if an exception is thrown while the handler is active. Any exception handler active prior to the call to catchException will be temporarily disabled while the new handler is active and automatically re-enabled when the new handler is cancelled.

cancelCatchException cancels the currently active exception handler. If a handler was made inactive when the handler being cancelled was enabled, then the handler is now made active again.

In essence, handlers are stacked. A newly enabled handled is pushed on a stack of handlers and the topmost handler is always utilized when an exception is thrown. When a handler is cancelled it is popped from the stack,

If there is no active handler when cancelCatchException is called, then no action is taken. In essence the request to cancel is just ignored.

If an exception handler is still active after the function in which it was enabled returns, then the behavior is undefined. It is the user's responsibility to call cancelCatchException before the function returns. If this responsibility is not met, then strange things can happen.

throwException throws the given exception, which will be caught by the most recently enabled handler, if there is one. If there is no enabled handler, then the program is terminated with an appropriate message to stderr that gives the exception number. The standard C library function exit is used to terminate the function and the exception number is passed to exit as its argument.

It is a fatal error to pass 0 to throwException. An appropriate error message is printed to stderr and the program is terminated by passing -1 to exit.

A thrown exception causes the cancellation of the handler that catches it.

The values of the callee-saved registers (ebx, esi, edi) should be restored at the time of the re-awakening of catchException to what they were at the call to catchException.

You will need to implement pieces of this assignment in Intel IA-32 assembly language. You should aim to minimize the amount of assembly language you need to write.

All C code for this assignment should be placed in the file eh.c and all assembly language code should be placed in the file asm.s. See ~cs520/public/prog4 for stubs for these files, as well as a main function for an initial test (in the file testEH.c) and a Makefile for building the initial test.

To avoid namespace pollution, preface the names of all helper functions that need to be visible to the linker by "EH_". Try to minimize the number of such functions and be sure to use the "static" keyword to hide all other functions and global variables.

Your program will be graded primarily by testing it for correct functionality. In addition, however, you may lose points if your program is not properly structured and documented. Decompose sub-problems appropriately into functions and do incremental testing. Leave your debugging output in your code, but disabled, when you do your final assignment submission.

This assignment is hard to decompose into pieces in order to award partial credit. Therefore, 50% of the points will be awarded based on a reading of your source code to identify whether an "honest effort" was made to try to complete this assignment. The other 50% of the points will be awarded on how well your exception handler works.

There is no lab on Friday October 15.

The lab on Friday October 22 will be a virtual lab, meaning we will not meet for the lab. Instead you should submit your code for evaluation prior to 12noon that day. (No late submissions will be accepted.) You will receive 50% of the points for submitting something and the remaining points will be awarded based upon my evalution of your code, including running the one public test. To turn in this lab, type:
~cs520/bin/submit lab7 eh.c asm.s

Your programs will be graded using agate.cs.unh.edu so be sure to test in that environment.

You should only submit eh.c and asm.s. Be sure you disable any debugging output before you submit your assignment.

Your programs should be submitted for grading from agate.cs.unh.edu. To turn in this assignment, type:
~cs520/bin/submit prog4 eh.c asm.s

Please submit only these two files. Do not turn in any other files!

Submissions can be checked by typing:
~cs520/bin/scheck prog4

To receive full credit for the assignment, you must turn in your files prior to 8am on Monday October 25. Programming assignments may be handed in late at a penalty of 2 points for one day late, 5 points for two days late, 10 points for three days late, 20 points for four days late, and 40 points for five days late. No program may be turned in more than 5 days late.

Remember: as always you are expected to do your own work on this assignment.


Last modified on October 11, 2010.

Comments and questions should be directed to hatcher@unh.edu