Skip to content

Latest commit

 

History

History
106 lines (68 loc) · 7.3 KB

File metadata and controls

106 lines (68 loc) · 7.3 KB

The program flow of control refers to the order in which instructions are executed in a program. Under normal circumstances, the CPU executes instructions in sequence, based on the instruction cycle. However, when interrupts occur, they can change the flow of control to handle urgent tasks before resuming the main program. Below, we compare program execution without interrupts and with interrupts:


1. Program Flow of Control Without Interrupts

In the absence of interrupts, the CPU follows a straightforward and sequential flow of control. The execution proceeds from one instruction to the next, as dictated by the program counter (PC). Any change in this sequence is dictated explicitly by control flow instructions like jumps, loops, function calls, or branches.

Normal Program Execution (No Interrupts)

  1. Fetch-Decode-Execute Cycle: The CPU continuously fetches, decodes, and executes instructions from the memory.

  2. Sequential Flow: Instructions are executed in the order they appear in the memory, with the Program Counter (PC) incrementing after each instruction, unless a control flow instruction is encountered (e.g., jumps or branches).

  3. Control Flow Instructions:

    • Conditional Branches: The program counter may change if a condition is met, such as if-else statements, loops (for, while), or comparisons.
    • Unconditional Jumps: The program counter may jump to a specific memory address as specified in the instruction.
    • Function Calls: The program execution transfers to a different part of the code, typically by calling a function or subroutine. Once the function completes, the flow returns to the calling location.
  4. No Interruptions: In this scenario, the program flow is predictable and linear, except for the control flow instructions that change the sequence. The CPU does not perform any additional tasks until explicitly instructed by the program.


2. Program Flow of Control With Interrupts

When interrupts are enabled, the flow of control can be altered asynchronously, meaning the CPU can temporarily pause its current task to deal with urgent events. An interrupt is a signal sent to the CPU from hardware or software, requesting immediate attention for a specific task (e.g., input/output operations, timers, or hardware failures). After handling the interrupt, the CPU returns to the original task.

How Interrupts Affect Program Execution

  1. Normal Execution: The program proceeds as usual, executing instructions sequentially based on the program counter, just like in the non-interrupt scenario.

  2. Interrupt Occurrence:

    • An interrupt signal (hardware or software) is received by the CPU, requesting attention for a specific task, such as data from a peripheral device (keyboard, mouse, or network) or an internal event (e.g., timer).
  3. Saving Current State:

    • The CPU saves the current execution state, including the values of registers, the program counter (PC), and other relevant information, onto the stack or a special memory location. This is important because the CPU needs to resume its original task after the interrupt has been handled.
  4. Interrupt Service Routine (ISR):

    • The CPU switches control to an Interrupt Service Routine (ISR), a special piece of code designed to handle the interrupt. The ISR may perform tasks like reading input data, resetting hardware timers, or signaling other components.
    • The Interrupt Vector Table stores addresses of ISRs. When an interrupt occurs, the CPU checks this table to determine which ISR to execute.
  5. Resuming Normal Execution:

    • After completing the ISR, the CPU restores the saved state from the stack (including the previous value of the program counter) and resumes normal program execution from where it left off.

Steps in Program Flow with Interrupts

  1. Normal Program Flow: The CPU is executing instructions normally.
  2. Interrupt Occurs: An interrupt signal is sent by a device or software.
  3. Save Context: The CPU saves its current state (registers, PC, etc.) to a stack.
  4. Execute ISR: The CPU transfers control to the ISR (Interrupt Service Routine) to handle the event.
  5. Restore Context: After the ISR completes, the CPU restores the saved state.
  6. Resume Execution: The CPU resumes program execution from where it was interrupted.

Example of Program Flow Without and With Interrupts

Without Interrupts:

Let’s assume the program is running a loop that reads data from memory and performs calculations.

for (int i = 0; i < 10; i++) {
    result[i] = calculate(data[i]);
}
  • The CPU fetches each instruction, executes it, and proceeds in sequence.
  • Once the loop completes, the program moves to the next instruction, maintaining its flow uninterrupted.

With Interrupts:

In the same scenario, if an external device (e.g., a network card) needs immediate attention to process incoming data, an interrupt may occur.

  • During the execution of the loop, an interrupt is raised by the network card indicating that new data has arrived.
  • The CPU pauses the current execution (e.g., in the middle of calculate(data[i])), saves the state (registers, PC, etc.), and jumps to the Interrupt Service Routine (ISR) for the network device.
  • The ISR might process the incoming data, store it in memory, or notify the application of the new data's arrival.
  • After the ISR finishes, the CPU restores the saved state and resumes the loop from where it was interrupted.

Types of Interrupts

  1. Hardware Interrupts:

    • Generated by hardware devices (e.g., keyboard, mouse, disk drives) to signal the CPU that an event has occurred, such as the completion of an I/O operation or a clock tick.
  2. Software Interrupts:

    • Generated by programs to request system services, like system calls in operating systems, or to handle exceptions (e.g., division by zero).
  3. Exceptions:

    • A special kind of interrupt that occurs when an error or unexpected condition arises during the execution of a program (e.g., division by zero, invalid memory access).

Program Flow with Multiple Interrupts

When multiple interrupts occur simultaneously, or before the first interrupt has been fully serviced, the CPU needs to manage them efficiently. This can be done through:

  • Interrupt Prioritization: Some interrupts are given higher priority over others. If a higher-priority interrupt occurs while servicing a lower-priority one, the CPU can interrupt the current ISR and handle the more urgent task.
  • Interrupt Nesting: The CPU can handle multiple interrupts in sequence by saving the current ISR's state and handling higher-priority interrupts first.
  • Masking: Lower-priority interrupts can be disabled (or masked) while a higher-priority interrupt is being handled.

Conclusion

  • Without interrupts, the program flow is linear and predictable, following the fetch-decode-execute cycle with changes based only on control instructions.
  • With interrupts, the flow of control can be altered asynchronously by external or internal events. The CPU pauses its current task, executes an Interrupt Service Routine, and then returns to its previous task, all while ensuring minimal disruption to the overall program execution.