This is a 6502 assembler I wrote, originally to support the 6502 chiplab. You can inspect the assembler output in the hex editor, and you can also download your assembled programs.
You can run your programs on an emulator of choice. I have also made a real 6502 available online, and you can submit your programs to run against a real chip.
You can learn more about the 6502, and get links to other resources at my 6502 page.
If you’re just here for the assembler, scroll to the bottom of this page.
; this program contains only two bytes, since the first output byte
; is at 0x8000
org $8000
dw $cafe
; this program outputs 0x8002 two bytes, since the first output byte
; is at 0x00
org 0 ; this is implicit, not needed at start of program
db
org $8000
dw $cafe
6502 instructions can be split into roughly 4 categories. Each of these categories supports different addressing modes for its operands.
Name | Syntax Example | Group 1 | Group 2 | Group 3 | Bytes for input | Explanation |
---|---|---|---|---|---|---|
Immediate | lda #120 | ✓ | ✓ | 1 | Direct input, encoded in the instruction | |
Zero Page | lda 120 | ✓ | ✓ | ✓ | 1 | Optimized form of “absolute”. Only supports addresses <= 0xFF |
Absolute | lda 300 | ✓ | ✓ | ✓ | 2 | Use a memory location. |
Zero Page, X | lda 20,X | ✓ | ✓ | ✓ | 1 | Optimized form of “absolute, X” for the zero page. |
Absolute, X | lda 1000,X | ✓ | ✓ | 2 | Input is at address arg + X | |
Absolute, Y | lda 1000,Y | ✓ | ✓ | 2 | Input is at address arg + Y | |
(Indirect, X) | lda (10,X) | ✓ | 1 | Input is at mem[ mem[arg] + x ] | ||
(Indirect), Y | lda (10),Y | ✓ | 1 | Input is value mem[arg] + y |
Most instructions within a group follow this support table, but there are some exceptions. Check a complete 6502 reference for more details.
These instructions support the widest variety of addressing modes.
Assembler | Description |
---|---|
ora | Or with Accumulator |
and | And with acc |
eor | Exclusive or with acc |
adc | add with carry |
sbc | subtract with carry |
lda | load acc from memory |
sta | store acc into memory |
cmp | compare with accumulator |
Most addressing modes are supported,
Assembler | Description |
---|---|
asl | arithmetic shift left |
rol | rotate left |
lsr | logical shift right |
ror | rotate right |
stx | store x into memory |
ldx | load x from memory |
dec | decrement value in memory |
inc | increment value in memory |
This group has the fewest addressing modes of the three groups.
Assembler | Description |
---|---|
bit | Bit test. Zero = acc & input. Neg = input bit 7. Overflow = input bit 6. |
jmp abs | jump to argument |
jmp (indirect) | jump to mem[argument] |
sty | store y to mem |
ldy | load y from mem |
cpx | compare x - mem |
cpy | compare y - mem |
These are conditional jumps, and possibly jump depending on the value of a particular bit in the status register. The argument is a single byte, and is interpretted as a signed 8 bit value. These can only jump from -128 to 127. The offset is relative to PC, which holds the address just after this instruction while executing.
Assembler | Description |
---|---|
bpl | branch if positive (negative clear) |
bmi | branch if negative (negative set) |
bvc | branch if overflow clear |
bvs | branch if overflow set |
bcc | branch if carry clear |
bcs | branch if carry set |
beq | branch if equal (zero set) |
bne | branch if not equal (zero clear) |
These instructions don’t use addressing modes, and their inputs are implicit in the name of the instruction.
Assembler | Description |
---|---|
brk | Trigger interrupt “Break” |
jsr | Jump to subroutine, saving return address on stack |
rti | Return from interrupt, restoring PC and flags from stack |
rts | Return from subroutine, restoring PC from stack |
php | Push flags to stack |
plp | Pull flags from stack |
pha | Push flags to stack |
pla | Pull frags from stack |
clc | Clear carry flag |
sec | Set carry flag |
sei | Set interrupt disable flag |
cli | Clear interrupt disable flag |
tya | Transfer y to acc |
clv | Clear overflow flag |
cld | Clear decimal mode flag |
sed | Set decimal mode flag |
dey | Decrement y (y = y - 1) |
dex | decrement x |
iny | Increment y (y = y + 1) |
inx | Increment x |
tay | Transfer(copy) acc to y |
txa | transfer x to acc |
txs | transfer x to stack |
tax | transfer acc to x |
tsx | transfer stack ptr to x |
nop | No-op, do nothing |