DYSTOPIANPIXEL

Memory and addresses

Tell an address from its contents and see how the processor finds bytes in memory.

Next Asset Lab concept artwork showing sprite patterns, palette colours and animation frames
Next Asset Lab concept artwork · Begin with Odin Dev.2k and the foundations of Z80 assembly.
TUTORIAL 1 / PART 05

Memory and addresses

Think of memory as numbered storage locations

A simple beginner model is to imagine memory as a very long row of small boxes. Each box stores one byte. Every box has a number so that the CPU can find it. That number is the address.

AddressContents
$6000$3E
$6001$42
$6002$21
$6003$00
$6004$60
$6005$C9

The address and the contents are different things. $6000 tells us where a byte is stored. $3E tells us what byte is stored there.

Address, contents and pointer are different ideas

This distinction is one of the most important foundations in assembly language. An address tells us where something is. The contents are the value stored at that address. A pointer is a value — usually held in a register or memory — that contains an address.

  1. HL contains $6000
  2. HL is being used as a pointer
  3. $6000 is an address
  4. memory at $6000 contains $3E

So the sentence 'HL contains $6000' does not mean that HL contains the byte stored at $6000. It means HL contains the address $6000. If we then use HL as a pointer, the CPU can access the memory located at that address.

Why addresses are normally hexadecimal

A Z80 address is 16 bits, so four hexadecimal digits represent it exactly. This is far easier to read than sixteen binary digits.

$6000
%0110000000000000

Both represent the same numerical address. The hexadecimal form is shorter and easier to recognise.

The Z80's 64K visible address space

A normal Z80 memory address is 16 bits wide. Sixteen bits give 65,536 possible addresses, from $0000 through $FFFF. If each address refers to one byte, that gives 65,536 byte locations — commonly called a 64K address space, or more precisely 64 KiB.

first address: $0000
last address:  $FFFF

65,536 addressable byte locations

The ZX Spectrum Next contains much more physical memory than this. Later we will learn how the Next's MMU maps different physical memory pages into the Z80's 64K view. For Tutorial 1, it is enough to understand the CPU's visible 16-bit address space.

Files on storage are not the same thing as bytes in RAM

Our source file lives on storage. When Odin assembles it, the resulting machine-code bytes are placed in RAM at addresses such as $6000. The CPU executes the bytes in memory; it does not execute the text in tut01.odn or tut01.asm directly.

  1. source file on storage
  2. Odin reads and assembles it
  3. machine-code bytes in RAM
  4. Z80 executes the RAM bytes

A byte does not know what it means

Memory only stores bit patterns. A byte does not carry a label saying 'I am a number', 'I am text' or 'I am an instruction'. Meaning comes from how the program uses that byte.

$41 = %01000001

Depending on context it could be:
  • the unsigned number 65
  • a character code
  • a colour or palette value
  • part of an address
  • an instruction byte
  • ordinary application data

This idea becomes especially important in Next Asset Lab because sprite pixels, palette entries, file headers, coordinates and executable instructions are all ultimately represented by bytes.

Program bytes and data bytes can share the same memory

The CPU interprets bytes as instructions when PC points at them and execution reaches them. Other bytes may simply be data that an instruction reads or writes. Assembly programming therefore requires us to keep track of which addresses contain code and which contain data.