Interrupt detection

Interrupt detection has a special hardware circuit, the interrupt type number generated by the CPU, CPU can directly identify. The interrupt type number for an external interrupt needs to be read from a peripheral. In addition, internal interrupts take precedence over external interrupts (except for single-step interrupts) and cannot be masked by software methods (except for single-step interrupts).

Interrupt handling procedure

The interrupt processing process consists of the following steps:

  1. Turn off the interrupt. The CPU closes interrupts, that is, no longer accepts other external interrupt requests.
  2. Save the breakpoint. Push the address of the instruction where the interrupt occurred onto the stack so that the interrupt can be returned correctly after processing (note that it is possible to save the address of the instruction where the interrupt occurred or the address of the instruction next to the instruction where the interrupt occurred, depending on the type of interrupt).
  3. Identify the interrupt source. The CPU identifies the source of the interrupt, determines the interrupt type number, and finds the corresponding interrupt handler entry address
  4. (The above three steps are generally done by the hardware circuit to handle interrupts) save the scene. The relevant register where the break occurred (the register to be used by the interrupt service program) and the contents of the flag register are pushed onto the stack.
  5. Execute interrupt service routine. Go to the interrupt service routine entry and start executing, reopening interrupts at a timely time to allow response to higher-priority external interrupts.
  6. (The last three steps of general software, that is, interrupt handlers are completed) restore the scene and return. The information that was pushed onto the stack during “protect the scene” is bounced back into the register, and the interrupt return instruction is executed to return the main program to continue. (IRET instruction, no operands, 3 words from the top of the stack, respectively into the IP, CS and FLAGS registers)

Software open and close interrupt methods

In the flags register, there are two types of flags:

  1. Status flag. Set by hardware, read by software.
  2. Control flags. The hardware performs different functions according to the Settings set by the software.

Interrupt Flag IF:

  • Controls the response to maskable interrupts, allowing the CPU to respond to maskable interrupt requests IF=0. IF=1, the CPU response is not allowed to mask interrupt requests.
  • The IF flag bit can be set with instructions, such as STL, where the interrupt flag is set to 1. CLT, interrupt flag clear 0.
  • IF has no effect on unmasked interrupts and internal interrupts.


Interrupt processing diagram