Lably
Skip to content

Interrupt handler

Computer systems programming special block code

  • 8 min read
  • 6 chapters

Related on Lably

Through Exception handling, Operating system and Computer keyboard

The traditional form of interrupt handler is the hardware interrupt handler. Hardware interrupts arise from an electrical trigger. The specifics of how an interrupt is handled depends on the hardware, but generally the CPU pauses execution of its current task, saves current program counter (and often flags) to the stack, and updates its program counter to the address of the routine that is responsible for servicing the interrupt. The interrupt service routine must then save any registers it may change. In general, hardware interrupts and their handlers are used to handle high-priority conditions that require the interruption of the current code the processor is executing.

It is convenient for software to be able to trigger the same mechanism: a software interrupt. Rather than using a hard-coded interrupt dispatch table at the hardware level, software interrupts are often implemented at the operating system level as a form of callback function.

Interrupt handlers have a multitude of functions, which vary based on what triggered the interrupt and the speed at which the interrupt handler completes its task. For example, pressing a key on a computer keyboard, or moving the mouse, triggers interrupts that call interrupt handlers which read the key, or the mouse's position, and copy the associated information into the computer's memory.

An interrupt handler is a low-level counterpart of event handlers. However, interrupt handlers have an unusual execution context, many harsh constraints in time and space, and their intrinsically asynchronous nature makes them notoriously difficult to debug by standard practice (reproducible test cases generally don't exist), thus demanding a specialized skillset—an important subset of system programming—of software engineers who engage at the hardware interrupt layer.

Nested Interrupts

During the execution of an interrupt service routine, it is possible for another interrupt to occur. How this second interrupt is handled is dependent on the hardware. In general, a developer will decide whether or not to let the current ISR be interrupted to handle a higher priority interrupt. If so, the current program counter (and often flags) will be saved to the stack before the address of the higher priority ISR is written to the program counter. The higher priority interrupt service routine must then save any registers it may alter. Upon completion of the higher priority ISR, the address and state from the lower priority ISR will be popped from the stack and execution of the lower priority ISR will continue.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication