Machine code from zero

A course for the Dragon 32 · Chapters 0 to 15

Chapter 6

Sums: the drawer learns to count

About 25 minutes · INCA, DECA, ADDA, SUBA, CLRA, INC, DEC

1Plus 1: INCA

Until now drawer A only held a number. Now it learns to calculate. The first instruction is the simplest: INCA says “add 1 to what is in drawer A”. No number comes after it; it always adds exactly 1.

Remember that the letters come one after another? A is $41, B is $42, C is $43. So if the drawer holds A and we add 1, it holds B.

Look at the drawer in the picture: a row of lamps has appeared at the bottom. For now it has one lamp, C. Soon we’ll see when it lights up. A question mark on a lamp means “we don’t know yet”.

2Minus 1: DECA

DECA is the opposite: “take 1 away from what is in drawer A”. From Z we go backwards: Y, then X.

3Add any number: ADDA

INCA only adds 1. When you want to add more, there is ADDA (“add to A”). After it you write how much to add, with a hash sign, just like LDA: ADDA #$10 adds $10.

And why $10, of all numbers? Remember from Chapter 4 that in a colour square the left digit is the colour? $10 moves the left digit up by 1 and leaves the lamps where they are. In other words: the same shape, in the next colour.

And what if there is no hash sign? Just like LDA: without # the number is a house. ADDA $0400 goes to house $0400, copies what is written there, and adds it to the drawer.

And there is subtraction too: SUBA (“take away from A”). SUBA #2 takes away 2, and SUBA $0400 takes away what is written in the house.

4The odometer rolls over: lamp C

Remember the odometer from Chapter 3? Drawer A has two wheels, each 0 to F. What happens at $FF plus 1? Both wheels roll over to 0, and the 1 that should move on to a third wheel... has nowhere to go. There is no third wheel in the drawer.

Figure 18: $FF plus 1. The drawer is left with $00, and the 1 that fell out lights up lamp C.

The processor doesn’t forget that 1: it lights up a lamp called C. Every ADDA updates it: did the result go past $FF? The lamp is on. Did it fit? Off. SUBA updates it too: if we went below 0, the odometer turns back to $FF, and C lights up.

Try it yourself. The drawer starts with $FE:

    Did you find something strange? INCA reaches exactly the same $00 as ADDA #1, but doesn’t touch lamp C. That is how the 6809 processor was designed: INCA and DECA are fast “counting” instructions, and they leave C as it was.

    5Back to zero: CLRA, and another test on C

    CLRA (“clear A”) puts 0 in the drawer, and also turns lamp C off. In the next program it turns C off at the start, and then there are two ways to reach $00. Watch the lamp.

    6Sums straight on the screen: INC and DEC

    Without the A at the end, INC and DEC work straight on a house: INC $0500 goes to house $0500, adds 1 to the number written there, and writes it back. Drawer A takes no part at all.

    7A line is missing

    We wanted to write ABC in the middle of the screen, with only one LDA.

    ?Puzzles

    As in the earlier chapters: your screen must match the picture. From now on a line with no name starts with a space, just like in DREAM.

    ▶On the real Dragon

    All the programs in this chapter, and the puzzle solutions too, were typed into DREAM on our Dragon machine and run. We checked square by square that the screen matches the windows here. We also checked the lamps: we added lines at the end of each program that copy the lamps and the drawers onto the screen, and compared them with the window.

    What we learned: INCA plus 1, DECA minus 1, ADDA add, SUBA subtract, CLRA zero. INC and DEC work straight on a house.
    The drawer rolls over like an odometer. ADDA and SUBA light up lamp C when you pass the edge. INCA and DECA don’t touch it.

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