After learning about STA
, the instructions STX
and STY
follow naturally. They allow your program to store the contents of the X and Y index registers into memory.
If you’re managing data structures, looping through arrays, or setting up pointers, you’ll frequently want to move index values into memory — and that’s where these instructions come in.
What Do STX and STY Do?
Both instructions work exactly like STA
, but they store the contents of the X or Y register instead of the accumulator.
They are purely output instructions — they move a value from a register into a given memory location.
Supported Addressing Modes
Instruction | Addressing Mode | Example | Opcode | Bytes | Cycles |
---|---|---|---|---|---|
STX | Zero Page | STX $00 | $86 | 2 | 3 |
Zero Page, Y | STX $10,Y | $96 | 2 | 4 | |
Absolute | STX $1234 | $8E | 3 | 4 | |
STY | Zero Page | STY $00 | $84 | 2 | 3 |
Zero Page, X | STY $10,X | $94 | 2 | 4 | |
Absolute | STY $1234 | $8C | 3 | 4 |
💡 Notice:
STX
supports Zero Page, YSTY
supports Zero Page, X- Neither supports absolute,X/Y or indirect modes
- And like
STA
, no immediate mode
Flags Affected
None.
Just like STA
, these instructions do not modify any status flags.
Example Usage
LDX #$FF
STX $D020 ; Set C64 border color using X register
LDY #$12
STY $C0 ; Store Y into zero page memory
LDX #$33
STX $0200,Y ; Not valid! STX does not support (abs),Y
Indexed zero page example:
LDY #$02
LDX #$A0
STX $10,Y ; Store X at address $12
Common Pitfalls
- Misunderstanding addressing support.
STX
andSTY
are more limited thanSTA
. If you need flexible addressing, move the value into A and useSTA
. - Trying to use STX/STY with absolute,X/Y.
These modes are not valid withSTX
orSTY
.
Summary
STX
and STY
are crucial for saving index values to memory:
- Store the contents of the X or Y register
- Support zero page, indexed zero page, and absolute addressing
- Don’t affect CPU flags
- Great for setting up buffers, counters, and memory-mapped I/O
Combined with STA
, these instructions give you full control over writing all main registers to memory.