JMP

From C64-Wiki
Jump to: navigation, search

JMP (short for "JuMP") is the mnemonic for a machine language instruction which unconditionally transfers program execution to the specified address. To those familiar with BASIC programming; this is the machine language equivalent to GOTO.

Vectors and indirectly addressed JuMPs[edit]

JMP is the only instruction in the 65xx instruction set which supports the purely indirect addressing mode, and this indirect JMP provides for the use of vectors: While the user has no easy access to modify the contents of either BASIC or KERNAL ROM, both these parts of the system makes ample use of vectors, set in RAM that the user can modify. This allows the user to "divert" parts of BASIC or KERNAL to custom routines.

JMP-ing into a subroutine[edit]

Normally, subroutines are called with a JSR, but consider some routine that prints out a message, e.g. fetched from a table of several messages. After printing any one of the messages, a carriage return control character is to be printed, before the message routine ends in an RTS. The "standard way" to print the this would be:

LDA #13      Print carriage return
JSR CHROUT   using CHROUT
RTS          Message done -- return to caller.

but since the call to a subroutine (the JSR CHROUT) is immediately followed by an RTS, there is the option of saving a byte and 9 machine cycles by JuMP-ing directly into the subroutine:

LDA #13      Print carriage return
JMP CHROUT   Leave this routine through CHROUT

This will first print out the required character, but when the CPU encounters the "final RTS" at the end of the CHROUT routine, this serves to return from not just CHROUT, but of the entire message-printing routine.

Addressing modes[edit]

Opcode Addressing
mode
Assembler
format
Length
in bytes
Number of
cycles
Dec Hex
76 4C Absolute JMP nnnn 3 3
108 6C Indirect JMP (nnnn) 3 5

JMP supports 2 different addressing modes, as shown in the table at right. In the assembler formats listed, nnnn is a two-byte (16-bit) address.

CPU flags[edit]

JMP does not affect any of the CPU's status flags.