Everything2
Near Matches
Ignore Exact
Full Text
Everything2

6502 instructions

created by Cugel the clever

(thing) by Cugel the clever (2.5 mon) (print)   ?   2 C!s I like it! Tue Aug 15 2000 at 14:42:50

This is a guide to the 56 3-letter codes that make up the 6502 instruction set. The 6502 is an 8-bit processor that forms the heart of such (once) popular computers as the Atari 800 and the Commodore 64. The chip still has its uses, as can be seen on the link collection at www.6502.org.
(I will add explanations to each of the codes, have a bit of patience please)


(thing) by AT (2 mon) (print)   ?   2 C!s I like it! Tue Dec 24 2002 at 19:36:47

The 6502 instruction set, like most of the early 8-bit microprocessors, was quirky. They all tended to have lots of special purpose instructions, like TSX, rather than the more generalized MOV like more modern processors. While the mnemonics are more a matter of style, the inconsistancies carried through to other aspects. The 6502 has a myriad of addressing modes, but which ones are available is highly dependant on which operations and registers are being used.

For example, here is a list of the addressing modes. Only the accumulator version of the instruction will support all modes; the rest support just the most important. The modes are:

  • immediate: argument is value
  • zero page: argument is 8-bit pointer to value within first 256 bytes of memory
  • zero page, x: argument is 8-bit pointer, which is added to register x to get the value within first 256 bytes of memory
  • absolute: argument is 16-bit pointer to value
  • absolute, x: argument is 16-bit pointer, which is added to register x to get the value
  • absolute, y: argument is 16-bit pointer, which is added to register y to get the value
  • (indirect, x): argument is 8-bit pointer, which is added to register x to get another 16-bit pointer to the actual value
  • (indirect), y: argument is 8-bit pointer to another 16-bit pointer, which is added to register y to the actual value

Here is a summary of the 6502 instructions:

Logical intructions

  • AND (bitwise and memory with accumulator)
  • BIT (test bits between and memory and accumulator)
    Only affects the flags. The Z flag is set if a bitwise and results in zero. V and S are loaded with bits 6 and 7 of the memory operand.
  • EOR (bitwise exclusive or memory with accumulator)
  • ORA (bitwise inclusive or memory with accumulator)

Shifts and rotates

  • ASL (arithmetic shift left)
    The least significant bit becomes zero, and the high bit is shifted into the carry flag. This can be used for a unsigned multiply by a power of two.
  • LSR (logical shift right)
    The most significant bit becomes zero, and the low bit is shifted into the carry flag. This can be used for a unsigned divide by a power of two.
  • ROL (rotate left through carry flag)
    The carry flag is shifted into the least significant bit, and the high bit is shifted into the carry flag.
  • ROR (rotate right through carry flag)
    The carry flag is shifted into the most significant bit, and the low bit is shifted into the carry flag.

Arithmetic

  • ADC (add memory to accumulator with carry)
    If the decimal flag is set, packed BCD addition is performed (i.e. 0x09 + 0x01 -> 0x10). If the flag isn't set, regular binary addition is performed. The 6502 has no add without carry instruction.
  • SBC (subtract with carry)
    If the decimal flag is set, packed BCD subtraction is performed (i.e. 0x10 - 0x01 -> 0x09). If the flag isn't set, regular binary subtraction is performed. The 6502 has no subtract without carry instruction.

Increments and decrements

  • DEC (decrement memory)
  • DEX (decrement register x)
  • DEY (decrement register y)
  • INC (increment memory)
  • INX (increment register x)
  • INY (increment register y)

Comparisons

  • CMP (compare memory and accumulator)
  • CPX (compare memory and register x)
  • CPY (compare memory and register y)

Register transfers

  • TAX (transfer accumulator to register x)
  • TAY (transfer accumulator to register y)
  • TSX (transfer stack pointer to register x)
  • TXA (transfer register x to accumulator)
  • TXS (transfer register x to stack pointer)
  • TYA (transfer register y to accumulator)

Loads and stores

  • LDA (load accumulator)
  • LDX (load register x)
  • LDY (load register y)
  • STA (store accumulator)
  • STX (store register x)
  • STY (store register y)

Stack operations

  • PHA (push accumulator)
  • PLA (pull accumulator)
  • PHP (push processor status (flags))
  • PLP (pull processor status (flags))

Branches, jumps, and returns

  • Bcc (branch on condition code)
    Branch on one of the S, V, C, or Z flags. The mnemonics are BPL & BMI (plus or minus), BVC & BVS (overflow clear or set), BCC & BCS (carry clear or set), and BNE & BEQ (not equal or equal), respectively. Only a one byte signed displacement can be used; for father branches the JMP instruction must be used.
  • JMP (jump)
    Always jumps to an absolute 16-bit address, either immediate or indirect. Buggy if the indirect address is on a page boundary.
  • JSR (jump to subroutine)
    Pushes the program counter and jumps.
  • RTS (return from subroutine)

Flags sets and clears

  • Cflag, Sflag (clear or set a flag)
    The mnemonics are CLC & SEC (clear and set carry), CLI & SEI (clear and set interrupt), CLV (clear overflow), and CLD & SED (clear and set decimal). There is no instruction to set overflow.

Interrupt instructions

  • BRK (break)
    Triggers an interrupt and skips a byte. This instruction pushs the program counter and the status register and does an indirect jump to an interrupt handler. Although the opcode is one byte, the instruction is treated as if it is two bytes long.
  • RTI (return from interrupt)
    Restores the flags and program counter from the stack after an interrupt.

Miscellaneous


printable version
chaos

qualitative comparison of assembly language and unprotected sex ADC Truly Useful Op Codes NOP
and PHP ASL LDA
CMP TYA PIC opcodes 6502
LDX bcc BEQ bit
STX tax STA Commodore 64
Inc TAY CLI EOR
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help


cooled by Rancid_Pickle

Cool Staff Picks
Nodes to live by:
Catharsis, or Living for the Cool
Hector Berlioz
work
Futurist Manifesto of Lust
The Kinsey Institute
Lines in the mind
Esperanto
February 22, 2006
Children of the Corny: A Nodermeet Out on the Prairie
Books
Hellcat
Tell me a story about being really alive
Freedom of speech
New Writeups
jjen
Sorrier than I ever thought I would be(personal)
locke baron
Moskva class antisubmarine cruiser(thing)
Wuukiee
May 15, 2008(idea)
locke baron
Kuznetsov class aircraft carrier(thing)
_lesra
for abby(thing)
Adaptive Child
Annie's garden salsa(recipe)
Simulacron3
Zig-Zag(thing)
Ouzo
Special Grilled Cheese(fiction)
Noung
Tiananmen Square Massacre(idea)
aneurin
Lord St Clair(person)
artman2003
Assholes and Douchebags: A Comparison(person)
locke baron
Tyan Thunder K8WE(thing)
locke baron
Udaloy class destroyer(thing)
Scaevola
Same-sex marriage(idea)
SteveMurrayFromNZ
British Standard Handful(idea)
This affordable entertainment brought to you by The Everything Development Company