instruction reference

ADD AND ASR BEQ BMI BNE
DEX DEY EOR HLT INA INX
INY JMP JSR LDA LDM LDS
LDX LDY LSL LSR MFC MFS
MFX MFY MTC MTS MTX MTY
MXY MYX NOP ORA OUT PHA
PHX PHY PLA PLX PLY ROL
ROR RTS SEQ SLO SLT STM
SWP SXS SXY SYS TSR

ADD - Add binary

Operation

[destination] ← [source] + [destination]

Syntax

ADD An, Am    - $An0m
ADD An, #imm8 - $Bn00 + imm8 (1 byte)

Timing

The immediate form takes 5 cycles, while the register form takes 4 cycles.

Description

Add the source operand (Am or imm8) to the destination operand (An) and store the result in the destination location. C will be set to 1 if an overflow occurred, 0 if not.

AND - Bitwise AND

Operation

[destination] ← [source] & [destination]

Syntax

AND An, Am    - $An2m
AND An, #imm8 - $Bn20 + imm8 (1 byte)

Timing

The immediate form takes 5 cycles, while the register form takes 4 cycles.

Description

AND the source operand (Am or imm8) to the destination operand (An) and store the result in the destination location.

ASR - Arithmetic shift right

Operation

[destination] ← [destination] shifted right by [source]

Syntax

ASR An, Am    - $An7m
ASR An, #0..7 - $Bn7i

Timing

In both cases, the minimum cycle time is 4 cycles, and the maximum is 11 cycles. The cycles required can be determined with the formula 4\+count where count is the shift amount.

Description

Arithmetically shift the bits of the operand right. The shift count can be specified either as a literal from 0 to 7 (0 produces no shift), or in a register. If a register is used, the value is modulo 8. The bits in the operand are shifted into C.

BEQ - Branch if equal

Operation

IF C == 0 THEN [PC] ← [PC] + d

Syntax

BEQ <label> - $C0 + rel8 (1 byte)

Timing

If the branch is taken, the instruction takes 4 cycles. Otherwise, it takes only 3 cycles.

Description

If the C register is zero, program execution continues at location [PC] + displacement, d. The displacement is a two's complement value. The value in the PC corresponds to the current location plus two. The range of the branch is -126 to +128 bytes.

BMI - Branch on minus

Operation

IF C[7] == 1 THEN [PC] ← [PC] + d

Syntax

BMI <label> - $C1 + rel8 (1 byte)

Timing

If the branch is taken, the instruction takes 4 cycles. Otherwise, it takes only 3 cycles.

Description

If bit 7 of C is set, program execution continues at location [PC] + displacement, d. The displacement is a two's complement value. The value in the PC corresponds to the current location plus two. The range of the branch is -126 to +128 bytes.

BNE - Branch if not equal

Operation

IF C != 0 THEN [PC] ← [PC] + d

Syntax

BNE <label> - $C2 + rel8 (1 byte)

Timing

If the branch is taken, the instruction takes 4 cycles. Otherwise, it takes only 3 cycles.

Description

If the C register is not zero, program execution continues at location [PC] + displacement, d. The displacement is a two's complement value. The value in the PC corresponds to the current location plus two. The range of the branch is -126 to +128 bytes.

DEX/DEY - Decrement X/Y

Operation

[X/Y] ← [X/Y] - 1

Syntax

DEX - $F7
DEY - $F9

Timing

Both DEX and DEY take 2 cycles.

Description

Decrement X or Y by 1. The C register will be set to 0 if X/Y becomes zero after the operation, otherwise it is set to 0xFF implicitly.

EOR - Exclusive bitwise OR

Operation

[destination] ← [source] ^ [destination]

Syntax

EOR An, Am    - $An4m
EOR An, #imm8 - $Bn40 + imm8 (1 byte)

Timing

The immediate form takes 5 cycles, while the register form takes 4 cycles.

Description

EOR the source operand (Am or imm8) to the destination operand (An) and store the result in the destination location.

HLT - Halt

Operation

Halt the CPU

Syntax

HLT - $FF

Timing

HLT takes 2 cycles.

Description

Halt the CPU until an interrupt is received. During this time, no instructions will be processed.

INA - Input from port

Operation

[destination] ← [I/O port]

Syntax

INA An, Am    - $An8m
INA An, #imm8 - $Bn80 + imm8 (1 byte)

Timing

At minimum, INA will take 4 cycles to complete (5 cycles if using the immediate form). This number may be higher due to system design.

Description

Read the value from the I/O port specified in the source operand (Am or imm8) into the destination operand (An)

INX/INY - Increment X/Y

Operation

[X/Y] ← [X/Y] + 1

Syntax

INX - $F6
INY - $F8

Timing

Both INX and INY take 2 cycles.

Description

Increment X or Y by 1. The C register will be set to 0 if X/Y becomes zero after the operation, otherwise it is set to 0xFF implicitly.

JMP - Jump

Operation

[PC] ← [destination]

Syntax

JMP <label> - $F3 + abs16 (2 bytes)

Timing

JMP takes 4 cycles to complete.

Description

Program execution continues at the effective address specified by the instruction.

JSR - Jump to subroutine

Operation

[S] ← [S] - 2; M([S]) ← [PC]

[PC] ← [destination]

Syntax

JSR <label> - $F4 + abs16 (2 bytes)

Timing

JMP takes 7 cycles to complete.

Description

JSR pushes the 16-bit address of the instruction immediately following the JSR onto the system stack. Program execution then continues at the effective address specified in the instruction.

LDA/LDX/LDY/LDS - Load immediate into destination

Operation

[destination] ← [immediate literal]

Syntax

LDA An, #imm8 - $8n + imm8 (1 byte)
LDX #imm16    - $F0 + imm16 (2 bytes)
LDY #imm16    - $F1 + imm16 (2 bytes)
LDS #imm16    - $F2 + imm16 (2 bytes)

Timing

LDA takes 3 cycles to complete, while LDX/LDY/LDS take 5, due to the required additional fetch for the 16-bit literal.

Desciption

Load the immediate literal specified into the destination.

LDM - Load from memory

Operation

[destination] ← M([source])

Syntax

LDM An, [X] - $1n
LDM An, [Y] - $3n
LDM An, [S] - $5n

Timing

LDM takes 4 cycles to complete.

Description

Load the destination with the value at the memory address specified by the source.

LSL/LSR - Logical shift left/right

Operation

[destination] ← [destination] shifted by [source]

Syntax

LSL An, #0..7 - $An5m
LSL An, Am    - $Bn5i
LSR An, #0..7 - $An6m
LSR An, Am    - $Bn6i

Timing

In both cases, the minimum cycle time is 4 cycles, and the maximum is 11 cycles. The cycles required can be determined with the formula 4\+count where count is the shift amount.

Description

Logically shift the bits of the operand in the specified direction. The shift count can be specified either as a literal from 0 to 7 (0 produces no shift), or in a register. If a register is used, the value is modulo 8. The bits in the operand are shifted into C.

MFr - Move from register

Operation

[destination] ← [source]

Syntax

MFC An - $En
MFX An - $FA0n
MFY An - $FA1n
MFS An - $FA2n

Timing

MFC takes 2 cycles to complete, while MFX/MFY/MFS takes 4 cycles.

Description

Copy the value of the source register into the destination. r specifies the source register:

MnemonicDescription
MFCMove from C
MFXMove from X
MFYMove from Y
MFSMove from S

If r is X, Y, or S, the source operand will be copied into 2 registers, the low byte in the source operand (An) and the high byte in the register next to it (A(n+1)). If An is A15, the high byte will be in A0.

MTr - Move to register

Operation

[source] ← [destination]

Syntax

MTC An - $DN
MTX An - $FA3n
MTY An - $FA4n
MTS An - $FA5n

Timing

MTC takes 2 cycles to complete, while MTX/MTY/MTS takes 4 cycles.

Description

Write the value of the source register into the destination. r specifies the source register:

MnemonicDescription
MTCMove to C
MTXMove to X
MTYMove to Y
MTSMove to S

If r is X, Y, or S, 2 registers will be used to perform the write, with the low byte in the source operand (An) and the high byte in the register next to it (A(n+1)) being written to the destination. If An is A15, the high byte will be in A0.

MXY/MYX - Move X to Y / Move Y to X

Operation

[Y] ← [X] / [X] ← [Y]

Syntax

MXY - $FA60
MYX - $FA70

Timing

Both MXY and MYX take 4 cycles to complete.

Description

Copy the full contents of register X into Y (MXY), or register Y into X (MYX).

NOP - No operation

Operation

None

Syntax

NOP

Timing

NOP takes 2 cycles to complete.

Description

NOP performs no operation. Execution continues with the instruction following the NOP instruction.

ORA - Bitwise OR

Operation

[destination] ← [source] | [destination]

Syntax

ORA An, Am    - $An3m
ORA An, #imm8 - $Bn30 + imm8 (1 byte)

Timing

The immediate form takes 5 cycles, while the register form takes 4 cycles.

Description

OR the source operand (Am or imm8) to the destination operand (An) and store the result in the destination location.

OUT - Output to port

Operation

[I/O port] ← [destination]

Syntax

OUT Am, An    - $AnFm
OUT #imm8, An - $BnF0 + imm8 (1 byte)

Timing

At minimum, OUT will take 4 cycles to complete (5 cycles if using the immediate form). This number may be higher due to system design.

Description

Write the value from the I/O port specified in the source operand (Am or imm8) into the destination operand (An)

PHA/PHX/PHY - Push to stack

Operation

[S] ← [S] - 1; M([S]) ← [An] (PHA)

[S] ← [S] - 2; M([S]) ← [X/Y] (PHX/PHY)

Syntax

PHA An - $4n
PHX    - $FAE0
PHY    - $FAE1

Timing

PHA takes 3 cycles to complete, while PHX/PHY takes 6 cycles.

Description

Push the 8-bit register (An) or 16-bit register (X/Y) onto the stack.

PLA/PLX/PLY - Pull from stack

Operation

M([S]) → [An]; [S] ← [S] + 1 (PLA)

M([S]) → [X/Y]; [S] ← [S] + 2 (PLX/PLY)

Syntax

PLA An - $9n
PLX    - $FAF0
PLY    - $FAF1

Timing

PLA takes 3 cycles to complete, while PLX/PLY takes 6 cycles.

Description

Pull an 8-bit value from the stack into An or a 16-bit value into X/Y.

ROL/ROR - Rotate left/right

Operation

[destination] ← [destination] rotated by [source]

Syntax

ROL An, #0..7 - $AnCm
ROL An, Am    - $BnCi
ROR An, #0..7 - $AnDm
ROR An, Am    - $BnDi

Timing

In both cases, the minimum cycle time is 4 cycles, and the maximum is 11 cycles. The cycles required can be determined with the formula 4\+count where count is the shift amount.

Description

Rotate the bits of the operand in the specified direction. The rotate operation is circular in the sense that the bit shifted out at one end is shifted into the other end, that is, no bit is lost or destroyed by a rotate operation. The shift count can be specified either as a literal from 0 to 7 (0 produces no rotation), or in a register. If a register is used, the value is modulo 8. The bits in the operand are shifted into C.

RTS - Return from subroutine

Operation

[PC] ← M([S]); [S] ← [S] + 2

Syntax

RTS

Timing

RTS takes 4 cycles to complete.

Description

The program counter is pulled from the stack and the previous value of the PC is lost. RTS is used to terminate a subroutine.

SEQ - Set if equal

Operation

IF [destination] == [source] THEN C ← 0x00 ELSE C ← 0xFF

Syntax

SEQ An, Am    - $An9m
SEQ An, #imm8 - $Bn90 + imm8 (1 byte)

Timing

The immediate form takes 5 cycles, while the register form takes 4 cycles.

Description

If the source (Am or imm8) is equal to the destination (An), C will be set to 0, otherwise it is set to 0xFF.

SLO - Set if lower than

Operation

IF [destination] < [source] THEN C ← 0x00 ELSE C ← 0xFF

Syntax

SLO An, Am    - $AnBm
SLO An, #imm8 - $BnB0 + imm8 (1 byte)

Timing

The immediate form takes 5 cycles, while the register form takes 4 cycles.

Description

If the source (Am or imm8) is lower than the destination (An), C will be set to 0, otherwise it is set to 0xFF. Both operands are treated as unsigned operands.

SLT - Set if less than

Operation

IF [destination] < [source] THEN C ← 0x00 ELSE C ← 0xFF

Syntax

SLT An, Am    - $AnAm
SLT An, #imm8 - $BnA0 + imm8 (1 byte)

Timing

The immediate form takes 5 cycles, while the register form takes 4 cycles.

Description

If the source (Am or imm8) is less than the destination (An), C will be set to 0, otherwise it is set to 0xFF. Both operands are treated as signed operands.

STM - Store into memory

Operation

M([destination]) ← [source]

Syntax

STM An, [X] - $2n
STM An, [Y] - $7n
STM An, [S] - $6n

Timing

STM takes 3 cycles to complete.

Description

Store the value from the source into memory at the memory address specified by the source.

SWP - Swap register with accumulator

Operation

[destination] ↔ [An:An+1]

Syntax

SWP An, X - $FABn
SWP An, Y - $FACn
SWP An, S - $FADn

Timing

SWP takes 4 cycles to complete.

Description

Swap the contents of X/Y/S with a 16-bit value specified with the low byte in An and the high byte in the register next to it A(n+1). If An is A15, the high byte will be in A0.

SXY/SXS/SYS - Swap register with register

Operation

[destination] ↔ [source]

Syntax

SXY - $FA80
SXS - $FA90
SYS - $FAA0

Timing

SXY/SXS/SYS takes 4 cycles to complete.

Description

Exchange the contents of the destination and source together.

TSR - Transfer register to register

Operation

[destination] ← [source]

Syntax

TSR An, Am - $AnEm

Timing

TSR takes 4 cycles to complete.

Description

Copy the source (Am) into the destination (An).