📜  ISR 和函数调用的区别

📅  最后修改于: 2021-09-27 14:27:22             🧑  作者: Mango

打断 :

  • 中断是微处理器工作期间发生的一种特殊情况。
  • 微处理器通过执行称为中断服务程序 (ISR) 的子程序来服务中断。
  • 中断可以通过外部信号(即在微处理器的外部引脚上)或作为软件中断或由程序产生的条件提供给处理器。

中断处理机制:

  • 当中断发生时,处理器首先完成当前指令。
  • 然后它挂起当前程序并执行 ISR。
  • 为此,它将 PC 的值(下一条指令的地址)压入堆栈。
  • 现在它将 ISR 地址加载到 PC 中并继续执行 ISR。
  • 在 ISR 结束时,它从堆栈中弹出返回地址并将其加载回 PC。
  • 这就是处理器返回到程序中的下一条指令的方式。

中断处理机制

示例 1 –
通过中断驱动的 I/O 进行 I/O 传输

  • 如果一个 I/O 设备想要与处理器进行数据传输,必须给处理器一个中断。
  • 中断是使处理器执行 ISR(中断服务程序)的条件。
  • 在 ISR 中,处理器将与 I/O 设备进行数据传输。
  • 每当设备想要传输数据时,它都会中断处理器。

示例 2 –
按键盘键中断请求,

  • 与处理器始终检查是否按下某个键不同,键盘会在我们按下某个键时中断处理器。在键盘的 ISR 中,它是键盘驱动软件的一部分,处理器将从键盘读取数据。

函数:

  • 子程序(procedure/Subroutine)是程序重复需要的一组指令。它存储为子程序并由主程序从多个地方调用。
  • 在 8086 处理器的情况下,子程序由 CALL 指令调用或由 RET 指令控制返回。
  • 它减少了程序的大小。
  • 执行缓慢,因为在堆栈中推送和弹出返回地址浪费了时间。
  • 这取决于堆栈。

ISR 和函数调用的区别:

Sl. No.

Interrupt service routine(ISR) 

Function call 

1.

The interrupt is usually initiated by an internal (i.e. divided by zero, register overflow etc.) or a external signal (i.e. external pins of) microprocessor  rather than the execution of instructions(i.e. software interrupt). After storing the current status of the program (i.e. value of PC ,PSW ) in the stack, the ISR is executed. ISR performs different types of tasks depending on the device which interrupted or instructions written by a programmer(in the case of software interrupts). The function call is invoked by execution of instructions, which perform the specific tasks, and also reduces the size of the program.

2.

  • The ISR address is written inside the interrupt vector table.
  • For example – 
    In the case of 8086, the first 1KB of memory, address 00000 H … 003FF H, is reserved for the IVT.
  • The ISR address for each interrupt is fixed.
The address of the subroutine is written inside the instructions which is written in the main program code.  

3.

The address of the ISR is determined by the hardware. The address of the subroutine is written inside the main program.

4.

ISR is used for all general purpose tasks.
For example –  
If the paper in the printer is not present, then the interrupt is generated by the printer which executes an ISR(i.e. error message on the display). 
Function calls are made for program specific tasks(i.e. for application specific tasks).

5.             

When an interrupt occurs during the execution of a current program, therefore, after execution of the current instruction, the processor executes ISR. After the execution of ISR, the processor must resume to program exactly as before the interrupt occurred. For this, the content of the PC, content of µP registers and the content of some status conditions is stored.
The collection of all status bit conditions in a microprocessor is called PSW(program status word).

During the interrupt cycle, the contents of PC and PSW are pushed onto the stack. The branch address for the particular interrupt is then passed to PC and a new PSW is loaded into the status register.

The last instruction in the ISR is the return from interrupted instruction. When this instruction is executed, the old PSW and the return address are popped from the stack. 

Here, only a PC is stored on the stack to get the address of the next instruction in the main program.

It is necessary for the subroutine to have access to data from the calling subroutine and to return results to that subroutine. Therefore, subroutine parameters and data linkage is done.

This can be done through 

  • The AC register can be used for a single input parameter and a single output parameter. In computers with multiple processor registers, more parameters can be passed this way.
  • Another way to pass data to a subroutine is through the memory.