liveBook · Manning

Store instruction

As explained in Classic Game Programming on the NES: Make your own retro video game

A store instruction copies a value from a processor register to a specified memory location.

A store instruction copies the value currently held in a processor register into memory. On the 6502, store commands begin with ST and name the source register: STA stores A, STX stores X, and STY stores Y. The operand identifies the destination memory location. section 3.3.2 section 3.4

Figure 3.15 Storing a register value in a memory location

Read a store as a one-way transfer: the named register supplies the value, and the address selects where that value is written in memory. section 3.3.2

Start with a direct address. Zero-page addresses use the first 256 bytes of memory and are written as an 8-bit offset; absolute addresses name a full 16-bit address anywhere in memory. Load a value into a register first, then store it at the chosen address. section 3.3.2

Store three register values in zero page

Load A with $10 and use STA $06 to place $10 at memory location $06. Likewise, load X with $11 and store it at $07 with STX, then load Y with $12 and store it at $08 with STY. After running the code, inspect memory to verify the values at those locations. section 3.3.2

Indexed addressing calculates the destination from a base address plus X or Y. For example, with X set to $01, STA $01,X stores A at $02; an absolute base can be indexed in the same way. Indirect indexed forms use bytes in zero page as an address before applying an index, allowing a store to reach an address specified through memory. section 3.3.2

Table 3.9 The available addressing modes (view table figure)

Mode

Description

Zero-page memory

The destination location is in zero-page memory using an 8-bit offset.

Absolute memory

The destination location is anywhere in memory using a 16-bit memory address.

Zero-page indexed with X or Y

The destination location in zero-page memory is specified by adding an 8-bit offset address to the value in either the X or Y index registers.

Absolute memory indexed with X or Y

The value is obtained from a location anywhere in memory starting at a specified 16-bit memory address and adding the value in either the X or Y index to determine the final memory address from which to get the value.

Zero-page indirect indexed with Y

The value is obtained from a location anywhere in memory starting at the base address stored in the specified 2 bytes of zero-page memory, and then the value in the Y register is added to that address to get the final address.

Zero-page index indirect with X

The value is obtained from a location anywhere in memory starting at the base zero-page offset, then adding the value in the X register to get the location in zero-page memory where the final address is specified in 2 bytes of memory.

Compare addressing modes to choose whether a destination is a direct zero-page address, a full address, an indexed address, or an address obtained indirectly through zero page. section 3.3.2

Choosing a store form means choosing how the destination is specified. Assemble and run small tests, then inspect the displayed memory range: the changed bytes show exactly where each register value was stored. section 3.3.2 section 3.4

How Classic Game Programming on the NES: Make your own retro video game covers it

How deep it goes
Works through direct, indexed, and indirect destination addressing with runnable memory-inspection examples.
Examples
Demonstrates storing $10, $11, and $12 in memory and storing $ee through direct, indexed, and indirect addresses.
What it assumes
Builds from register values and memory addresses without requiring prior assembly experience.
What you can do after
Practice/perform store operations and verify their destination bytes with the Online Assembler Simulator.
How it connects
Connects registers, memory addresses, zero-page memory, absolute addressing, and X/Y index registers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Game Programming on the NES: Make your own retro video game. The section links point to the parts of the book it draws on.