A course for the Dragon 32 · Chapters 0 to 15
At the end of Chapter 8 you saw a crossroads that goes back. That is the whole secret of this chapter. Think of a race track: the car passes one station after another, and at the end of the lap there is a gate. If the gate is closed, it drives another lap. When it opens, the car leaves on the straight road, to the end.
You write a loop with a name for a place at the start of the track (here LOOP), and a crossroads at its end that jumps back to it. In this window the track is drawn next to the program, and the car moves along with it.
The same loop, only the gate moves: instead of $0520 (the end of row 8), $0600, the house just after the screen. And X starts in the corner, at $0400.
Try the speed slider. At one step a second, the loop’s 1,536 instructions take 25 minutes. On the Dragon: less than eight thousandths of a second. This is the moment you understand why people write in machine code.
Where does that number come from? Inside the processor a clock ticks, about 890,000 ticks a second. Each instruction takes a few ticks: STA ,X+ takes 6, CMPX takes 4, BLO takes 3. One lap = 13 ticks. 512 laps = 6,656 ticks. Under every window there is now a clock that counts them for you.
Sometimes we don’t care how far, but how many times. “Draw 5 blocks.” For that the processor has another small drawer, B. It is just like A: one number, 0 to 255. It has LDB and STB, just like A.
And the new instruction: DECB, “take 1 off B”. When B reaches 0, lamp Z comes on. So BNE (“turn if Z is off”) goes back as long as we haven’t reached zero.
A can be a counter too. Here A is both the digit we write and the counter. Remember Chapter 1? The ordinary digit 0 is $70, and the digit 9 is $79.
We wanted a column of 8 yellow blocks, from top to bottom. B counts 8 laps. What do we need to do to the finger on every lap?
A small track inside a big track. The inner loop (COL) writes 6 squares in a row. The outer loop (ROW) moves to the next row and does it again. LEAX 26,X: after 6 steps to the right, 26 more bring us exactly to the start of the next row, because 6 + 26 = 32.
The Dragon is too fast for the eye. To see something happen, you have to let it wait. How does a computer wait? It counts! A loop that does nothing but count down:
LEAX -1,X takes 1 off X, and lamp Z comes on when X reaches 0. $2000 is 8,192 laps, each lap 8 ticks: about 0.07 of a second.
But wait: X is busy counting now. So who points at the screen? A second finger, Y. It works just like X: LDY, STA ,Y+, CMPY. On the screen it is marked in red.
In this window the speed is already on “Dragon speed”. And if you run it in steps, a ⏩ Skip the wait button appears inside the wait, so you don’t have to press 16,000 times.
In these puzzles you may not write an STA line for every square. Only a loop.
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).