1.The Basics of Exception Handling Interrupts Initiated outside the instruction stream Arrive asynchronously (at no specific time), Examples: o I/O device status change o I/O device error condition Traps occur due to something in instruction stream. Examples: o Unaligned address error o Arithmetic overflow o System call
2.An exception takes away control from the user and transfers it to the supervisor (i.e. the operating system) Think of the various reasons an exception can occur. An exception triggers an unscheduled procedure call.
3.Coprocessor C0 has a cause register (Register 13) that contains a 4-bit code to identify the cause of exception Cause register pending exception code interrupt Bits 15-10 Bits 5-2 [Exception Code = 0 means I/O interrupt = 12 means arithmetic overflow etc] MIPS instructions that cause overflow (or some other violation) lead to an exception, which sets the exception code. It then switches to the kernel mode (designated by a bit in the status register of C0, register 12) and transfers control to a predefined address to invoke a routine (exception handler) for handling the exception. Interrupt Status register Enable Interrupt Mask 15-8 1 0 Kernel/User
4.(EPC = Exception Program Counter, Reg 14 of C0) Memory L: add $t0, $t1, $t2 overflow! Return address (L+4) Exception handler routine is saved in EPC Next instruction Overflow ra ← EPC; jr ra Invalid instruction ra ← EPC; jr ra System Call ra ← EPC; jr ra The Exception Handler determines the cause of the exception by looking at the exception code bits. Then it jumps to the appropriate exception handling routine. Finally, it returns to the main program.
5.Visualizing Exception Handling main interrupt / exception Analyze the cause of exception arithmetic interrupt from keyboard? overflow? Exceptions cause mostly unscheduled procedure calls.
6.Example: Read one input from a Keyboard Consider reading a value from the keyboard. Assume that the interrupt enable bit is set to 1. The first line, ".text 0x80000080" places the code explicitly at the memory location where the interrupt service routine is called. .text 0x80000080 mfc0 $k0, $13 # $k0 = $Cause; mfc0 $k1, $14 # $k1 = $EPC; andi $k0, $k0, 0x003c # $k0 &= 0x003c (hex); # Filter the Exception Code; bne $k0, $zero, NotIO # if ($k0 ≠ 0) go to NotIO # Exception Code 0 => I/O instr. sw $ra, save0($0) # save0 = $ra; jal ReadByte # ReadByte(); (Get the byte). lw $ra, save0($0) # $ra = save0; jr $k1 # return; NotIO: Other routines here Note that procedure ReadByte must save all registers that it plans to use, and restore them later.
7.Understanding Logic Design Appendix C of your Textbook on the CD When you write add $t0, $t1, $t2, you imagine something like this: $t1 Adder $t0 $t2 What kind of hardware can ADD two binary integers? We need to learn about GATES and BOOLEAN ALGEBRA that are foundations of logic design.
8.AND gate X Y X.Y 0 0 0 X X.Y 0 1 0 Y 1 0 0 1 1 1 OR gate X Y X+Y 0 0 0 X 0 1 1 X+Y 1 0 1 Y 1 1 1 NOT gate X X 0 1 X X 1 0 Typically, logical 1 = +3.5 volt, and logical 0 = 0 volt. Other representations are possible.
9.Analysis of logical circuits X X.Y F Y X.Y What is the value of F when X=0 and Y=1? Draw a truth table. X Y F 0 0 0 0 1 1 1 0 1 1 1 0 This is the exclusive or (XOR) function. In algebraic form F= X.Y + X.Y
10.More practice 1. Let A.B + A.C = 0. What are the values of A, B, C? 2. Let (A + B + C).(A + B + C) = 0. What are the possible values of A, B, C? • Draw truth tables. • Draw the logic circuits for the above two functions. A B C