C86 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C8 · Lesson 6 · Memory, addresses and the memory map

Bytes, words and where everything lives

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?

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?

Name it

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 232 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.

AddressWhat's there
0x0000 to 0x0FFFthe program
0x1000 upwarddata (.data)
just below 0x10000the stack, growing down from sp = 0x10000
0x20000LEDS: writing the low 8 bits lights 8 LEDs
0x20004SWITCHES: reading gives 8 switches
0x20008TIMER: counts instructions run
0x2000CTIMECMP: the timer's alarm (lesson 25)
0x20010OUT: 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.

Worked example

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

Show the solution
  1. Element 9's address. addr=0x1000+4×9=0x1024
  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.

Worked example

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

Show the 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.

Worked 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?

Show the solution
  1. The stack's lowest point. 0x10000−0x800=0xF800
  2. The gap. gap=0xF800−0x1A40=0xDDC0=56,768bytes 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

An array of 32-bit words starts at 0x1578. At what address does element 5 start (counting from 0)?

Check

The word 0xFAAB4621 is stored at address 0x1000. Which byte is at address 0x1000?

Check

The byte at an address is 0xAD. What value does lb (load byte, signed) put in a register?

Check

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

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

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

  1. Athe first instruction of the program
  2. Bthe top word of the stack
  3. Cthe switches
  4. Dthe LEDs
  5. Ethe first word of data

Check

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

Check

An array of 8-byte elements starts at address 4096.

(a)

Where is element 3?

(b)

What offset does a load from the base register use?

Check

sh stores the halfword BEEF at address 0x1002.

(a)

Which byte is at 0x1002?

(b)

And at 0x1003?

Explain why

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

Practice

Keep practicing

11 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.