Machine code from zero

A course for the Dragon 32 · Chapters 0 to 15

Chapter 2

The first commands: LDA and STA

About 20 minutes · just two commands

1Meet the processor

Inside the Dragon there is one chip that does all the work: the processor, called the 6809. Everything we saw in the earlier chapters, the writing in houses, it does by itself, hundreds of thousands of little actions a second.

The processor has a few small drawers. Today we’ll meet only one: drawer A. The drawer holds one number, just like a house’s board. (Remember the note from Chapter 0? This is it.)

Figure 6: The processor. Today only drawer A is open. We’ll discover the rest when we need them.

2Two commands

LDA says: put a number in drawer A. (LD from Load.) Whatever was in the drawer before is wiped out.

STA says: write what is in drawer A in this house. (ST from Store.) The house is wiped and written, and the drawer doesn’t empty: the same number stays in it.

Figure 7: LDA puts a number in the drawer. STA takes a photo of the drawer and writes it to the house. The drawer still holds $41.
Figure 8: What is in one line.
From today the numbers in the code are written in the computer’s way of writing, with $: $41 is 65, and $0400 is 1024. Everywhere we’ll also write the ordinary number next to them, until we learn this way of writing in Chapter 3.

3A first program, step by step

In each window: guess first, then press Step to see one line happen. The yellow arrow shows which line will happen now.

4What is “the fixed ending”?

At the bottom of every window there is a grey line: “the fixed ending”. These are 4 lines that wait for you to press a key:

  1. KEY LDX $A000
  2. JSR ,X
  3. BEQ KEY
  4. RTS

Why do we need them? When a program ends in DREAM, the editor comes straight back and draws itself on the screen, and you wouldn’t have time to see anything. For now we just copy them to the end of every program, like a signature. In Chapter 10 we’ll understand every word in them.

5A line is missing

In this program we wanted to write OK in the middle of the screen, but one line was deleted. Which line should be in the empty place?

6A broken program: the hash sign was forgotten

Someone wanted an A in the corner, and wrote this:

  1. @ LDA $41
  2. STA $0400
  3. … the fixed ending

The # is missing. Without the hash sign, LDA doesn’t get the number itself. It goes to house number $41 (house 65), and takes a photo of what is written on its board.

What we saw when we ran this in DREAM on our Dragon machine: a dark square with @. House 65 happened to hold 0, and 0 on the screen is an inverted @. On another day there could be a different number there, and then a different square would appear. ✓ Checked in DREAM

This is the most common mistake in the language. If you got something strange instead of what you wanted, the first thing to check: is there a #? Chapter 5 is all about this.

?Puzzles: write it yourself

Write a program in the black window and press Run and check. It is enough to write the LDA and STA lines. We’ll add the fixed ending ourselves.

▶Running it on the Dragon (DREAM)

All the programs in this chapter’s windows, and the solutions to both puzzles too, we typed into DREAM on our Dragon machine, assembled (0000 ERRS) and ran. They do exactly what the windows here show. How to load DREAM: in “DREAM — first lesson”. After N to OLD TEXT ? you type:

  1. @ LDA #$41
  2. STA $0400
  3. KEY LDX $A000
  4. JSR ,X
  5. BEQ KEY
  6. RTS

A line without a name starts with a real SPACE. @ says “the program starts here”. Then BREAK, A, ENTER to assemble, and BREAK, X, ENTER to run. A appears in the corner. Any key takes you back to the editor.

What we learnt: LDA # puts a number in drawer A, and STA writes what is in the drawer in a house. The drawer doesn’t empty.
Without # the computer goes to the house with that number and takes a photo of it. This is the most common mistake.

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