JSR (Jump to SubRoutine)

PC = PC - 1; Push (PC >> 8); Push (PC & 0xFF); PC = [operand]

JSR performs a subroutine call by storing the address of the next instruction onto the stack, and then setting the program counter (PC) to the address of the subroutine specified by the operand. When the subroutine completes, a RTS instruction is typically used to return control to the calling routine.

Addressing Modes

Mode

Syntax

Bytes

Cycles

Absolute

JSR addr

20 ll hh

6

Flags Affected

None.

Examples

Jump to a Subroutine

    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

  • RTS (ReTurn from Subroutine)

Comments