A course for the Dragon 32 · Chapters 0 to 15
Until now every program ran line after line, always the same way. In this chapter the program learns to decide. Every decision starts with a question.
CMPA #$41 asks: “what is in drawer A, compared with $41?”. The processor does a subtraction in its head, doesn’t keep the result, and only lights up lamps:
Drawer A itself doesn’t change. CMPA is a SUBA that only looks.
In the window above, a third lamp, N, sometimes came on too. It is simple: it shows lamp 128 of the last result, the leftmost lamp of the eight (Chapter 4). After LDA, lamp N is on exactly when the number is 128 or more. And on our screen that means: a colour square.
CMPA isn’t the only one that touches the lamps. LDA, STA, INCA, DECA and the rest of the sums also update Z and N, according to the number they produce. Lamp C is different: only ADDA, SUBA and CMPA change it (and CLRA turns it off).
Now the decision. BEQ is a crossroads: “if lamp Z is on, turn”. After it you write the name of a place in the program, where to turn to. If the lamp is off, you carry straight on to the next line. BNE is the opposite: you turn when the lamp is off (not equal).
You write the name of a place right at the start of the line, before the instruction, like the @ that was always there. And there is one name that already exists in every program: KEY, the fixed ending. BEQ KEY says “if equal, go to the end”.
Try the crossroads. Choose what is in the drawer and what we ask, and see where each kind of crossroads sends you:
In the last example it was “if not Y, draw red”. What if we want two ways: Y blue, otherwise red? We also need an instruction that says “skip the second way”. That is BRA: a crossroads that always turns, without asking.
Without the BRA, after the blue the program would carry straight on to the line NO and draw red as well, on the same square. Work it out in your head: what would be left?
Lamp C says “A is smaller”. So there are crossroads for it too: BLO (“turn if lower”) turns when C is on, and BHS (“turn if higher or same”) turns when C is off.
Letters are numbers, and they are in alphabetical order. So “smaller than M” means “before M”. The next program puts a letter on the left side of the screen if it comes before M, and on the right side if not:
A very useful question in games: “is there something here?”. An empty square is $60 (Chapter 5).
The lamps don’t keep the answer for long. Every instruction that updates them wipes out what was there. STA, for example, turns Z on or off according to what it wrote. That is why you put the crossroads right after the question.
Now we can read the whole fixed ending:
The Dragon’s small job checks the keyboard. When nothing has been pressed, it comes back with lamp Z on. And then BEQ KEY: if Z is on, turn to KEY, in other words go back and ask again. And again. And again. Hundreds of times a second, until a key is pressed. Then Z is off, we carry on to RTS, and go back to DREAM.
A crossroads that sends you back: that is a loop. You’ve seen it in every program since Chapter 2.
If you can go back to KEY, you can go back to any place. This program uses CMPX: the same comparison, only for the finger. “Has the finger reached $0520 yet?” If not, go back and write another block.
Five lines, and a whole row of blocks. Press Fast ⚡ and see. That is what all of Chapter 9 is about.
Here each puzzle is checked in several cases. The locked lines put something in the corner, and in each case something different. Your program has to work correctly in all of them.
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).