---
title: Memory, addresses and the memory map
heading: Bytes, words and where everything lives
eyebrow: C8 · Lesson 6 · Memory, addresses and the memory map
xp: 50
---

::: hook
To a processor, memory is one long row of bytes, each with a number: its address. Instructions, variables, arrays, the stack and even the switches and lights on the board all live somewhere in that row, and the processor reaches every one of them the same way, with a load or a store at an address. Where each thing lives is the memory map. Here a program adds up an array of four numbers stored in data memory and writes the total after them.

::: play
- Step through the program and watch the memory window: each row is one 4-byte word.
- Each `lw` reads a word at t0 plus an offset. Why do the offsets go up by 4?
- The last two loads read the single byte at 0x1004, which is part of the word holding −5. Why do they give different answers?
:::
:::

```sim cpu
.data
nums: .word 12, -5, 30, 7
total: .word 0
.text
    la t0, nums         # t0 = the address of nums[0]
    lw t1, 0(t0)        # nums[0]
    lw t2, 4(t0)        # nums[1], 4 bytes further on
    add a0, t1, t2
    lw t2, 8(t0)        # nums[2]
    add a0, a0, t2
    lw t2, 12(t0)       # nums[3]
    add a0, a0, t2
    sw a0, 16(t0)       # total = the sum
    lbu a1, 4(t0)       # the byte at 0x1004, as unsigned
    lb a2, 4(t0)        # the same byte, as signed
    ebreak
show: a0, a1, a2, t1, t2
hex: t0
memory: 0x1000..0x1014
```

::: predict
## −5 is stored as the word 0xFFFFFFFB at address 0x1004. What does `lbu a1, 4(t0)` (load the one byte at 0x1004, unsigned) put in a1?

- [ ] 255 (0xFF)
- [x] 251 (0xFB)
- [ ] 4294967291 (0xFFFFFFFB)
- [ ] −5

::: reveal
251. RISC-V is little-endian: a word's least significant byte sits at its lowest address, so the byte at 0x1004 is 0xFB. `lbu` fills the rest of the register with 0s, giving 251. `lb` copies the byte's top bit into the rest instead (sign extension), giving 0xFFFFFFFB, which is −5.
:::
:::

::: nameit
## Addresses, words and the map

**Byte addressing.** Every byte has its own address. A word is 4 bytes, and words sit at addresses that are multiples of 4. With 32-bit addresses a processor can reach $2^{32}$ bytes, 4 GiB.

**Loads and stores.** `lw`/`sw` move a word, `lh`/`sh` a 2-byte halfword, `lb`/`sb` a byte. `lb` and `lh` sign-extend; `lbu` and `lhu` fill with 0s. The address is a base register plus a 12-bit signed offset: `lw t2, 8(t0)` reads the word at t0 + 8.

**Byte order.** Little-endian: the word 0x12345678 at 0x1000 is stored as 78, 56, 34, 12 at 0x1000 to 0x1003. Some processors and network protocols are big-endian, storing the most significant byte first, so data crossing between them has to be swapped.

**Arrays.** Element i of an array of words starts at base + 4i. A compiler computes it with a shift: `slli t1, t1, 2` makes 4i, then an add.

**Labels and data.** In assembly, `.data` starts the data section, `.word 12, -5` reserves words with those values, and a label like `nums:` names the address. `la t0, nums` puts that address in t0.

**This board's memory map.**

| Address | What's there |
|---|---|
| 0x0000 to 0x0FFF | the program |
| 0x1000 upward | data (`.data`) |
| just below 0x10000 | the stack, growing down from sp = 0x10000 |
| 0x20000 | LEDS: writing the low 8 bits lights 8 LEDs |
| 0x20004 | SWITCHES: reading gives 8 switches |
| 0x20008 | TIMER: counts instructions run |
| 0x2000C | TIMECMP: the timer's alarm (lesson 25) |
| 0x20010 | OUT: each word written is printed |

Real microcontrollers are laid out the same way, with larger numbers. On ARM Cortex-M chips, for example, SRAM starts at 0x2000_0000 and the peripherals at 0x4000_0000.
:::


::: example
## An `int` array starts at 0x1000, and t0 holds that address. Where is element 9, and how is it loaded?

::: solution
1. **Element 9's address.**
   $$\begin{aligned} \text{addr} &= \text{0x1000} + 4 \times 9 \\ &= \text{0x1024} \end{aligned}$$
2. **Load it.** That's 36 bytes past the base: `lw t1, 36(t0)`.

**Answer:** element 9 is at 0x1024, and `lw t1, 36(t0)` loads it.
:::
:::

::: example
## The word 0x0A0B0C0D is stored at 0x1000. What does `lbu` from 0x1001 give?

::: solution
1. **Lay out the bytes.** Little-endian order puts 0x0D at 0x1000, 0x0C at 0x1001, 0x0B at 0x1002 and 0x0A at 0x1003.
2. **Read the byte.** `lbu` from 0x1001 gives 0x0C.

**Answer:** 0x0C, which is 12.
:::
:::

::: example
## A program's `.data` and `.bss` end at 0x1A40. The stack grows down from 0x10000, and its deepest use is 2 KiB. How much room is left between them for a heap?

::: solution
1. **The stack's lowest point.**
   $$\text{0x10000} - \text{0x800} = \text{0xF800}$$
2. **The gap.**
   $$\begin{aligned} \text{gap} &= \text{0xF800} - \text{0x1A40} \\ &= \text{0xDDC0} \\ &= 56{,}768\ \text{bytes} \end{aligned}$$
   about 55 KiB.
3. **Who shares it.** A heap that grows up from 0x1A40 and a stack that grows down from the top share that space, and nothing stops them from meeting.
4. **The danger.** On a small board, a deep recursion or a large local array silently overwrites the heap.

**Answer:** 0xDDC0 bytes, which is 56,768, about 55 KiB.
:::
:::

::: check
given: p = 1024..2000 int; i = 1..40 int; let B = 4*p
answer: B + 4*i
base: 16
width: 4

## An array of 32-bit words starts at 0x{B:hex4}. At what address does element {i} start (counting from 0)?

::: right
Right: 0x{answer}. Element i is 4i bytes past the start.
:::

::: misses
- [B + i] Each word is 4 bytes, so element {i} is 4 × {i} bytes on.
- [B + 4*(i - 1)] Element 0 is at the start, so element {i} is {i} words on.
- [any] Not yet. Base + 4 × i, in hex.
:::
:::

::: check
given: b0 = 16..63 int; b1 = 64..127 int; b2 = 128..191 int; b3 = 192..255 int; k = 0..3 int; let w = b3*16777216 + b2*65536 + b1*256 + b0
answer: floor(w/256^k) - 256*floor(w/256^(k + 1))
base: 16
width: 2

## The word 0x{w:hex8} is stored at address 0x1000. Which byte is at address 0x100{k}?

::: right
Right: 0x{answer}. Little-endian puts the least significant byte at the lowest address.
:::

::: misses
- [floor(w/256^(3 - k)) - 256*floor(w/256^(4 - k))] That's big-endian order. RISC-V stores the least significant byte first.
- [any] Not yet. 0x1000 holds the lowest two hex digits, 0x1001 the next two, and so on.
:::
:::

::: check
given: b = 128..255 int
answer: b - 256
tolerance: 0

## The byte at an address is 0x{b:hex2}. What value does `lb` (load byte, signed) put in a register?

::: right
Right: {answer}. The top bit is 1, so the byte is negative in two's complement, and `lb` extends that sign through the whole register.
:::

::: misses
- [b] That's what `lbu` gives. `lb` treats the byte as signed.
- [any] Not yet. A byte with its top bit set is its unsigned value minus 256.
:::
:::

::: check
kind: asm
given: p = 1..999 int; q = 1000..1999 int
expect: mem[0x1000] = q; mem[0x1004] = p

## Swap the two words in data memory, so that a holds what b held and b holds what a held.

```asm starter
.data
a:  .word p
b:  .word q
.text
    # swap the words at a and b
    ebreak
```

```asm solution
.data
a:  .word p
b:  .word q
.text
    la t0, a
    lw t1, 0(t0)        # a
    lw t2, 4(t0)        # b
    sw t2, 0(t0)
    sw t1, 4(t0)
    ebreak
```

::: right
Right. Both words have to be in registers before either is overwritten.
:::
:::

::: check
kind: match

## Match each address on this board to what's there.

- 0x0000 :: the first instruction of the program
- 0x1000 :: the first word of data
- 0xFFFC :: the top word of the stack
- 0x20000 :: the LEDs
- 0x20004 :: the switches

::: right
Right. Program, data, stack and devices each get their own range of the one address space.
:::
:::

::: check
given: b = 16 | 20 | 24 | 30
answer: 2^(b - 10)
unit: KiB
tolerance: 0

## A processor has {b}-bit addresses. How much memory can it address, in KiB (1 KiB = 1024 bytes)?

::: right
Right: $2^{\given{b}}$ bytes, which is {answer}.
:::

::: misses
- [2^b] That's in bytes. Divide by 1024 for KiB.
- [any] Not yet. $2^b$ bytes, divided by $2^{10}$.
:::
:::

::: check
kind: parts
given: b = 4096 | 8192; i = 3 | 9 | 25; s = 2 | 4 | 8

## An array of {s}-byte elements starts at address {b}.

::: part
answer: b + s*i
tolerance: 0

### Where is element {i}?

::: misses
- [b + i] Multiply the index by the element size.
- [any] base + size × index.
:::
:::

::: part
answer: s*i
tolerance: 0

### What offset does a load from the base register use?

::: misses
- [any] size × index.
:::
:::
:::

::: check
kind: parts
given: h = 4660 | 48879 | 2748

## `sh` stores the halfword {h:hex4} at address 0x1002.

::: part
answer: h - 256*floor(h/256)
tolerance: 0
base: 16
width: 2

### Which byte is at 0x1002?

::: misses
- [floor(h/256)] Little-endian puts the low byte at the lower address.
- [any] The low byte.
:::
:::

::: part
answer: floor(h/256)
tolerance: 0
base: 16
width: 2

### And at 0x1003?

::: misses
- [any] The high byte.
:::
:::
:::


::: explain
## Why is element i of an array of words at base + 4i, and what goes wrong if a program uses base + i?

::: model
Memory is addressed in bytes, and each word takes up 4 of them, so element 0 occupies the 4 bytes at base, element 1 the next 4, and element i starts 4i bytes in. If a program used base + i, element 1's address would point at the second byte of element 0, so the load would read a word made of three bytes of one element and one of the next, giving garbage (and on many processors an alignment fault, since the address isn't a multiple of 4).
:::
:::

::: practice

::: check
given: p = 1024..4000 int; i = 1..60 int; let B = 4*p
answer: B + 4*i
base: 16
width: 4

## Word array at 0x{B:hex4}. Address of element {i}?

::: misses
- [B + i] 4 bytes per word.
- [any] Base + 4i.
:::
:::

::: check
given: b0 = 16..63 int; b1 = 64..127 int; b2 = 128..191 int; b3 = 192..255 int; k = 0..3 int; let w = b3*16777216 + b2*65536 + b1*256 + b0
answer: floor(w/256^k) - 256*floor(w/256^(k + 1))
base: 16
width: 2

## 0x{w:hex8} at 0x2000, little-endian. Byte at 0x200{k}?

::: misses
- [any] Lowest address, least significant byte.
:::
:::

::: check
given: b = 128..255 int
answer: b - 256
tolerance: 0

## `lb` reads the byte 0x{b:hex2}. Result?

::: misses
- [any] Subtract 256.
:::
:::

::: check
kind: asm
given: n = 1..2000 int; k = 1..15 int
expect: mem[4096 + 4*k] = n

## Store {n} into word {k} (counting from 0) of the array that starts at 0x1000.

```asm starter
    ebreak
```

```asm solution
    li t0, 0x1000
    li t2, k
    slli t2, t2, 2
    add t0, t0, t2
    li t1, n
    sw t1, 0(t0)
    ebreak
```

::: misses
- [any] The address is 0x1000 + 4 × {k}.
:::
:::
:::
