[[6502]]
# 6502 Instructions
## **The Instruction Set**
The 6502 has a relatively basic set of instructions, many having similar functions (e.g. memory access, arithmetic, etc.). The following sections list the complete set of 56 instructions in functional groups.
### Load/Store Operations
These instructions transfer a single byte between memory and one of the registers. Load operations set the negative ([[6502 registers#N|N]]) and zero ([[6502 registers#Z|Z]]) flags depending on the value of transferred. Store operations do not affect the flag settings.
| | | |
| --- | --- | --- |
| [[6502 reference#LDA\|LDA]] | Load Accumulator | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#LDX\|LDX]] | Load X Register | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#LDY\|LDY]] | Load Y Register | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#STA\|STA]] | Store Accumulator | |
| [[6502 reference#STX\|STX]] | Store X Register | |
| [[6502 reference#STY\|STY]] | Store Y Register | |
### Register Transfers
The contents of the X and Y registers can be moved to or from the accumulator, setting the negative ([[6502 registers#N|N]]) and zero ([[6502 registers#Z|Z]]) flags as appropriate.
| | | |
| --- | --- | --- |
| [[6502 reference#TAX\|TAX]] | Transfer accumulator to X | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#TAY\|TAY]] | Transfer accumulator to Y | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#TXA\|TXA]] | Transfer X to accumulator | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#TYA\|TYA]] | Transfer Y to accumulator | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
### Stack Operations
The 6502 microprocessor supports a 256 byte stack fixed between memory locations $0100 and $01FF. A special 8-bit register, S, is used to keep track of the next free byte of stack space. Pushing a byte on to the stack causes the value to be stored at the current free location (e.g. $0100,S) and then the stack pointer is post decremented. Pull operations reverse this procedure.
The stack register can only be accessed by transferring its value to or from the X register. Its value is automatically modified by push/pull instructions, subroutine calls and returns, interrupts and returns from interrupts.
| | | |
| --- | --- | --- |
| [[6502 reference#TSX\|TSX]] | Transfer stack pointer to X | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#TXS\|TXS]] | Transfer X to stack pointer | |
| [[6502 reference#PHA\|PHA]] | Push accumulator on stack | |
| [[6502 reference#PHP\|PHP]] | Push processor status on stack | |
| [[6502 reference#PLA\|PLA]] | Pull accumulator from stack | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#PLP\|PLP]] | Pull processor status from stack | All |
### Logical
The following instructions perform logical operations on the contents of the accumulator and another value held in memory. The BIT instruction performs a logical AND to test the presence of bits in the memory value to set the flags but does not keep the result.
| | | |
| --- | --- | --- |
| [[6502 reference#AND\|AND]] | Logical AND | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#EOR\|EOR]] | Exclusive OR | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#ORA\|ORA]] | Logical Inclusive OR | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#BIT\|BIT]] | Bit Test | [[6502 registers#N\|N]], [[6502 registers#V\|V]], [[6502 registers#Z\|Z]] |
### Arithmetic
The arithmetic operations perform addition and subtraction on the contents of the accumulator. The compare operations allow the comparison of the accumulator and X or Y with memory values.
| | | |
| --- | --- | --- |
| [[6502 reference#ADC\|ADC]] | Add with Carry | [[6502 registers#N\|N]], [[6502 registers#V\|V]], [[6502 registers#Z\|Z]], [[6502 registers#C\|C]] |
| [[6502 reference#SBC\|SBC]] | Subtract with Carry | [[6502 registers#N\|N]], [[6502 registers#V\|V]], [[6502 registers#Z\|Z]], [[6502 registers#C\|C]] |
| [[6502 reference#CMP\|CMP]] | Compare accumulator | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]], [[6502 registers#C\|C]] |
| [[6502 reference#CPX\|CPX]] | Compare X register | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]], [[6502 registers#C\|C]] |
| [[6502 reference#CPY\|CPY]] | Compare Y register | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]], [[6502 registers#C\|C]] |
### Increments & Decrements
Increment or decrement a memory location or one of the X or Y registers by one setting the negative ([[6502 registers#N|N]]) and zero ([[6502 registers#Z|Z]]) flags as appropriate,
| | | |
| --- | --- | --- |
| [[6502 reference#INC\|INC]] | Increment a memory location | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#INX\|INX]] | Increment the X register | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#INY\|INY]] | Increment the Y register | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#DEC\|DEC]] | Decrement a memory location | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
| [[6502 reference#DEX\|DEX]] | Decrement the X register | [[6502 instructions#N\|N]], [[6502 instructions#Z\|Z]] |
| [[6502 reference#DEY\|DEY]] | Decrement the Y register | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]] |
### Shifts
Shift instructions cause the bits within either a memory location or the accumulator to be shifted by one bit position. The rotate instructions use the contents if the carry flag ([[6502 registers#C|C]]) to fill the vacant position generated by the shift and to catch the overflowing bit. The arithmetic and logical shifts shift in an appropriate 0 or 1 bit as appropriate but catch the overflow bit in the carry flag ([[6502 registers#C|C]]).
| | | |
| --- | --- | --- |
| [[6502 reference#ASL\|ASL]] | Arithmetic Shift Left | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]], [[6502 registers#C\|C]] |
| [[6502 reference#LSR\|LSR]] | Logical Shift Right | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]], [[6502 registers#C\|C]] |
| [[6502 reference#ROL\|ROL]] | Rotate Left | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]], [[6502 registers#C\|C]] |
| [[6502 reference#ROR\|ROR]] | Rotate Right | [[6502 registers#N\|N]], [[6502 registers#Z\|Z]], [[6502 registers#C\|C]] |
### Jumps & Calls
The following instructions modify the program counter causing a break to normal sequential execution. The [[6502 reference#JSR|JSR]] instruction pushes the old [[6502 registers#PC|PC]] onto the stack before changing it to the new location allowing a subsequent [[6502 reference#RTS|RTS]] to return execution to the instruction after the call.
| | | |
| --- | --- | --- |
| [[6502 reference#JMP\|JMP]] | Jump to another location | |
| [[6502 reference#JSR\|JSR]] | Jump to a subroutine | |
| [[6502 reference#RTS\|RTS]] | Return from subroutine | |
### Branches
Branch instructions break the normal sequential flow of execution by changing the program counter if a specified condition is met. All the conditions are based on examining a single bit within the processor status.
| | | |
| --- | --- | --- |
| [[6502 reference#BCC\|BCC]] | Branch if carry flag clear | |
| [[6502 reference#BCS\|BCS]] | Branch if carry flag set | |
| [[6502 reference#BEQ\|BEQ]] | Branch if zero flag set | |
| [[6502 reference#BMI\|BMI]] | Branch if negative flag set | |
| [[6502 reference#BNE\|BNE]] | Branch if zero flag clear | |
| [[6502 reference#BPL\|BPL]] | Branch if negative flag clear | |
| [[6502 reference#BVC\|BVC]] | Branch if overflow flag clear | |
| [[6502 reference#BVS\|BVS]] | Branch if overflow flag set | |
Branch instructions use relative address to identify the target instruction if they are executed. As relative addresses are stored using a signed 8 bit byte the target instruction must be within 126 bytes before the branch or 128 bytes after the branch.
### Status Flag Changes
The following instructions change the values of specific status flags.
| | | |
| --- | --- | --- |
| [[6502 reference#CLC\|CLC]] | Clear carry flag | [[6502 registers#C\|C]] |
| [[6502 reference#CLD\|CLD]] | Clear decimal mode flag | [[6502 registers#D\|D]] |
| [[6502 reference#CLI\|CLI]] | Clear interrupt disable flag | [[6502 registers#I\|I]] |
| [[6502 reference#CLV\|CLV]] | Clear overflow flag | [[6502 registers#V\|V]] |
| [[6502 reference#SEC\|SEC]] | Set carry flag | [[6502 registers#C\|C]] |
| [[6502 reference#SED\|SED]] | Set decimal mode flag | [[6502 registers#D\|D]] |
| [[6502 reference#SEI\|SEI]] | Set interrupt disable flag | [[6502 registers#I\|I]] |
### System Functions
The remaining instructions perform useful but rarely used functions.
| | | |
| --- | --- | --- |
| [[6502 reference#BRK\|BRK]] | Force an interrupt | [[6502 registers#B\|B]] |
| [[6502 reference#NOP\|NOP]] | No Operation | |
| [[6502 reference#RTI\|RTI]] | Return from Interrupt | All |
___
This page was last updated on 2nd January 2002