After learning how to load values into the accumulator using LDA
, it’s time to store them back into memory. That’s exactly what the STA
instruction does.
Where LDA
is about input, STA
is about output. It allows your 6502 program to write the value currently held in the accumulator (A) to a specific memory address.
What Does STA Do?
STA
takes the value in the accumulator and writes it to a memory location. That location is specified using one of several addressing modes.
This is essential when you want to:
- Update variables
- Write to memory-mapped hardware (like screen RAM or I/O)
- Store intermediate results for later use
Supported Addressing Modes
Unlike LDA
, STA
does not support the immediate mode — because you can’t store to a literal value.
Here are the valid modes:
Addressing Mode | Example | Opcode | Bytes | Cycles |
---|---|---|---|---|
Zero Page | STA $00 | $85 | 2 | 3 |
Zero Page, X | STA $10,X | $95 | 2 | 4 |
Absolute | STA $1234 | $8D | 3 | 4 |
Absolute, X | STA $1234,X | $9D | 3 | 5 |
Absolute, Y | STA $1234,Y | $99 | 3 | 5 |
Indirect, X | STA ($20,X) | $81 | 2 | 6 |
Indirect, Y | STA ($20),Y | $91 | 2 | 6 |
Flags Affected
None.STA
does not modify any status flags — not even Zero or Negative.
This makes sense: you’re writing data, not interpreting it.
Example Usage
LDA #$41 ; Load ASCII 'A' into accumulator
STA $0400 ; Write it to screen memory at $0400
LDA #$00
STA $D020 ; Set border color on the C64
Or, with indexed addressing:
LDX #$05
LDA #$FF
STA $0200,X ; Store $FF at address $0205
Indirect addressing:
LDA #$3C
STA ($20),Y ; Store A to address pointed to by $20 + Y
Common Pitfalls
- Forgetting that STA doesn’t affect flags. If you plan to branch based on the accumulator value, test it before the STA.
- Using STA #$44 — this is illegal. You can’t store to an immediate value!
- Misusing indirect modes.
STA ($20),Y
looks up a pointer from zero page$20
, adds Y, and stores the value.
Summary
STA
is the simplest way to send data from your CPU into memory — and that includes writing to hardware like video, sound, or I/O chips.
Key takeaways:
- Stores accumulator content into memory
- Supports all basic addressing modes except immediate
- Does not modify any flags
- Essential for output, memory updates, and I/O access
With LDA
and STA
, you have the fundamental load-store mechanism of the 6502 down. Next up, we’ll explore storing other registers: STX
and STY
.