Machine code from zero

A course for the Dragon 32 · Chapters 0 to 15

Chapter 7

The finger X

About 25 minutes · LDX, ,X, ,X+, LEAX

1A new, bigger drawer

Until now, in every STA we wrote the house number ourselves. Want to draw somewhere else? You change all the lines. There is a better way.

The processor has a drawer called X. It is twice as wide as A: it holds four digits, so the number of a house fits in it ($0400, $05FF...). Think of it as a finger: the number inside it is the house the finger points at.

Figure 19: drawer X holds $0402, so the finger points at house $0402. ,X is that house, and 1,X is the neighbour on its right.

LDX #$0400 says: “put $0400 in drawer X”, in other words: point at house $0400. Just like LDA with a hash sign, only for the big drawer.

2Writing where the finger is: STA ,X

Instead of a house number, you write ,X (a comma, then X). STA ,X says: “write A in the house the finger points at”. On the screen in the window, the finger is drawn above its square.

3Moving the finger: LEAX

LEAX 1,X says: “move the finger one step to the right”. The number before the comma is how far to move: LEAX 32,X moves the finger one row down (32 squares in a row, remember?). And a number with a minus moves backwards: LEAX -1,X left, LEAX -32,X up.

Try it yourself: the finger starts at $0400, and drawer A holds a red block.

    4Write and move: STA ,X+

    Writing a letter and then moving one step is so common that there is a shortcut for it. STA ,X+ says: “write where the finger is, then move it one step to the right”. The plus comes after the X, because the step comes after the writing.

    5Next to the finger: 1,X 32,X

    You can also write next to the finger without moving it. STA 1,X writes in the house to the right of the finger, and STA 32,X in the house below it. The finger stays where it is.

    And here is the magic: the next program draws a big block. Choose another place, and only the first line changes. Everything else stays the same, and the drawing moves.

    6Reading with the finger: LDA ,X

    What works for writing works for reading too. LDA ,X copies the house the finger points at. LDA 1,X copies the neighbour.

    7A line is missing

    We wanted a line of three red blocks.

    8The secret of the fixed ending (part 1)

    From the start, every program has ended with the same four lines. Now we can understand the first one:

    KEY LDX $A000

    KEY is the name of a place (we’ll use it in a moment). LDX $A000 without a hash sign: “go to house $A000 and copy”. Drawer X is wide, so it copies two houses: $A000 and $A001. There, in the Dragon’s fixed memory, the number of a house is written: the house where a small job lives that checks “has a key been pressed?”. The finger now points at it.

    And the second line, JSR ,X: “go and do the job the finger points at, and come back”. We’ll learn a lot more about JSR. We’ll understand the third line in the next chapter.

    ?Puzzles: drawing with a finger

    In these puzzles you may not write a house number after STA. All the writing goes through the finger.

    ▶On the real Dragon

    What we learned: X is a drawer for a house number: a finger. LDX #$0400 points. ,X is the finger’s house, 1,X and 32,X the neighbours.
    STA ,X+ writes and moves. LEAX 32,X moves the finger one row down. Change the LDX, and the whole drawing moves.

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