Machine code from zero

A course for the Dragon 32 · Chapters 0 to 15

Chapter 13

The program lives on the street

About 20 minutes · words turn into numbers

1What does DREAM do when it “assembles”?

In Chapter 0 we said: in memory there are only numbers, from 0 to 255, one in each house. So where is your program? It isn’t written there in words. The processor doesn’t know the word LDA. It only knows numbers.

When you press A (assemble) in DREAM, DREAM turns each line into numbers, and writes them on the street of houses, house after house. On our machine (after CLEAR 200,12000) it starts at house $2EE1.

Figure 28: a little DREAM dictionary. On the left, what you write; on the right, what is written on the street. The word turns into one number (sometimes two), and the number you wrote is written straight after it.

In DREAM you saw this all the time, without noticing: in the listing after assembling, on the left, is the house number, and after it the numbers written there. This window also has a street: the program’s memory, eight houses to a row.

2PC: the finger that walks over the numbers

How does the processor know which command is next? It has one more finger, PC. It points at the house where the next command starts. The processor reads the number there ($86? That’s LDA #!), reads as many numbers as the command needs, does it, and PC moves on to the command after it.

And a loop? BLO LOOP turned into two numbers: $25 (that’s BLO) and $F9. $F9 is -7: “if you need to turn, go back 7 houses” (counting from the house after the BLO). DREAM counted for you how many houses back LOOP is. The name has gone. Only a number is left.

3Change one number, and the program changes

Now comes the most surprising moment in the course. If the program is numbers on the street, you can change a number, without touching the words, and the processor will do something else. In this window you can click a number on the street and type another (two digits, like AF).

    Did you notice task 3? You changed one number, and the word LDA turned into LDB. The processor can’t tell “words” from “numbers you wrote”. It is all the same thing: numbers on the street.

    4How much room does the program take up?

    You count houses. LDA #$BF takes up 2. STA $0510 takes up 3. The fixed ending takes up 8. In DREAM, the last line of the listing shows the free house after the program. Take $2EE1 away from it, and you know how many houses.

    Write lines here, and see what they turn into. (The assembler adds the fixed ending by itself.)

    ?Puzzles: one number only

    In these puzzles you may not touch the words. You change one number on the street, and run.

    ▶On the real Dragon

    What we learned: the program is numbers in memory. LDA # = $86, STA = $B7, RTS = $39. DREAM translates words into numbers, and place names into distances.
    The finger PC points at the next command. Change one number on the street, and the processor does something else.

    A hobbyist tribute project, not affiliated with or endorsed by Dragon Data. The course, drawings and text are our own. Every program in the course was checked on the site's Dragon machine (XRoar running the BASIC we wrote).