Exceptions and Interrupts Breakpoints arithmetic overflow traps and
Exceptions and Interrupts ◆ Breakpoints, arithmetic overflow, traps, and interrupts are all classified as exceptions. ◆ An exception is an event that requires a special handling by the CPU and initiates a change in the normal flow of program execution. ◆ An interrupt is a signal coming from I/O devices outside the CPU. Whenever an I/O device needs the service of CPU, it generates an interrupt signal to be send to the CPU.
Exceptions and Interrupts (cont. ) ◆ Compared with Programming I/) (with busy waiting or pooling), interrupt provides a more efficient method for utilizing the CPU time and providing a better throughput of I/O operations. Throughput is defined as the rate of completion of I/O operations, for example 10 K operations per one second. ◆ For each device, there is a special program called interrupt service routine (driver) for processing the interrupts from the I/O devices and providing the requested services.
Exceptions and Interrupts (cont. ) ◆Whenever the CPU receives an interrupt signal, a special program called interrupt response routine or trap handler is run by the CPU to poll the I/O devices to determine which one generated the interrupt and starts its driver. ◆ Breakpoints, arithmetic overflow, and traps, are exceptions generated by the CPU and also require service routines.
Coprocessor Zero ◆The method implemented by the MIPS designers to interrupt the currently running program is to include some additional hardware referred to as coprocessor 0 that contains a number of specialized registers that can be accessed at the assembly language level for exception handling. ◆ The top window of the PCSpim simulator displays these registers: EPC Coprocessor register 14 (Exception Program counter) Cause Coprocessor register 13 Bad. Vaddress Coprocessor register 8 Status Coprocessor register 12
Two New Instructions Used to Access Coprocessor Registers mfc 0 $k 0, $13 # CPU register $k 0 is loaded with # contents of the Cause register mtc 0 $0, $12 # CPU register $0 is stored in the # Status register (Cleared to zero) # Destination on the Right !!!!!!!
Enabling Interrupts at the Status Register Level li mtc 0 $s 1, 0 x 0000 ffff $s 1, $12 # Mask to enable all interrupts # Store enable bits in Status register
Enabling Keyboard Interrupt li li sw $s 1, 2 $a 3, 0 xffff 0000 $s 1, 0($a 3) # Base address of I/O # Enable Keyboard Interrupt
MIPS CPU Memory 0 xffff 0000 Interrupt Enable Receiver Control Ready 0 xffff 00004 Receiver Data 0 xffff 0008 Interrupt Enable Transmitter Control 0 xffff 000 c Ready Transmitter Data
Modifications to Trap Handler bgtz $v 0 _echo # <<<<< Modified Version **** ######## New Code In The Trap Handler ######## _echo: li $a 0, 0 xffff 0000 # Base address of I/O devices lw $v 0, 4($a 0) # Get Character from the Keyboard sw $v 0, 12($a 0) # Send Character to the Display b ret
Random Number Generator # Description: Generates a random number each time key is pressed. # Numbers range from 0 to 99. Press Enter key to quit. # Must Run with Memory-Mapped I/O and Random Trap Handler. . data # Data declaration section nl: . asciiz "n" msg: . asciiz "n. How Lucky Can You Get? " bye: . asciiz "n** Come Back Again **". text main: li $a 3, 0 xffff 0000 # Base address of I/O li $s 1, 2 sw $s 1, 0($a 3) # Enable Keyboard Interrupt li $s 1, 0 x 0000 ffff # Mask to enable all interrupts mtc 0 $s 1, $12 # Store enable bits in Status register li $v 0, 4 # Print message la $a 0, msg syscall li $t 0, 211 # Seed values li $t 1, 3021377
clear: li $v 1, 0 mult mflo addiu beqz $t 0, $t 1 $t 0, 5923 $v 1, ranloop addiu beqz li la syscall li divu mfhi li syscall b $v 1, -10 $v 1, quit $v 0, 4 $a 0, nl li la syscall li syscall $v 0, 4 $a 0, bye # Clear the flag ranloop: $v 1, 100 $t 0, $v 1 $a 0 $v 0, 1 # Keystroke will change $v 1 # to ASCII value for the key # Quit if Enter Key # Print newline # Controls Range (0 – 99) # Get Remainder clear quit: $v 0, 10 # Print newline
Exercise 9. 7 Modify the random number generator so you can play “Craps” with the computer. This requires that two random numbers be computed and printed each time you press the six (6) key.
Exercise 9. 7 (MIPS assembly language) clear: li la syscall li $v 0, 4 $a 0, msg # Print message # " Roll the Dice: " $v 1, 0 # Clear the flag mult mflo addiu beqz $t 0, $t 1 $t 0, 5923 $v 1, ranloop addiu beqz addiu blez $v 1, -10 $v 1, quit $v 1, -38 $v 1, ranloop: # keystroke will change $v 1 # to ASCII value of key # Quit if Enter Key # Ignore zero & most special keys
Exercise 9. 7 Continued divu $t 0, $v 1 mfhi $a 0 addi $a 0, 1 li $v 0, 1 syscall li $v 0, 4 la $a 0, spxx syscall mflo $t 0 divu $t 0, $v 1 mfhi $a 0 addi $a 0, 1 li $v 0, 1 syscall b clear # Get Remainder # Will get numbers between 1 and N # N depends on which key is pressed # For example "6" key simulates Dice # Print " & " # Generate Second Random Number # Get last Quotient # Get Remainder # Will get numbers between 1 and N # N depends on which key is pressed # For example "6" key simulates Dice
Exercise 9. 12 Adduovf ( x, y, s) Write a function Adduovf( x, y, s) that will find the 32 -bit sum “s” of two unsigned arguments “x” and “y”. An exception should be generated if the unsigned representation of the sum results in overflow. Perform all communication on the stack. Hint: A carry out at the most Significant Digit (MSD) is Overflow for unsigned numbers.
Exercise 9. 12 (Assembly Language) Adduovf ( x, y, s) Label Op-Code Dest. S 1, S 2 Adduovf: lw $t 3, 0($sp) lw $t 4, 4($sp) addu $t 5, $t 4, $t 3 sw $t 5, 8($sp) xor $t 6, $t 3, $t 4 bltz $t 6, diff bgez $t 3, ok overflow: break ok: jr $ra Comments diff: # Checking MSD of sum bgez jr $t 5, overflow $ra # Get x # Get y # Get sum # Put sum on stack # Checking MSD of x and y # Will cause an exception
Exercise 9. 13 Write a function Add 64 that will perform a 64 -bit addition: x = y + z, where the values for x, y, and z are stored as two 32 -bit words each: Add 64(x 1: $t 1, x 0: $t 0, y 1: $t 3, y 0: $t 2, z 1: $t 5, z 0: $t 4) All six parameters are passed on the stack. If the 64 -bit sum results in overflow an exception should be generated. After writing your code, calculate the performance indexes: Space: _________(Words of code) Time: _________(Maximum number of clock cycles to execute) $t 3 $t 2 $t 5 $t 4 $t 1 $t 0 x 1 Sum x 0
Exercise 9. 13 (Assembly Language) Label Op-Code Dest. S 1, S 2 Add 64: lw $t 3, 8($sp) lw $t 2, 12($sp) lw $t 5, 16($sp) lw $t 4, 20($sp) addu $t 0, $t 2, $t 4 add $t 1, $t 3, $t 5 xor $t 6, $t 2, $t 4 bltz $t 6, diff same: bgez $t 2, fini carry addi $t 1, 1 fini: sw $t 1, 0($sp) sw $t 0, 4($sp) jr $ra diff: bgez $t 0, carry b fini Comments # Get y upper # Get y lower # Get z upper # Get z lower # add lower half # add upper half # Checking for a carry to upper half # Generate carry to upper # Return x lower
- Slides: 18