Lab 5 Handling Exceptions
1 Objectives
Following completion of this lab you should be able to:
- Use the MIPS instructions
mfc0
,mtc0
, anderet
. - Discuss how MIPS handles exceptions.
- Analyze and modify exception handlers.
2 Exception Handlers
-
Pull your repository and verify that the
lab05
folder contains files namedexception.asm
andexception.s
. -
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:
- select
Exception Handler ...
- set
Include this exception handler file in all assemble operations
- then browse to
exception.s
- select
- unset
-
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"?
-
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:
- What value is stored in the EPC?
- What value is stored in the Cause register?
- What is the value of "exception level" bit?
- After the exception is processed:
- What instruction is used to return to our program?
- Where in the program do we return?
- After we return to the program, are interrupts enabled?
- After we return to the program, what is the value of "exception level"?
-
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:
-
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
andcatch
will be accessible in user and kernel code..data .globl try .globl catch try: .word 0 catch: .word 0
-
Modify the
.ktext
section so that if the memory variabletry
is set to 1 and the exception is an overflow then the memory variablecatch
is set to 1 and no error message is printed. Otherwise the exception handler should behave as before.
-
-
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 modifiedexception.asm
would be a good starting point. You should be able to use thetry
andcatch
globals defined in your exception handler. Verify that your handler works as follows:- Ignores overflow when
try
is set to 1 - Handles overflow like normal when
try
is 0 - Other exceptions (for example, unaligned address exception) are not affected by
try
at all
- Ignores overflow when
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.