Difference between return from interrupt(RTI) and return from subroutine(RTS)

19,717

Solution 1

Usually return from interrupt restores the flags so that the interrupted code can continue to execute properly. Return from subroutine does not need to do that instruction is used intentionally in that flow of code and known that the flags are or are not destroyed depending on the architecture. In architectures that use a stack for the return address it is very apparent. A return from interrupt will pop the flags then the return address where a return from subroutine will pop only the return address.

Solution 2

When a hardware interrupt occurs on an x86 the flags and return code segment+offset are pushed onto the stack. Then interrupts are disabled. This is to set the stage for the interrupt routine to service the interrupt: switch stacks or whatever it wants to do before either re-enabling interrupts and processing some more before/or returning from the interrupt. The iret instruction pops the previously saved flags (including the interrupt flag which was originally enabled) and the return location so that the interrupted routine can continue processing none the wiser.

Solution 3

The fact that a processor is handling an interrupt is marked in one or more flags in the status register of a CPU.

This is needed, e.g. to mask other potential interrupts.

In order to tell the processor that interrupt handling is over and the flag can be reset, the RTI instruction is used instead of just RTS.

Of course the details depend on the particular CPU.

Solution 4

RET will just pop the two bytes to PC (Addr low and Addr High) RETI will reset the interrupt enable flipflop and two bytes will be poped from the stack

Solution 5

Well, interrupts are stored in a different code segment, so RTI are called with a far-call, while subroutines usually stays in the same code segment as the main program, so they're called with a near-call. The difference is that far-calls push the segment and the offset as return address, while near-calls push only the offset.

Share:
19,717
Amit Singh Tomar
Author by

Amit Singh Tomar

SOreadytohelp

Updated on August 09, 2022

Comments

  • Amit Singh Tomar
    Amit Singh Tomar almost 2 years

    I would like to know what is difference between return from interrupt(RTI) and return from subroutine(RTS). Are both are the same or there is any difference between these two?

  • Gunther Piez
    Gunther Piez over 13 years
    I would like to add that in many architectures additional information is pushed on the stack and so the stack frame has a slightly different layout in the interrupt routine.
  • Olof Forshell
    Olof Forshell over 13 years
    It is not "marked" as you call it. If it is a hardware interrupt the interrupt bit will be set to disabled. However this can be caused by "normal" processing so it is no reliable indicator of an interrupt in progress.
  • Curd
    Curd over 13 years
    @Olof Forshell: Seems you are thinking about one particular architecture. Since AMIT didn't refer to a particular architecture I just outlined the general behaviour that is common or similar to all architecures I know. Of course there are more details, as I mentioned.
  • Amit Singh Tomar
    Amit Singh Tomar over 13 years
    Thakns for you response @dwelch .....the flag that return from interrupt pop out is related to what???
  • Curd
    Curd over 13 years
    @AMIT: the flags (content of Processor Status Word) poped at RTI are the ones that were pushed at the start of the interrupt by the processor. This is, however, not automatically the case for all archtetures: Good old Z80 or 8051 architecture do NOT push the PSW at interrupts. In those cases the code of the iterrupt handler has to take care of it.
  • old_timer
    old_timer over 13 years
    @AMIT @Curd That is why I started with the word Usually, I should have been a little less subtle. You did not specify an architecture so I had to be a bit general. Architectures that do not have a difference between return from interrupt and return from subroutine often do not have separate instructions. when they do it is usually something like popping the status word or for changing an internal processor protection state or something like that.
  • Michael Petch
    Michael Petch over 6 years
    Question was about RTI and RTS, they are for architectures other than x86. RET/IRET are x86/x86-64