How does memory addressing work
In the last lesson we said that gathering lots of memory cells gives you memory. But out of all those cells, how do you go straight to the one cell you want?
Too many cells to find one
There are billions of memory cells.
So how do you find which cell holds "that value I saved earlier" among so many?
They have no names, and they all look exactly alike.
Every cell looks the same, so there's no way to pick out just one.
Picture a library with tens of thousands of books shoved onto shelves with no numbers.
Finding the book you want would be nearly impossible.
Memory has the same problem, so it needs a solution.
Give every cell a number
The solution works just like lockers at a swimming pool.
Each identical locker has a number: 0, 1, 2, and so on.
Memory numbers every cell in order the same way.
Top: the cell number (address). Bottom: the value stored in that cell.
This number is called an address.
Just as a home address lets mail arrive at the right door, an address on every cell lets the computer go to exactly the cell it wants.
Call an address and go to that cell
With addresses, finding something is instant.
Say "get the value in cell 3," and memory goes straight to cell 3 and takes out the value inside.
Even with billions of cells, knowing the number gets you there in one step.
Enter an address below and press read, and that cell's value appears.
Enter an address and go to that cell.
Going straight to a cell with a single number is the secret to why memory is fast.
It doesn't count from the start; it points directly at the address.
Put a value into the cell you want
It's not just for taking things out.
Say "put this value in cell 5," and the value is stored in that cell.
Reading means taking out by address; writing means putting in by address.
Those two actions are everything memory does.
Choose an address and put a value in.
Read by address, write by address. Simple.
Yet these two simple actions are the foundation of every program.
Variables, photos, anything at all: in the end, everything goes in and out by address like this.
The CPU asks by address
Now for the big picture.
When the CPU, which does the calculating, needs a value, it asks memory by address.
Something like: "Give me the value at 3, and put this result in 7."
The CPU and memory get the work done by passing addresses back and forth.
Passing things back and forth by address.
With that, we've made one full lap around the foundations of a computer's body.
Starting from 0 and 1, we've come all the way to calculating, remembering, and finding things by address.
From the next lesson on, we'll look at how these parts follow real instructions.