6502 Assembly



This book is a guide to the 6502 Assembly language. This book will teach the different memory addressing modes and instructions of the 8-bit 6502 processor.

You might want to learn 6502 assembly language programming if you want to do Atari 2600/8-bit family/5200/7800 Programming, Commodore PET/VIC/64/128 Programming, Acorn 8 Bit Programming, Apple I/II Programming, NES Programming or Super NES Programming.

Syntax
Syntax will vary between assemblers - this book will use the following syntax throughout:

Memory layout
16-bit values are stored in memory in little-endian, so the least significant byte is stored before the most significant. E.g. if address  contains   and address   contains , reading a two-byte value from   will result in.

Signed integers are in two's complement and can represent values from -128 to +127. Bit 7 is set if the integer is negative.

The 6502's program counter is 16 bits wide, so up to 2^16 (65536) bytes of memory are addressable. Certain regions of memory are reserved for particular purposes:

Memory Addressing Modes
Each instruction uses one of thirteen memory addressing modes, which determines the operand of the instruction. An example is provided for each.

Accumulator: A
The Accumulator is implied as the operand, so no address needs to be specified.

 Example 

Using the ASL (Arithmetic Shift Left) instruction with no operands, the Accumulator is always the value being shifted left.

ASL

Implied: i
The operand is implied, so it does not need to be specified.

 Example 

The operands being implied here are X, the source of the transfer, and A, the destination of the transfer.

TXA

Immediate: #
The operand is used directly to perform the computation.

 Example 

The value  is loaded into the Accumulator.

LDA #$22

Absolute: a
A full 16-bit address is specified and the byte at that address is used to perform the computation.

 Example 

The value  at address   is loaded into the X register.

LDX $D010

Zero Page: zp
A single byte specifies an address in the first page of memory, also known as the zero page, and the byte at that address is used to perform the computation.

 Example 

The value at address  is loaded into the Y register.

LDY $02

Relative: r
The offset specified is added to the current address stored in the Program Counter (PC). Offsets can range from -128 to +127.

 Example 

The offset  is added to the address in the Program Counter (say  ). The destination of the branch (if taken) will be.

BPL $2D

Absolute Indirect: (a)
The little-endian two-byte value stored at the specified address is used to perform the computation. Only used by the  instruction.

 Example 

The addresses  and   are read, returning   and   respectively. The address  is then jumped to.

JMP ($A001)

Absolute Indexed with X: a,x
The value in  is added to the specified address for a sum address. The value at the sum address is used to perform the computation.

 Example 

The value  in   is added to   for a sum of. The value  at address   is used to perform the add with carry ( ) operation.

ADC $C001,X

Absolute Indexed with Y: a,y
The value in  is added to the specified address for a sum address. The value at the sum address is used to perform the computation.

 Example 

The value  in   is added to   for a sum of. The value  at address   is incremented ( ) and   is written back to.

INC $F001,Y

Zero Page Indexed with X: zp,x
The value in  is added to the specified zero page address for a sum address. The value at the sum address is used to perform the computation.

 Example 

The value  in   is added to   for a sum of. The value  at address   is loaded into the Accumulator.

LDA $01,X

Zero Page Indexed with Y: zp,y
The value in  is added to the specified zero page address for a sum address. The value at the sum address is used to perform the computation.

 Example 

The value  in   is added to   for a sum of. The value  at address   is loaded into the Accumulator.

LDA $01,Y

Zero Page Indexed Indirect: (zp,x)
The value in  is added to the specified zero page address for a sum address. The little-endian address stored at the two-byte pair of sum address (LSB) and sum address plus one (MSB) is loaded and the value at that address is used to perform the computation.

 Example 

The value  in   is added to   for a sum of. The address  at addresses   and   will be where the value   in the Accumulator is stored.

STA ($15,X)

Zero Page Indirect Indexed with Y: (zp),y
The value in  is added to the address at the little-endian address stored at the two-byte pair of the specified address (LSB) and the specified address plus one (MSB). The value at the sum address is used to perform the computation. Indeed addressing mode actually repeats exactly the Accumulator register's digits.

 Example 

The value  in   is added to the address   at addresses   and   for a sum of. The Accumulator is then exclusive ORed with the value  at.

EOR ($2A),Y

Instructions
These are the instructions for the 6502 processor including an ASCII visual, a list of affected flags, and a table of opcodes for acceptable addressing modes.

Compare and Test Bit
The Negative (N), Zero (Z), and Carry (C) status flags are used for conditional (branch) instructions.

All Compare instructions affect flags in the same way:

 Test Bits in Memory with Accumulator:  

A & M

Flags: N = M7, V = M6, Z

Stack
The processor status is stored as a single byte with the following flags bits from high to low: NV--DIZC.

Subroutines and Jump
 Jump to New Location:  

Jump to new location by changing the value of the program counter.

Warning: When used with the absolute indirect addressing mode, a hardware bug can result in unexpected behavior when the specified address is. E.g.  will read the low byte from   and the high byte from , instead of reading the high byte from   as one would expect. This is due to an overflow in the lower byte of the indirect address not being carried into the upper byte.

Flags: none

 Jump to New Location Saving Return Address:  

Jumps to a subroutine

The address before the next instruction (PC - 1) is pushed onto the stack: first the upper byte followed by the lower byte. As the stack grows backwards, the return address is therefore stored as a little-endian number in memory. PC is set to the target address.

Flags: none

 Return from Subroutine:  

Return from a subroutine to the point where it called with.

The return address is popped from the stack (low byte first, then high byte). The return address is incremented and stored in PC.

Flags: none

 Return from Interrupt:  

Return from an interrupt.

P is popped from the stack. PC is popped from the stack.

Flags: all

Miscellaneous
 Break:  

Force an Interrupt

Flags: B = 1, I = 1

 No Operation:  

No Operation

Flags: none