The Program Is Not What Runs
You write words. The processor only ever sees numbers. Something has to stand between them, and the interesting question is when it does its work.
Here is a program:
LET score = 0
LET score = score + 10
SHOW score
You can read it. So can we. The processor cannot.
A processor runs numbers. Counting in Twos showed you that everything in memory is a
number, and that includes the program. Somewhere in the machine, that program is a row of
numbers, and the letters LET and SHOW are nowhere to be found.
So who turned your words into numbers? Something stood in the middle. This module is about that something.
A machine small enough to see
Real processors have hundreds of instructions. To see the idea, we want one with three.
Imagine a machine with a single box in it, starting at 0, and three things it knows how to do:
| Name | Number | What it does |
|---|---|---|
INC |
1 | add one to the box |
DOUBLE |
2 | double the box |
SHOW |
3 | show what is in the box |
The names on the left are for you. The numbers in the middle are what the machine runs. It
has never heard of INC. It knows that when it sees a 1, it adds one.
Here is a program for it:
INC
INC
DOUBLE
SHOW
Add one, add one, double it, show it. The box goes 0, 1, 2, 4, and the machine shows 4.
And here is what the machine runs:
1 1 2 3
Those are the same program. The top one is for reading. The bottom one is for running. Something has to turn the first into the second, and that something is called a translator.
The names are made up
Those three names belong to no real machine. We invented them so that nothing here depends on which computer you end up with.
When you meet a real machine, it will have names of its own for its own instructions, and real numbers behind each one. The idea will be exactly this. Only the table gets bigger.
The one question
Every translator does the same job: names in, numbers out. The difference between them, and it turns out to be the only difference that matters, is when they do it.
Do they translate a line at the moment the machine needs it, and then do the same again the next time that line comes round? Or do they translate the whole program once, before anything runs, and never look at the names again?
The next two units take one answer each. By the end of them you will know why a game written one way keeps up with the screen and a game written the other way cannot.
When it’s wrong, see why
- You typed a name and the machine did nothing. The machine does not read names. If nothing translated your program into numbers first, there was nothing for it to run.
- You gave it a number it does not have. Our tiny machine knows 1, 2 and 3. A 4 means nothing to it, and a real machine given a number it does not know will do something strange rather than stop and ask.
- You expected the letters to be in memory. They are not. What is in memory is the numbers. The letters were for you.
What you’ve learnt
- A program is text for you, and what runs is numbers for the machine.
- Something has to translate between them.
- The names are a convenience. The machine only ever knows the numbers.
- The question that decides everything is when the translating happens.
What’s next
In Unit 2 the translator works as it goes, one line at a time, every time. That is how the BASIC in these machines runs, and it is patient with you for a reason.