Lab 5 Handling Exceptions

1 Objectives

Following completion of this lab you should be able to:

2 Exception Handlers

  1. Pull your repository and verify that the lab05 folder contains files named exception.asm and exception.s.

  2. Start MARS and update the settings as follow:

    • unset Assemble all files in directory
    • unset Initialize Program counter to global 'main' if defined
    • set an exception handler:
      1. select Exception Handler ...
      2. set Include this exception handler file in all assemble operations
      3. then browse to exception.s
  3. Examine lab05/exception.s. Section A.7 of the book will be helpful in understanding what the exception handler is doing.

    • What conditions need to be true for an interrupt to occur (your answer should include the bits from the cause and status registers)?
    • What is the meaning of "exception level"?
  4. Start MARS and trace the execution of exception.asm.

    • When MARS starts are interrupts enabled?
    • Is it in user mode?
    • What instruction causes the exception and why?
    • When the exception is raised:
      1. What value is stored in the EPC?
      2. What value is stored in the Cause register?
      3. What is the value of "exception level" bit?
    • After the exception is processed:
      1. What instruction is used to return to our program?
      2. Where in the program do we return?
      3. After we return to the program, are interrupts enabled?
      4. After we return to the program, what is the value of "exception level"?
  5. Modify exception.s to enable a try-catch type mechanism for arithmetic overflow similar to the following:

    try {
        b = a + 2;
    }
    catch (overflowException e) {
        if (try == 1) {
            catch = 1;
        }
        else {
            execute the default overflow exception handler
        }
    }

    Remember that uncaught exceptions automatically go to the system exception handler. The basic steps required to modify the exception handler are:

    1. Add storage for the try-catch variables. Do this by adding the following code to the top of the "Standard startup code." The startup code is near the bottom of the exception handler. The variables try and catch will be accessible in user and kernel code.

              .data
              .globl try
              .globl catch
      try:    .word 0
      catch:  .word 0
    2. Modify the .ktext section so that if the memory variable try is set to 1 and the exception is an overflow then the memory variable catch is set to 1 and no error message is printed. Otherwise the exception handler should behave as before.

  6. Write a program to test that your new try-catch functionality does what is expected when try is enabled and when it is disabled. A modified exception.asm would be a good starting point. You should be able to use the try and catch globals defined in your exception handler. Verify that your handler works as follows:

    1. Ignores overflow when try is set to 1
    2. Handles overflow like normal when try is 0
    3. Other exceptions (for example, unaligned address exception) are not affected by try at all

3 Turning It In

Submit the answers to the questions contained in the lab guide using the question sheet in hardcopy.

Submit your changed files to your repository.

Only one lab should be submitted for each team. Make sure all team member's names are included on your submission.