Homework 1

22C:116, Fall 1998

Due Monday Aug 31, 1998, in class

Douglas W. Jones

Always, on every assignment, please write your name legibly as it appears on your University ID and on the class list!

This assignment is dominated by review questions that are not as much specific to the material in the book as they are to material you should have learned in prior courses or work experience.

  1. What is your E-mail address? (If you have more than one, give the address you'd prefer used for class purposes.)

  2. Background: Most programming languages provide some mechanism that can be used for exception handling. In Ada and Java, the mechanism is very carefully thought out, allowing the programmer to name an exception, create a handler for it, and then, when the exception is raised within the (dynamic) scope of the handler, the code that raised the exception is abandoned and the handler is executed.

    You should have learned about some such mechanism in your programming language concepts course, a prerequisite for 22C:116!

    In Pascal, there is no built-in notion of exception handling, but the language allows non-local goto statements, and (to the surprise of many) these are sufficient. Any procedure or function that may raise an exception must take the handler as a procedure parameter. To raise the exception, simply call the procedure that was passed as a handler. This procedure never returns! Instead, it does a non-local goto, abandoning the execution context in which it was called and jumping to the end of the block in which it was declared. This is illustrated below:

         procedure MightRaiseException( Procedure Exception );
            begin
               ...
               if trouble then Exception;
               ...
            end;
    
         procedure HandlesException;
            label 999;
    
            procedure Handler;
               begin
                  ... code to handle exception ...
                  goto 999
               end;
    
            begin
               ...
               MightRaiseException( Handler );
               ...
               999:
            end
    
    The Problem: Write code in C or C++, using the C standard library routines setjmp() and longjmp() to demonstrate the construction of an exception handler in C or C++ that uses logic parallel to the Pascal code illustrated above. Information on setjmp() and longjmp() is available in the standard reference books for C and it is available on any Unix system using the man command.

  3. The Problem: Given what you have learned about setjmp() and longjmp(), suggest how they might be implemented. Do not write code! Write a short paragraph suggesting an implementation!

  4. Write a short paragraph that distinguishes between the notion of processor state, as the term is conventionally used in discussions of machine architecture, and process state, as the term is conventionally used in discussions of operating systems.

  5. What hardware features must a processor include if it is to support interrupts?