Changes

Jump to: navigation, search

6502 Instructions - Introduction

2,670 bytes added, 13:03, 19 January 2022
no edit summary
There are 13 [[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 ==
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 ==
The 6502 has rudimentry addition and subtraction instructions, which operate on the accumulator (A):
ADC ; add with carry
SBC ; subtract with carry
It There are also increment and decrement instructions for the X and Y registers and for memory:  DEC ; decrement memory DEX ; decrement X register DEY ; decrement Y register INC ; increment memory INX ; increment X register INY ; increment Y register The 6502 also has instructions for left and right bit-shifts and rotations (which can act as multiply-by-2 and divide-by-2):
ASL ; arithmetic shift left
{{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!}}
 
== Program Flow ==
 
=== Unconditional Jump ===
 
An unconditional jump is like a "Goto" -- it sets the address of the next instruction to be executed (by modifying the Program Counter (PC)):
 
JMP ; jump to address
 
=== Jump to SubRoutine ===
 
A jump to a subroutine is also unconditional, but the current value of the Program Counter (PC) is placed on the stack so that when the subroutine (aka procedure, function, or method) is finished, execution can resume at the instruction after the jump to subroutine:
 
JSR ; jump to subroutine (pushes PC on stack, loads operand into PC)
RTS ; return from subroutine (pops PC from stack)
 
=== Conditional Branch ===
 
A conditional branch is like a jump, except that it is only performed if a certain condition is met:
 
BCC ; branch on carry clear (C flag is clear)
BCS ; branch on carry set (C flag is set)
BEQ ; branch if equal (Z flag is set)
BMI ; branch if minus (N flag is set)
BNE ; branch if not equal (Z flag is clear)
BPL ; branch if plus (N flag is clear)
BVC ; branch if overflow clear (V flag is clear)
BVS ; branch if overflow set (V flag is set)
 
Note that the operand for conditional branch instructions is a relative offset - a signed 8-bit value (in the range -128 to +127) that is added to the current PC. When writing assembler (or viewing disassembled code), the operand is ''written'' as an absolute address or label, but the actual [[Machine Language|machine language]] code uses the relative addressing mode. For this reason, a branch that is too far will not assemble and will produce an error message.

Navigation menu