6 / 6 · Intro
Memory and storage
With 32-bit addresses a processor can name 4,294,967,296 bytes, exactly 4 GiB. Switch the power off and RAM forgets all of it.
Jump to the 10 practice questionsYou will learn
- Tell memory (RAM) from storage
- Order the memory layers by speed
Before this: What a CPU does
1Explain
16 GB of memory, 512 GB of storage
"My laptop has 16 GB of memory, so it can hold 16 GB of photos." That sentence mixes up two different parts, and the laptop advert itself shows why. It lists two sizes, for example 16 GB of memory and 512 GB of storage. Memory, or RAM, holds the programs and data the CPU is using right now. Storage, a solid-state drive (SSD) or an older hard disk, keeps your files when the power is off. The photos live on the 512 GB side. The 16 GB is the working space where a photo goes while you are editing it.
2Explain
Why RAM forgets when the power goes
Most RAM stores each bit as a tiny charge that leaks away unless it is refreshed many times a second, so everything in it is lost when the power goes. That is what "volatile" means. Storage keeps data without power. An SSD traps charge in flash memory cells that hold it for years, and a hard disk records bits as magnetised spots on spinning platters. Storage is much bigger and cheaper per byte, but much slower to reach, so programs are copied from storage into RAM before they run.
3Problem
Volatile
What does "volatile" mean for RAM?
Hint
Think of a power cut.
Solution
Answer: It loses its contents when the power is off
RAM charges leak away without power and refreshing.
4Problem
Files with the power off
Which keeps your files when the power is off?
Hint
The one that is not volatile.
Solution
Answer: SSD
Flash cells in an SSD hold their charge without power.
5Explore
How the CPU finds one byte among billions
Memory is a long row of cells, each holding one byte, and each cell has a number called its address, starting from 0. To read or write, the CPU puts an address on a set of wires and gets back the byte stored there. The number of address bits sets how much memory can be reached. Four address bits, as in the toy computer, give 2 to the power 4 = 16 cells. Sixteen bits give 65,536. Thirty-two bits give 4,294,967,296 bytes, which is exactly 4 GiB.
Each extra address bit doubles the number of cells that can be reached.
const bits = 4;
console.log(2 ** bits);166Problem
16 address bits
How many memory cells can 16 address bits reach?
cells
Hint
2 to the power 16.
Solution
Answer: 65536 cells
2¹⁶ = 65,536.
7Problem
32 address bits
How many bytes can 32-bit addresses name?
bytes
Hint
2 to the power 32.
Solution
Answer: 4294967296 bytes
In byte-addressed memory, each address pattern names one byte. The 32 address bits give 2³² = 4,294,967,296 patterns, so that many bytes can be addressed.
8Problem
That in GiB
How many GiB is that?
GiB
Hint
One GiB is 2 to the power 30 bytes.
Solution
Answer: 4 GiB
2³² ÷ 2³⁰ = 4.
9Problem
Bits in the toy
The toy computer has 16 cells of 8 bits. How many bits of memory is that in total?
bits
Hint
16 × 8.
Solution
Answer: 128 bits
16 × 8 = 128 bits, or 16 bytes.
10Explore
Number or instruction
A cell has no idea what its bits mean. In the toy computer the 8 bits 00000100 in a data cell are the number 4, but read as an instruction the same bits mean HALT 4. Only the way the CPU reaches a cell decides which, as the next instruction or as data named by an instruction. That follows directly from von Neumann's idea of keeping both in one memory. It is also why a bug that writes to the wrong address can wreck a whole program.
Drag to turn. Scroll or pinch to zoom.
11Problem
Cell 13 after 4 × 3
After the 4 × 3 program halts, what number is in cell 13?
Hint
4 + 4 + 4.
Solution
Answer: 12
It adds 4 three times: 4, 8, 12.
12Problem
00000100 as an instruction
In the toy, a data cell holds 00000100. Read as an instruction, what would it mean?
Hint
The first four bits, 0000, are the opcode.
Solution
Answer: HALT 4
Opcode 0000 is HALT; the address bits are 0100 = 4.
13Explore
The same bits, read two ways
The first line reads the cell as a number, the second as an instruction of the toy computer.
const cell = 0b00000100;
const names = ["HALT", "LOAD", "ADD", "SUB", "STORE", "JUMP", "JZ"];
console.log(cell);
console.log(names[cell >> 4] + " " + (cell & 15));4
HALT 414Explain
Each layer down is bigger and slower
Fast memory is expensive, so computers use layers. Closest to the circuits are the registers, a few dozen values the CPU works on directly, like the accumulator in the toy. Next come caches, small blocks of fast memory on the processor chip itself, usually named L1, L2 and L3, which keep copies of recently used parts of RAM. Then RAM, then the SSD or hard disk, and beyond that the network. Each layer down is bigger and slower. A program runs fast when most of what it needs is already in the top layers.
15Problem
Fastest to slowest
Order from fastest to slowest.
- Registers
- SSD
- Cache
- RAM
Hint
From inside the CPU outwards.
Solution
Answer:
- Registers
- Cache
- RAM
- SSD
Each layer further from the circuits is bigger and slower.
16Problem
Why programs are copied into RAM
Why are programs copied from storage into RAM before they run?
Hint
Compare how fast the CPU reaches each one.
Solution
Answer: RAM is much faster for the CPU to reach
Storage is large and lasting but slow; RAM is the working space.
17Practice
How many address bits?
Finish addressBits(cells): return the smallest number of address bits that can give every cell its own address. 16 cells need 4 bits, 1000 cells need 10.
4
10
16function addressBits(cells) {
let bits = 0;
return bits;
}
console.log(addressBits(16));
console.log(addressBits(1000));
console.log(addressBits(65536));
Hint
Keep adding a bit while 2 ** bits is still smaller than the number of cells.
A solution
function addressBits(cells) {
let bits = 0;
while (2 ** bits < cells) bits++;
return bits;
}
console.log(addressBits(16));
console.log(addressBits(1000));
console.log(addressBits(65536));18Explain
Common mistakes
"More RAM makes the CPU faster."
RAM size changes neither the clock nor the work done per tick. It decides how much can be kept close at hand. If the programs and data you use already fit, more RAM changes little. If they don't fit, the computer keeps going back to much slower storage, and then more RAM helps.
"16 GB of memory means room for 16 GB of files."
Files are kept in storage, the 512 GB in the advert. RAM is the working space for whatever is open right now, and it empties when the power goes.
"If it is on the screen, it is safely inside the computer."
An open document sits in RAM. Until it is saved to storage, a power cut or a flat battery wipes it, because RAM is volatile.
19
Summary
- Memory is numbered cells; n address bits reach 2ⁿ cells (32 bits = 4 GiB).
- The same bits can be an instruction or data; how the CPU reaches them decides.
- RAM is volatile working memory; SSDs and hard disks keep data without power.
- Registers, cache, RAM, storage: each layer down is bigger and slower.