Open In App

Difference Between ISR and Function Call

Last Updated : 06 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In embedded systems programming, or when working with microcontrollers, one has to distinguish between the terms "Interrupt Service Routine" and "function call". Generally, ISRs and function calls are used to execute some blocks of the code. But, they serve different purposes and, in some cases, may run under different conditions. In this article, a few main distinctions between ISRs and function calls are considered, which will be useful to define when to use both of them within a project.

Interrupt Service Routine(ISR)

The ISR is a particular type of function that should be executed automatically in response to an interrupt. An interrupt is basically a signal to the processor to stop its present execution and deal with an urgent task immediately. ISRs are mainly used in embedded systems that handle real-time events, such as dealing with timers, input/output operations, or even communication protocols.

What is an Interrupt?

  • An interrupt is a special type of condition that occurs during the working of a microprocessor.
  • Microprocessor services the interrupt by executing a subroutine called interrupt service routine (ISR).
  • The interrupt can be given to the processor by the external signal(i.e. on external pins of a Microprocessor) or as a software interrupt or by the condition produced by the program.

Interrupt Handling Mechanism

  • When an interrupt occurs, the processor first finishes the current instruction.
  • It then suspends the current program and executes an ISR.
  • For this, it pushes the value of PC (address of next instruction) into the stack.
  • Now it loads the ISR address into PC and proceeds to execute the ISR.
  • At the end of the ISR, it pops the return address from the stack and loads it back into PC.
  • This is how the processor returns to the very next instruction in the program.
Interrupt Handling Mechanism
Interrupt handling mechanism

Example 1:
 I/O transfer by interrupt driven I/O

  • If an I/O device which wants to perform a data transfer with the processor, must give an interrupt to the processor.
  • An interrupt is a condition that makes the processor execute an ISR (Interrupt Service Routine).
  • In the ISR, the processor will perform data transfer with the I/O device.
  • Whenever a device wants to transfer data, it will interrupt the processor.

Example 2:
Interrupt request by pressing the keyboard key,

  • Instead of the processor checking all the time, whether a key is pressed, the keyboard interrupts the processor as when we press a key. In the ISR of the keyboard, which is a  part of the keyboard driver software, the processor will read the data from the keyboard.

Advantages of ISR

  • Based on Real-Time: ISRs occur instantly in response to any time-critical event that would require attention to be paid to critical tasks immediately.
  • Resource Efficient: It is resource-efficient as the whole system is put to hold and critical tasks can have priority handling.
  • System Responsiveness: Enhances system responsiveness by interruption of the current running process that is handling high-priority events in the process of function.

Drawbacks of ISR - Interrupt Service Routine

  • Complex Debugging: ISRs are a bit difficult to debug since ISRs may be called at any time, which is again dependent on various factors and may lead to unexpected behavior.
  • Limited Execution Time: ISRs should be short and efficient; more execution time may result in the system getting failed in its normal operation and giving a chance for other interrupts.
  • Context Switching Overhead: ISRs introduce some overhead since the currently running processor state needs to be saved and restored upon an interrupt.

Function Call

A function call is a normal way of calling a piece of code that executes some particular task within the program. Other than ISRs, function calls are executed sequentially within the flow of a main program. Functions are the basic units in programming, which enable code modularity and reuse.

  • A subroutine (procedure/Subroutine) is a set of instructions needed repeatedly by the program. It is stored as a subroutine and called from several places by the main program.
  • In the case of an 8086 processor, a subroutine is invoked by a CALL instruction or  and control returns by a RET instruction.
  • It reduces the size of the program.
  • Executes slow because time is wasted to push and pop the return address in the stack.
  • It depends on the stack.

Advantages of Function Call

  • Modularity of code: Due to the use of function calls, it becomes rather easy to break down the code into smaller fragments that are easier to handle; this brings about modularity of code.
  • Code Reusability: The functions can be used any number of times within a program, which in turn saves the developer from redundancy and ultimately helps save development time.
  • Control of Execution Flow: Function calls make the flow of execution predictable and controlled, and hence the code is readable. It is, therefore, rather easier to follow through with the logic of a program.

Disadvantages of Function Call

  • Sequential Execution: Function calls form part of the main flow of control of a program and hence are not as well-suited for serving time-critical events as compared to ISRs.
  • Overhead: Though small, repeated function calls may lead to some overhead in resource-constrained environments.
  • Lack of real time handling: Since function calls provide no facility for immediate reaction to events from outside the program, functions are not good candidates for real-time applications.

Difference Between ISR and Function Call

Aspect

ISR (Interrupt Service Routine)

Function Call

Purpose

Handles time-critical tasks triggered by interrupts.

Executes a block of code in the program's normal flow.

Execution

Automatically executed in response to an interrupt signal.

Manually invoked by the program's control flow.

Timing

Executed immediately when an interrupt occurs.

Executes sequentially based on program logic.

Context Switching

Typically involves saving the current state and switching context.

No context switching; follows the normal program stack.

Priority

High-priority, preemptive execution over normal code.

Follows the program's priority and execution order.

Real-Time Handling

Designed for real-time, urgent processing.

Not suitable for handling real-time tasks.

Overhead

Can introduce context-switching overhead.

Minimal overhead during function invocation.

Complexity

More complex, especially with nested interrupts.

Generally simpler and easier to debug.

Use Case

Used for handling hardware events, timers, or I/O operations.

Used for modularizing code and reusing functions.

Execution Time Limitation

Should be short to avoid affecting system performance.

No strict time limitation; depends on program design.

Conclusion

In summary, ISRs and function calls implement two different things in programming, especially in embedded systems. ISRs are quite important in handling real-time and urgent tasks initiated through interrupts, which guarantee timely responsiveness of the system. However, they introduce complexities that include debugging difficulties and execution speed. Function calls, on the other side, help an individual organize codes in regard to modularity and reusability within the normal flow of a program. They may lack the ability to handle real-time events, but they do provide a structured and predictable execution path. It is relevant to make sense of the differences and where to use each one appropriately in software development.


Next Article
Article Tags :

Similar Reads