Understanding the 6502: What is an Opcode?

If you’re new to programming the 6502 microprocessor—or revisiting it from the world of modern computing—one of the first terms you’ll encounter is the opcode. This blog post lays the foundation for the rest of the series by explaining what opcodes are, how they relate to the 6502’s architecture, and why understanding them is key to mastering the platform.

What Is an Opcode?

An opcode (short for operation code) is a single byte of machine code that tells the processor what operation to perform. The 6502 has 151 documented opcodes, each representing a specific instruction like loading a value into a register, adding two numbers, or jumping to another location in memory.

Example:

  • $A9 is the opcode for LDA with immediate addressing (Load Accumulator with a constant value).

Every instruction the CPU executes starts with an opcode. Some are followed by operands (data or addresses), depending on the instruction and the addressing mode.

Opcodes and Addressing Modes

The same instruction—like LDA—can have different opcodes depending on how it accesses memory. The 6502 supports a variety of addressing modes, including:

  • Immediate: LDA #$10 → load the value $10
  • Zero Page: LDA $00 → load from address $0000
  • Absolute: LDA $1234 → load from full 16-bit address
  • Indexed: LDA $20,X → add X to the base address
  • Indirect: for instructions like JMP

Each variant has its own opcode byte. For example:

  • LDA #$10$A9 10
  • LDA $10$A5 10
  • LDA $1234$AD 34 12

So when we say “the 6502 has 151 opcodes,” we’re counting instruction + addressing mode combinations, not just the instruction names.

The Structure of a 6502 Instruction

A typical instruction has this structure:

C#
[Opcode] [Operand (02 bytes)]

Examples:

  • CLC (Clear Carry) → one byte: $18
  • LDA #$42 → two bytes: $A9 42
  • JMP $1234 → three bytes: $4C 34 12

The number of bytes used and the number of CPU cycles required to execute the instruction depends on the addressing mode.

Why Learn Opcodes?

Understanding opcodes gives you low-level control over the CPU. This knowledge allows you to:

  • Optimize code for speed and size
  • Write routines for graphics, sound, and input
  • Build emulators, disassemblers, and debuggers
  • Understand what’s really going on beneath a BASIC program

Even when using an assembler that lets you write LDA #$10, it’s the opcode $A9 that gets assembled and stored in memory.

What’s Next in the Series?

This post is the beginning of a journey through every single 6502 opcode. In the upcoming entries, we’ll group instructions by functionality—such as loading data, arithmetic, branching, and stack operations—and break them down in detail.

Each post will include:

  • Instruction description
  • Addressing modes
  • Effects on flags
  • CPU cycles
  • Practical examples

Stay tuned, and let’s start unlocking the power of the 6502—one opcode at a time.


Leave a Reply

Your email address will not be published. Required fields are marked *