We've just updated MediaWiki and its underlying software. If anything doesn't look or work quite right, please mention it to us. --RanAS
RTS: Difference between revisions
From SnesLab
(clarify derailment) |
(added image; sp incremented twice) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 35: | Line 35: | ||
|} | |} | ||
'''RTS''' (Return from Subroutine) is a | '''RTS''' (Return from Subroutine) is a 65x instruction used to return control from a subroutine that was called from the same [[bank]]. | ||
RTS pulls the [[return address]] from the [[stack]], but increments the value by one before loading it into the [[program counter]]. | RTS pulls the [[return address]] from the [[stack]], but increments the value by one before loading it into the [[program counter]]. In other words, RTS unwinds what [[JSR]] did to the stack. The [[stack pointer]] is incremented twice: during cycle 3 and cycle 4. | ||
No flags are affected. | No flags are affected. | ||
Line 45: | Line 45: | ||
RTS | RTS | ||
</pre> | </pre> | ||
[[File:rts.png]] | |||
Forgetting to RTS can cause [[derailment]] as control falls through the end of the subroutine into the next one or garbage. | Forgetting to RTS can cause [[derailment]] as control falls through the end of the subroutine into the next one or garbage. | ||
Line 63: | Line 65: | ||
* Pickens, John. http://www.6502.org/tutorials/6502opcodes.html#RTS | * Pickens, John. http://www.6502.org/tutorials/6502opcodes.html#RTS | ||
* Clark, Bruce. http://www.6502.org/tutorials/65c816opcodes.html#6.2.2.2 | * Clark, Bruce. http://www.6502.org/tutorials/65c816opcodes.html#6.2.2.2 | ||
* https://archive.org/details/mos_microcomputers_programming_manual/page/n128 | |||
Latest revision as of 20:00, 10 August 2024
Basic Info | |||||||
---|---|---|---|---|---|---|---|
Addressing Mode | Opcode | Length | Speed | ||||
Stack (RTS) | 60 | 1 byte | 6 cycles |
Flags Affected | ||||||||
---|---|---|---|---|---|---|---|---|
N | V | M | X | D | I | Z | C | |
. | . | . | . | . | . | . | . |
RTS (Return from Subroutine) is a 65x instruction used to return control from a subroutine that was called from the same bank.
RTS pulls the return address from the stack, but increments the value by one before loading it into the program counter. In other words, RTS unwinds what JSR did to the stack. The stack pointer is incremented twice: during cycle 3 and cycle 4.
No flags are affected.
Syntax
RTS
Forgetting to RTS can cause derailment as control falls through the end of the subroutine into the next one or garbage.
See Also
External Links
- Eyes & Lichty, page 496 on RTS
- Labiak, page 178 on RTS
- 8.2 on MCS6500 Manual, page 108 on RTS
- Carr, page 271 on RTS
- Leventhal, page 3-90 on RTS
- snes9x implementation of RTS: https://github.com/snes9xgit/snes9x/blob/master/cpuops.cpp#L2982
- Pickens, John. http://www.6502.org/tutorials/6502opcodes.html#RTS
- Clark, Bruce. http://www.6502.org/tutorials/65c816opcodes.html#6.2.2.2
- https://archive.org/details/mos_microcomputers_programming_manual/page/n128