Instructions

All legal 6502 opcodes consist of a instruction and an addressing mode. The instruction tells us what an opcode will do and how it will affect registers, flags and memory. For example there are 8 LDA opcodes which will all load some data into the A register; but differ in what addressing mode they have which determines where said data will be fetched from. All instructions will have a short 3 letter symbol that is used when writing an assembly program.

## List of Instructions

### Load Instructions

Load instructions load a byte from memory as an operand and store it in a register. The N and Z flags will be affected in the following way:

* The N flag will contain bit 7 of the loaded byte.
* The Z flag will be set if the loaded byte is 0 or cleared otherwise.

Load Instructions

### Store Instructions

Sore instructions store the value that is in a register into memory, these instructions do not affect any flags.

Store Instructions

### Transfer Instructions

Transfer instructions transfer the data that is in a register into another register. All of the transfer instructions **except the TXS instruction** will set/clear the N and Z flags in the following way:

* The N flag will contain bit 7 of the transferred byte.
* The Z flag will be set if the transferred byte is 0 or cleared otherwise.

### Arithmetic Instructions

The 6502 has instructions for two of the four basic arithmetic with carry in; the ADC instruction for addition and the SBC instruction for subtraction. Both of these instructions will take in a byte as an operand and the carry flag and perform their operation with the accumulator before moduloing the result by 256 and storing the result back in the accumulator. The addition operation will add the carry flag to the result and the subtraction instruction will subtract the inverse of the carry flag to the result. The operations will be packed binary coded decimal operations if the decimal flag is cleared or binary coded decimal operations if it set. There is no addition and subtraction operations that do not carry in so the carry flag is usually cleared or set with the CLC and SEC instructions respectively. The flags will be set in the following ways:

* The N flag will be bit 7 (the sign bit) of the result of the addition/subtraction.
* The Z flag will be set if the result is 0 or cleared otherwise.
* The C flag will be set if an addition before being modulated resulted in a number larger than 255 (binary)/$99 (decimal) or cleared if a subtraction before being modulated resulted in a number less than 0. It will be cleared/set respectively otherwise.
* The V flag will be set if an addition before being modulated resulted in a number that when represented as a two's complement is outside the -128 to 127 range or cleared if a subtraction before being modulated resulted in a number that when represented as a two's complement is outside the -128 to 127 range. It will be cleared/set respectively otherwise.

Arithmetic Instructions
 SBC | **S**u**b**tract Memory from Accumulator with **C**arry/Borrow In

### Bitwise Instructions

There are three bitwise operations: bitwise AND, bitwise inclusive OR (OR) and bitwise exclusive OR (EOR/XOR). These instructions for these operations take a byte as a operand perform said instruction on the accumulator with the operand and store the result in the accumulator. Each bit of the result will be the result of performing the bitwise operation between the bit of the accumulator and operand with the same significance.

Bitwise Instructions
 ORA | Inclusive **Or** with **A**ccumulator
 EOR | **E**xclusive **Or** with Accumulator

### Bit Shift and Rotate Instructions

Bit shift and rotate instructions all bitshift the accumulator to the left or right by one and store the result back in the accumulator. The bit shift instructions will shift a 0 bit into the bit at the opposite end to where a bit was shifted out while the rotate instructions will shift in the carry flag. The flags will be set in the following way:

* The N flag will be bit 7 (the sign bit) of the result of the addition/subtraction.
* The Z flag will be set if the result is 0 or cleared otherwise.
* The C flag will be set to the bit that was shifted out.

Bit Shift and Rotate Instructions
 LSR | **L**ogical **S**hift **R**ight Accumulator by 1 without Carry In
 ROL | **Ro**tate Accumulator **L**eft by 1 with Carry In
 ROR | **Ro**tate Accumulator **R**ight by 1 with Carry In

### Comparison Instructions

Compare instructions take a byte as an operand and subtract said operand from a register before discarding the result and only keeping some flags. Unlike the SBC instruction; the decimal and carry flags will be ignored and the operation will always be a packed binary subtraction without carry in and the overflow flag will remain unchanged. The flags will be set in the following way:

* The N flag will be bit 7 (the sign bit) of the result of the subtraction.
* The Z flag will be set if the register and operand are equal or cleared otherwise.
* The C flag will be cleared if the operand is greater than the register or set otherwise.

Comparison Instructions

### Increment/Decrement Instructions

Increment and decrement will add or subtract one respectively from an index register (but not the accumulator) or memory address. Although they are addition and subtraction instructions, they will ignore the decimal (always perform a packed binary addition/subtraction) and carry flags and will not change the carry and overflow flags. The flags will be set in the following way:

* The N flag will be bit 7 (the sign bit) of the new value.
* The Z flag will be set if the new value is 0 or cleared otherwise.

### Pull from Stack Instructions

The pull instructions will pull a byte from stack and store it in the accumulator or flags register.

Pull from Stack Instructions

### Push to Stack Instructions

The push instructions will push a byte onto the stack from the accumulator or flags register. No flags will be affected.

Push to Stack Instructions

### Directly Set/Clear Flag Instructions

The carry, decimal and interrupt flags can be directly be set and cleared with set and clear flag instructions. The overflow flag however, only has a clear instruction and no set instruction while the negative and zero flags have no set or clear instructions.

### Unconditional Flow Control Instructions

There are unconditional flow control instructions that allow the program counter to be directly set, allowing for jumping to an address or subroutine and returning from a subroutine or interrupt. These instructions are unconditional and so will always jump to the address and the addresses are absolute and as such are not dependant on the current program counter. A JMP instruction will only jump to an address while a JSR instruction will first push a return address then jump to an address allowing an RTS instruction to be used to return to the instruction after the JSR instruction. An RTI instruction will return from an interrupt and will also restore the processor status that was pushed when the interrupt was triggered.

Unconditional Flow Control Instructions
 JSR | **J**ump to **S**ub**r**outine
 RTS | **R**e**t**urn from **S**ubroutine
 RTI | **R**e**t**urn from **I**nterrupt

### Conditional Branch Instructions

The branch instructions all branch to a relative address if and only if a specific flag is set or cleared. As they are relative, they will branch to an address that depends on the current address and an offset; this address jumped to is the current address plus a signed two's complement byte.

## External Links

6502 Instruction Set - Masswerk

NMOS 6502 Opcodes - 6502.org