Open In App

Return from Subroutine and Return from Interrupt

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss the Return from Subroutine and Return from Interrupt and will explain in detail and finally conclude with Difference between Return from Subroutine and Return from Interrupt. Let’s discuss it one by one.

Return from Subroutine :

  • To return from a subroutine is to return execution of code to the point it was before the subroutine was called.
  • Usually a subroutine is called by ‘CALL‘ instruction. At each CALL instruction, the IP and CS of the next instruction is pushed into the stack, before control is transferred to procedure. At the end of the procedure ‘RET’ instruction must be executed.
  • RET is used o return from a subroutine previously called by CALL. When it is executed, the previously stored content if the IP and CS are retrieved into the CS, IP registers from stack and the execution of the main program continues further.
  • Therefore given that calling a subroutine pushes the current address PC+2 onto stack. Returning from a subroutine will return execution at the address calculated by the topmost two bytes of the stack. 
  • The most significant byte is popped off the stack first, followed by the least significant byte. Address of the RET is loaded from the stack. After the RET is executed, the stack and its pointers are modified accordingly.

Return from Interrupt :

  • To return from interrupt is to return code execution to the point it was at before the interrupt has occurred along with saving flags into flag register.
  • Usually when an interrupt occurs, t is handled by calling its respective service routine. When an interrupt service routine is to be called, before transferring control to it, the IP, CS along with flags are stored on to the stack to indicate the location from where the execution is to be continued after the Interrupt Service Routine (ISR) is executed. So, at the end of ISR, ‘IRET‘ is executed, the values of IP,CS, and flags are retrieved from stack to continue the execution of the main program.
  • IRET or RETI is used to return from ISR. It first loads flags from stack and restores its previous value, i.e. it enables the flag interrupt. After loading flags from stack, it then loads CS, IP values into the registers from the stack and execution of the main program continues further. The flags are pushed into and popped from stack using ‘PUSHF’ and ‘POPF’ instructions respectively. IRET  address is loaded from stack and the global interrupt flag is enabled. After the IRET is executed, the stack and its pointers are modified accordingly.

Difference between Return from Subroutine and Return from Interrupt : 

  1. The IRET instruction is used to exit from an interrupt procedure while RET is to return from an subroutine.
  2. IRET is similar to RET except that RET will just pop two bytes to PC while IRET will reset the interrupt enable (IEN)  flip flop and two bytes will be popped from the stack. 
  3. In order to tell the processor that interrupt handling is over and flag can be reset, the IRET instruction is used instead of just RET.
  4. IRET functions identically to RET if it is executed outside of an ISR.

Last Updated : 10 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads