Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions and Interrupts ◆ Breakpoints, arithmetic overflow, traps, and interrupts are all classified as exceptions. ◆ An exception is an event that requires.

Similar presentations


Presentation on theme: "Exceptions and Interrupts ◆ Breakpoints, arithmetic overflow, traps, and interrupts are all classified as exceptions. ◆ An exception is an event that requires."— Presentation transcript:

1 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.

2 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 10K 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.

3 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.

4 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 BadVaddressCoprocessor register 8 Status Coprocessor register 12

5 Two New Instructions Used to Access Coprocessor Registers mfc0 $k0, $13 # CPU register $k0 is loaded with # contents of the Cause register mtc0 $0, $12 # CPU register $0 is stored in the # Status register (Cleared to zero) # Destination on the Right !!!!!!!

6 Enabling Interrupts at the Status Register Level li$s1, 0x0000ffff # Mask to enable all interrupts mtc0$s1, $12# Store enable bits in Status register

7 Enabling Keyboard Interrupt li$s1, 2 li$a3, 0xffff0000# Base address of I/O sw$s1, 0($a3)# Enable Keyboard Interrupt

8 MIPS CPU Memory Receiver Control Receiver Data Transmitter Control Transmitter Data 0xffff0000 0xffff00004 0xffff0008 0xffff000c Interrupt Enable Ready

9 Modifications to Trap Handler bgtz $v0 _echo# <<<<< Modified Version **** ################ New Code In The Trap Handler ############### _echo: li$a0, 0xffff0000 # Base address of I/O devices lw$v0, 4($a0) # Get Character from the Keyboard sw$v0, 12($a0) # Send Character to the Display bret

10 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 "\nHow Lucky Can You Get?" bye:.asciiz "\n** Come Back Again **".text main: li$a3, 0xffff0000 # Base address of I/O li$s1, 2 sw$s1, 0($a3)# Enable Keyboard Interrupt li$s1, 0x0000ffff # Mask to enable all interrupts mtc0$s1, $12# Store enable bits in Status register li$v0, 4# Print message la$a0, msg syscall li$t0, 211# Seed values li$t1, 3021377

11 clear: li$v1, 0# Clear the flag ranloop: mult$t0, $t1 mflo$t0 addiu$t0, $t0, 5923 beqz$v1, ranloop# Keystroke will change $v1 # to ASCII value for the key addiu$v1, $v1, -10 beqz$v1, quit# Quit if Enter Key li$v0, 4# Print newline la$a0, nl syscall li$v1, 100# Controls Range (0 – 99) divu$t0, $v1 mfhi$a0# Get Remainder li$v0, 1 syscall bclear quit: li$v0, 4# Print newline la$a0, bye syscall li$v0, 10 syscall

12 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.

13 Exercise 9.7 (MIPS assembly language) clear: li$v0, 4# Print message la$a0, msg# " Roll the Dice:" syscall li$v1, 0# Clear the flag ranloop: mult$t0, $t1 mflo$t0 addiu$t0, $t0, 5923 beqz$v1, ranloop# keystroke will change $v1 # to ASCII value of key addiu$v1, $v1, -10 beqz$v1, quit# Quit if Enter Key addiu$v1, $v1, -38 blez$v1, ranloop# Ignore zero & most special keys

14 Exercise 9.7 Continued divu$t0, $v1 mfhi$a0# Get Remainder addi$a0, $a0, 1# Will get numbers between 1 and N li$v0, 1# N depends on which key is pressed syscall# For example "6" key simulates Dice li$v0, 4# Print " & " la$a0, spxx syscall # Generate Second Random Number mflo$t0# Get last Quotient divu$t0, $v1 mfhi$a0# Get Remainder addi$a0, $a0, 1# Will get numbers between 1 and N li$v0, 1# N depends on which key is pressed syscall# For example "6" key simulates Dice bclear

15 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.

16 Exercise 9.12 (Assembly Language) Adduovf ( x, y, s) LabelOp-Code Dest. S1, S2Comments Adduovf: lw$t3, 0($sp)# Get x lw$t4, 4($sp)# Get y addu$t5, $t4, $t3# Get sum sw$t5, 8($sp)# Put sum on stack xor$t6, $t3, $t4# Checking MSD of x and y bltz$t6, diff bgez$t3, ok overflow: break# Will cause an exception ok: jr$ra diff:bgez$t5, overflow# Checking MSD of sum jr$ra

17 Exercise 9.13 Write a function Add64 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: Add64(x1: $t1, x0: $t0, y1: $t3, y0: $t2, z1: $t5, z0: $t4) 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) $t2 $t4 $t0 $t3 $t5 $t1 x1Sumx0

18 Exercise 9.13 (Assembly Language) LabelOp-Code Dest. S1, S2Comments Add64: lw$t3, 8($sp)# Get y upper lw$t2, 12($sp)# Get y lower lw$t5, 16($sp)# Get z upper lw$t4, 20($sp)# Get z lower addu$t0, $t2, $t4# add lower half add$t1, $t3, $t5# add upper half xor$t6, $t2, $t4# Checking for a carry to upper half bltz$t6, diff same:bgez$t2, fini carryaddi$t1, $t1, 1# Generate carry to upper fini:sw$t1, 0($sp)# Return x upper sw$t0, 4($sp)# Return x lower jr$ra diff: bgez$t0, carry bfini


Download ppt "Exceptions and Interrupts ◆ Breakpoints, arithmetic overflow, traps, and interrupts are all classified as exceptions. ◆ An exception is an event that requires."

Similar presentations


Ads by Google