- Make a new directory for the assignment.
- Download the following files:
- Execute the instructions.s
program under spim:
spim -file instructions.s
The program
should generate six
Bad Data/Stack Address Exceptions (Exception 7) because
the program assumes that the data segment starts at 0 and
xspim assumes that it starts at 0x10000000.
- Run the instructions.s
program again using xspim and this time single step through
at least two exceptions. Make sure you see how the flow of
control is changed by looking at the source code and note where
the exceptions occur.
xspim -file instructions.s
- Modify instructions.s to a MIPS program
instr_good.s by changing two instructions so that it
runs correctly. (The program should form the sum
A[0] + A[1] + ... + A[8] = 0 + 1 + 2 + ... + 8 = 36
and print this number. Make no further use of the file
instr_good.s below.)
- At the bottom of the test program instructions.s,
add the default handler code trap.handler:
cat instructions.s trap.handler > instr_trap.s
Run xspim using the following command line.
(Don't forget the -notrap option, or Spim will load both
the default handler and the one you concatenated at the end.)
xspim -notrap -file instr_trap.s
You can also try out spim:
spim -notrap -file instr_trap.s
The program should run the same way it did before.
- Now edit the trap handler trap.handler
so that it handles a bad data/stack address (Exception 7) as follows.
(If you wish you can just directly edit the file instr_trap.s,
say, to produce a file instr_trap_fixed.s.)
- Print a special new message indicating that you are adjusting
for exception 7.
- Add 0x10010000 to $t1.
- Restart the current instruction. (Note: Restart the current
instruction, not the next instruction as the current
handler does.)
When you run the program you should only get two Exception 7 occurrences
at the instructions:
lw $t4, 84($t1) #Constant 4 stored in $t4
....
sw $t3, 0($t1) #sum is stored
Your program should handle these exceptions so that the correct
calculation is performed and a 36 is printed at the end.
- Debugging hints:
- You will probably need to do a lot of single-stepping to debug.
- Be sure to use addu when adding addresses so that
you don't generate an overflow exception.
- Be very careful not to use any registers in your trap handler
that you haven't explicitly saved in kernel static memory.
It is best to just use the register $a0 for all your work.
- Note that when the exception error message is printed by
the handler, register $k0 holds the exception number 7,
but it is shifted 2 positions to the left, so that $k0
actually holds 28 = 0x1c = 11100 (base 2).
- You should NOT modify the original code instructions.s
at all. The original bad data address references should remain.
All modifications occur in the edited handler.