1,885
edits
Changes
→Miscellaneous Instructions
The [[6502]] processor has a compact instruction set, consisting of just 56 instructions:
{{Admon/tip|Addressing Modes and Registers are essential!|Take the time to understand the [[6502 Addressing Modes|6502 addressing modes]] and [[6502#Registers|registers]]. This page groups these is essential background infromation for understanding the 6502 instructions and explains their basic function.}}
== Addressing Modes ==
There are 13 thirteen [[6502 Addressing Modes]]. All of these instructions work with at least one addressing mode, and many work with several addressing modes. See the [[#Resources|Resources]] section for Opcode tables that define which instructions work with which addressing modes. == Registers == Most of these instructions work with [[Register|registers]]. Refer to the [[6502#Registers|6502 page, Register section]] for details on the 6502's internal registers.
== Performance ==
To convert the number of cycles to time, multiply the cycles by the time between system [[Clock|clock]] pulses. Many 6502 systems operated at 1 MHz (1 million operations per second), and therefore 1 cycle corresponded to 1 millionth of a second, or 1 microsecond (uS). Therefore, an instruction that took 4 clock cycles would take 4 uS to execute.
== Instructions by Category == === Loading and Storing Data (to/from Memory) ===
==== Register-Memory Loads and Stores ====
There are three instructions to load data from memory to a register:
And there are three matching instructions to store data from a register to a memory location:
STA ; store the accumulator STX ; store the X register STY ; store the Y register ==== Push/Pull on the Stack ==== When a value is pushed to the stack, the selected register is written to memory location $0100+SP and the stack pointer register (SP) is decremented. When a value is pulled from the stack, the stack pointer register (SP) is incremented and the selected register is loaded from memory location $0100+SP. There are two instructions to push data onto the stack: PHA ; push the accumulator PHP ; push the processor status register (SR) And two matching instructions to pull data from the stack: PLA ; pull the accumulator PLP ; pull the processor status register (SR) Note that some other operations, such as JSR, interrupts, RTI, and RTS, cause data to be pushed to or pulled from the stack. ==== Transferring Data between Registers ==== The X and Y registers can be transferred to/from the accumulator: TAX ; transfer A to X TAY ; transfer A to Y TXA ; transfer X to A TYA ; transfer Y to A You can also transfer the Stack Pointer (SP) to/from the X register: TSX ; transfer SP to X TXS ; tranfer X to SP It is not possible to directly transfer the Status Register (SR) to a general-purpose register, but you can you transfer it via the stack (e.g., by pushing SR to the stack with with <code>PHP</code> and then popping the stack to the accumulator with <code>PLA</code>). === Arithmetic and Bitwise Operations === {{Admon/tip|Watch the Carry Flag!|Failing to clear the carry flag before addition or to set the carry flag before subtraction is the cause of many bugs in 6502 programs. The carry flag also affects the rotate instructions. Be sure to set or clear this flag with the <code>SEC</code> or <code>CLC</code> instructions when needed!}} '''For full details on all of the arithmetic and bitwise instructions, see the [[6502 Math]] page.''' The 6502 has basic addition and subtraction instructions, which operate on the accumulator (A):STA ADC ; add with carry SBC ; store subtract with carry There are also increment and decrement instructions for the accumulatorX and Y registers and for memory: DEC ; decrement memory DEX ; decrement X register DEY ; decrement Y register
=== Push/Pull on the Stack = Conditional Branch ====
The <code>ADC ; add with carryNOP</code>instruction does nothing. It can be used to pad code for alignment purposes, or unwanted code can be overwritten in situ with this opcode to disable it.