RTI is the 6502 instruction that finishes an interrupt-handling routine. It returns execution to the interrupted program after the routine has restored the registers and interrupt state saved on the stack. section 5.13 section 4.4.3
Use RTI for a routine entered by an interrupt, such as the NES NMI routine; use RTS for an ordinary subroutine. The return instruction must be reached with the stack restored to its expected position. section 4.4.3
A typical NMI ending proceeds in this order:
- Pull the saved value for Y into A, then transfer it to Y with TAY. section 5.13
- Pull the saved value for X into A, then transfer it to X with TAX. section 5.13
- Pull the saved A value and execute RTI to return from the interrupt. section 5.13
Returning from the NMI routine
pla tay pla tax pla rti section 5.13
Listing 5.23 Restoring registers and returning (example.s)
#1
pla
tay
pla
tax
pla
rti
.endproc
Inspect the complete register-restoration sequence and its final RTI instruction. section 5.13
