Machine code from zero

A course for the Dragon 32 · Chapters 0 to 15

Chapter 9

A race track: loops

About 25 minutes · New drawer: B

1The road goes back

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.

Figure 22: the loop as a track. On every lap STA ,X+ writes a square and moves the finger. BLO LOOP is the gate: as long as X is lower than $0520, it stays closed.

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.

2The whole screen in five lines

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.

3A new drawer: B, the lap counter

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.

Figure 23: counting down. LDB #5, and after every lap DECB. Only when B reaches 0 does lamp Z come on, and BNE lets you out.

4Counting on the screen

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.

5A line is missing

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?

6A loop inside a loop

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.

7Slower: a waiting loop

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:

  1. LDX #$2000
  2. WAIT LEAX -1,X
  3. BNE WAIT

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.

?Puzzles: with a loop

In these puzzles you may not write an STA line for every square. Only a loop.

▶On the real Dragon

What we learned: a loop = a name for a place, and at the end a crossroads that jumps back to it. The gate is the comparison: CMPX and BLO, or a counter: LDB #5, DECB, BNE.
The Dragon does about 890,000 ticks a second. So the eye can see, we count down in a waiting loop, and leave Y to do the drawing.

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).