RTS (ReTurn from Subroutine)

PC = Pop(); PC = (PC | (Pop() << 8)) + 1

RTS performs a return from subroutine by retrieving the address of the next instruction from the stack and setting the program counter (PC) to that address plus one.

Addressing Modes

Mode

Syntax

Bytes

Cycles

Implied

RTS

60

6

Flags Affected

None.

Examples

Simple Subroutine Call and Return

    JSR SUBROUTINE ; jump to subroutine
    ; code to execute after subroutine returns
    RTS            ; return from subroutine
SUBROUTINE:
    ; code for the subroutine
    RTS            ; return to the caller

Storing the Return Address in a Zero Page Location

    LDA #<SUBROUTINE   ; load the low byte of the subroutine address into A
    PHA                ; push it onto the stack
    LDA #>SUBROUTINE   ; load the high byte of the subroutine address into A
    PHA                ; push it onto the stack
    JMP (ZP_LOCATION)  ; jump to the address stored in a zero page location
ZP_LOCATION:
    .byte SUBROUTINE   ; the low byte of the subroutine address
    .byte SUBROUTINE>>8 ; the high byte of the subroutine address
SUBROUTINE:
    ; code for the subroutine
    RTS                ; return to the caller

See Also

  • JSR (Jump to SubRoutine)

Comments