COSC 211 Lab 7 – Exceptions Solved

$ 24.99
Category:

Description

0.Introduction
In this lab, you will have the opportunity to implement an exception handler in MIPS assembly. Additionally, there are a series of questions from the text book on pipelining. There is a total of 36 marks available for this assignment.
In this assignment, we implement an exception handler that handles
1. exceptions generated by program errors
2. exceptions generated by the instruction break n
The task is divided into two parts so that you can complete them incrementally. Please read the following before you start to work on the questions. Your goal is to be able to interpret the cause and status registers and have your exception handler act accordingly. Please consult your class notes. It is imperative that you review Appendix A.7 and section 4.9 before starting this lab as it will provide details on interrupts and exception handling.
When we start SPIM, a default exception is loaded and included in the user program. Implemented are some exceptions handling routines and a start-up routine that calls the user program’s main procedure. We want to create our own exception handler and not use the default one provided. To tell SPIM that you want to use your own exception handler, you need to explicitly tell the simulator where your exception is to be found.

Figure 1 Figure 2
This is set by Simulator->Settings from the main menu bar (Figure 1). To load your exception handler, select the MIPS tab from the settings window and make sure Load Exception Handler is checked. From the file selection box next to the check box, navigate to and select your exception handler (Figure 2 & 3).

Figure 3
When you return to the main window, any code you load into QtSpim will now use your exception handler that you have created meaning that when your code encounters an exception, it will use your provided exception handler file. When you run your program now, you will need to change the start address. The program start address can be found under Settings->Set Run Parameters. The new address is 0x00400000.
1. Programming Assignment
1. (/20) Write an exception handler that replaces every instruction that causes an exception with a NOP instruction (this is an instruction that does ‘no operation’, but delays once cycle). A framework has been provided for you named lab7handler.s, setting up your initial .kdata and .ktext sections (these sections are part of the kernel segment which will handle your exception). You are required to setup the rest of the file to make it functional. Please consult your lecture notes.
While under normal conditions it is not permissible to write to the .text section of your code during runtime; when an exception occurs, control is transferred away from your program to the kernel which executes your exception handler. As a result, you can modify memory in your code space when control is transferred to the exception handler. For this first exercise, the exception handler will replace each offending instruction with a no-operation instruction. A nop instruction is encoded as the word: 0x00000000. It essentially allows one instruction cycle to pass without anything happening. To overwrite the instruction with a nop, you need to determine the address of the offending instruction which can be stored in a k register. Using a second k register, you can load the machine instruction value for nop and then store it at the address of the offending instruction. This can be done as:
li $k1, 0x0 #this is the nop instruction value sw $k1, ($k0) #now write is to the address in $k0
You will need to determine the address of the offending instruction. In this case, make sure you return to the address of the offending instruction and re-execute the instruction. Your code will need to handle an arithmetic exception and an unaligned address exception. You will need to examine the Status and Cause registers to determine the source of the exception (see section 4.9, A-34 & A-35). The lab7handler.s also contains a start-up procedure which calls the main procedure of the user program. Test your exception handler with this assembly program testexceptions.s.
The output should look like:
—exception loop—
Exception occurred at PC=0x00400038
Arithmetic overflow
Exception occurred at PC=0x0040003c
Exception occurred at PC=0x00400040
Unaligned address in store: 0x7fffffff
—exception loop—
—exception loop—
—exception loop—
—exception loop—
QtSpim will also generate errors that will be displayed in popup windows which can be ignored for this exercise. Your code will display a text message indicating the type of exception encountered. Note that the output “Exception occurred at PC=0x0040003c” is from SPIM itself.
Include a program comment header that has your name, student number and program name. Name your file handler1.s and submit through the submission link in Canvas.
2. (/10) In the program testexceptions.s, there is an instruction break 20. This is a special instruction that will generate an exception and is handled by QtSpim. It generates a breakpoint exception. Modify your exception handler so that it will handle the exception generated by the instruction break n in the following way: for every exception generated by break n, output the value n but leave the instruction unchanged.
The number n in the instruction break n is encoded as part of the instruction. You have to get the instruction from the memory and extract the number (There is an issue with the break n encoding format with SPIM, it won’t be difficult for you to figure out what it is). The output, when testing with testexceptions.s, should now look like
—exception loop—
Exception occurred at PC=0x00400054
Arithmetic overflow
Exception occurred at PC=0x00400058
20
Exception occurred at PC=0x0040005c
Unaligned address in store: 0x7fffffff
—exception loop—
Exception occurred at PC=0x00400058
20
—exception loop—
Exception occurred at PC=0x00400058
20
—exception loop—
Exception occurred at PC=0x00400058
20
—exception loop—
Exception occurred at PC=0x00400058
20
Unlike the exercise in part 1, when you encounter and handle a break exception, you will need to continue execution at the next instruction so you will have to increment the EPC accordingly (so that you can return to the instruction following the instruction which generated the exception). Include a program comment header that has your name, student number and program name. Name your file handler2.s and submit through the submission link in Canvas.

2. Written Assignment
1. (/6) Exercise 4.9 (from 5th edition)
a. 4.9.1 (4 marks)
b. 4.9.2 (2 marks)
Written Submission

Reviews

There are no reviews yet.

Be the first to review “COSC 211 Lab 7 – Exceptions Solved”

Your email address will not be published. Required fields are marked *