Open main menu

CDOT Wiki β

Changes

6502 Instructions - Introduction

56 bytes added, 16:09, 13 September 2022
no edit summary
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.
== Loading and Storing Data (to/from Memory) 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:
STY ; store the Y register
==== Push/Pull on the Stack ====
When a value is pushed to the stack, the stack pointer register (SP) is decremented and the selected register is written to memory location $0100+SP.
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:
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):
'''For full details on all of the arithmetic and bitwise instructions, see the [[6502 Math]] page.'''
=== Test and Comparison Operations ===
The A, X, and Y registers can be directly compared with immediate or memory values:
{{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:
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:
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.
=== Manipulating Flags ===
The 6502 provides instructions for setting and clearing various condition flags:
Note that there is no instruction to set the overflow (V) flag.
=== Miscellaneous Instructions ===
BRK ; "BREAK" - turn control over to the debugger