Neccesity of Interrupts
Neccesity of Interrupts
Necessity of Interrupts
When you have one or more I/O devices connected to a microprocessor system,
any one of them may demand service at any time. The microprocessor can
service these devices in one of the two ways. One way is to use the polling
routine. The other way is to use an interrupt. In the following section, we will
see both ways.
In polling, the microprocessor's software simply checks each of the I/O devices
every so often. During this check, the microprocessor tests to see if any device
needs servicing.
A more desirable method would be the one that allows the microprocessor to be
executing its main program and only stop to service I/O devices when it is told
to do so by the device itself. In effect, the method, would provide an external
asynchronous input that would inform the processor that it should complete
whatever instruction that is currently being executed and fetch a new routine
that will service the requesting device. Once this servicing is completed, the
processor would resume exactly where it left off. This method is called interrupt
method.
The necessity of interrupts in a microprocessor-based system arises from the need for
efficient, real-time handling of external or internal events without wasting CPU resources.
Interrupts allow the microprocessor to respond immediately to critical tasks or events and
then resume normal operations, making systems more efficient and responsive.
In the absence of interrupts, the microprocessor would have to rely on polling, where it
continuously checks devices or conditions for activity. This is inefficient because the
processor wastes time checking for events that may not occur frequently. Interrupts eliminate
the need for continuous polling, allowing the processor to perform other useful tasks and only
respond to an event when it occurs.
Example: A network interface card (NIC) may receive data packets at random times.
The interrupt mechanism allows the microprocessor to stop its current operation and
process the received packet as soon as it arrives.
Interrupts can be assigned different priorities, allowing the system to determine which events
are more important. If multiple interrupts occur, the microprocessor can handle the highest-
priority interrupt first, ensuring that critical tasks are addressed before less important ones.
Example: In a medical monitoring system, an interrupt from a heart rate sensor might
be given higher priority than one from a temperature sensor, ensuring that life-critical
signals are processed immediately.
Interrupts are essential in systems that require multitasking or time-sharing, such as operating
systems. They allow the processor to switch between multiple tasks efficiently, ensuring that
important tasks are not delayed by less urgent ones.
Example: In an operating system, a timer interrupt can be used to ensure that the
processor switches between different applications or processes at regular intervals,
allowing time-sharing between tasks.
Example: When a printer is ready to receive data from the processor, it sends an
interrupt signal, and the processor responds by sending the data. Once the data
transfer is complete, the processor resumes its previous task.
7. Power Efficiency
Interrupts help reduce power consumption in embedded systems. The processor can enter a
low-power or sleep mode when there is no immediate task to perform. It only "wakes up" and
consumes power when an interrupt signal is received, making the system more energy-
efficient.
Review Questions
Interrupts are signals that temporarily halt the execution of the current program and divert
the control of the microprocessor to execute a specific task or service routine. After the
interrupt service routine (ISR) is completed, the microprocessor resumes the execution of the
main program. Interrupts are used to handle urgent or important events, such as I/O device
requests, in a timely manner.
Example of Interrupt:
Consider a situation where a printer needs to print data after receiving a signal from the
microprocessor:
asm
In polling, the microprocessor periodically asks each device whether it needs any action to be
taken. If a device requires service, the processor responds accordingly; otherwise, it continues
checking the next device or performs other tasks. Polling can be a simple method for
managing devices, but it is generally less efficient than interrupts because it consumes more
processor time checking devices that may not need attention.
How Polling Works
The microprocessor runs a continuous loop, where it checks the status of one or more
devices in a predefined order.
If the device status indicates that it needs attention (e.g., data is ready to be read or
written), the processor performs the necessary actions.
If no device needs attention, the processor either continues polling or performs other
operations.
Example of Polling
Problem:
You want to continuously check whether a key has been pressed on a keyboard connected to
the microprocessor. The program checks the keyboard's status and only performs a specific
task if a key is pressed.
POLL_KEYBOARD:
CALL CHECK_KEY_STATUS ; Check if any key is pressed
JZ POLL_KEYBOARD ; If no key is pressed (zero flag set), keep polling
CALL READ_KEY ; If a key is pressed, read the key
JMP PROCESS_KEY ; Jump to process the key input
CHECK_KEY_STATUS:
; Code to check keyboard status
; If a key is pressed, return with zero flag clear (Z=0)
; Otherwise, set zero flag (Z=1)
RET
READ_KEY:
; Code to read the pressed key from the keyboard
RET
PROCESS_KEY:
; Process the key input (perform action based on the key pressed)
JMP POLL_KEYBOARD ; Return to polling after processing the key
Explanation:
If a key is pressed (zero flag is cleared), the processor reads the key using the
READ_KEY subroutine and then processes the input.
Printer Example:
Imagine a microprocessor managing a printer. The processor needs to check whether the
printer is ready to receive more data. It does this by polling the printer to check if it's free.
POLL_PRINTER:
CALL CHECK_PRINTER_STATUS ; Check if the printer is ready
JZ POLL_PRINTER ; If the printer is not ready, keep polling
CALL SEND_DATA_TO_PRINTER ; If the printer is ready, send data
JMP POLL_PRINTER ; Continue polling after sending data
CHECK_PRINTER_STATUS:
; Check printer status
; If printer is ready, clear the zero flag (Z=0)
; If printer is busy, set the zero flag (Z=1)
RET
SEND_DATA_TO_PRINTER:
; Send the required data to the printer
RET
Explanation:
The processor continuously checks the status of the printer using the
CHECK_PRINTER_STATUS subroutine.
If the printer is busy, the processor stays in the polling loop.
If the printer is ready, the processor sends data and resumes polling.
Polling involves the processor constantly checking the device status, which can waste
CPU time, especially if no devices need attention for a long period.
Interrupts are a more efficient alternative, where the device sends a signal (interrupt)
to the processor when it requires attention. The processor can handle other tasks and
only respond to the device when it’s necessary.
Key Differences:
Advantages of Polling
Disadvantages of Polling
1. Wastes Processor Time: The CPU spends valuable time checking devices even when
they don’t need service, reducing system efficiency.
2. Slower Response Time: Since polling occurs at intervals, a device might have to wait
before being checked, leading to slower response times.
3. Scalability Issues: Polling becomes inefficient as the number of devices increases
because the processor has to check each device individually.
Summary:
Polling is a technique where the processor regularly checks each device to see if it
needs attention, but it is less efficient than using interrupts.
Polling can lead to wasted processor time because the microprocessor might spend
time checking devices that don't need service.
In contrast, interrupts allow the processor to perform other tasks and only respond
when necessary.
Polling is simple to implement but can lead to inefficiency in larger systems or
systems with time-sensitive tasks.