Machine code from zero

A course for the Dragon 32 · Chapters 0 to 15

Chapter 3

The odometer: counting like the computer

About 20 minutes · no new commands

1Why a different way of writing at all?

You have seen $0400 dozens of times by now, always with a translation next to it: 1024. Time to understand it.

We count with ten digits, 0 to 9. The computer likes to count with 16 signs: 0 to 9, and after them A, B, C, D, E, F. The $ in front says: “this number is written in counting by 16”.

Figure 9: the sixteen signs. After 9 we don’t go to 10; we carry on with letters.

Why is this handy? Because everything fits exactly: a number in one house is always two digits ($00 to $FF), a house number is always four digits, and a row on the screen is exactly $20.

2A car’s odometer

Remember the old odometer in a car? Wheels with digits. The right wheel turns, and when it passes 9 it goes back to 0 and moves the wheel next to it on by one.

The computer’s odometer is exactly the same, except that each wheel goes up to F before it goes back to 0.

Figure 10: on top, the computer’s odometer; below, the ordinary one. Same moment, same idea: the right wheel rolls over and moves its neighbour on.

3Turn it yourself

The odometer starts at $0400, the first house of the screen. Under it, the same number in ordinary counting. Press the buttons and watch the wheels.

    Look at the +$20 button: it always takes you one row down on the screen. $20 is 32, and a row has 32 squares.

    4What each wheel is worth

    In ordinary counting, in 254 the digit 2 is worth two hundred, because each place to the left is worth 10 times more. On the computer’s odometer each place to the left is worth 16 times more: 1, 16, 256, 4096.

    Figure 11: translating $0420: each digit times the value of its place, then add them up.

    You don’t need to work this out in your head every time. What matters is to remember a few numbers that come up all the time:

    5The screen map, this time in the computer’s way of writing

    Now the numbers at the side of the screen make sense. Each row starts $20 after the one before. After 8 rows the wheel rolls over, and the 4 becomes 5: halfway down the screen we reach $0500.

    Figure 12: the start of each row. The pattern repeats: 00, 20, 40, 60, 80, A0, C0, E0, and then the next wheel moves on.

    And inside the row? Add the column number, also in the computer’s way of writing. Column 15 is $0F, and column 16 is $10. So the square in row 8, column 16, is $0500 plus $10: $0510.

    From now on, on the screens in the windows, the column numbers along the top are in the computer’s way of writing: 00 to 1F.

    6Programs with houses in the computer’s way of writing

    The same two commands from Chapter 2. Only the houses start speaking the computer’s language. Guess first, then Step.

    7A line is missing

    We wanted A, with B under it. One line is missing. Which one?

    ?Puzzles

    Puzzle 1 · Translator

    Translate into ordinary counting. You can use the odometer above to help.

    $0420 =   $05FF =   $FF =

    And now code puzzles. The houses are given only in the computer’s way of writing.

    ▶On the real Dragon

    In DREAM you can write numbers both ways. STA $0400 and STA 1024 are exactly the same line: a number without $ counts as ordinary.

    And BASIC has its own sign for the computer’s way of writing: &H. Try:

    PRINT &H0400

    And the computer will answer 1024.

    What we saw on our Dragon machine: in BASIC, PRINT &H0400 answered 1024, and PRINT &H05FF,&HFF answered 1535 and 255. In DREAM, the lines LDA #88 and STA 1056 were translated into exactly the same numbers as LDA #$58 and STA $0420, and the X appeared in square $0420. ✓ Checked on our Dragon machine
    What we learned: $ means “counting by 16”. Each wheel goes 0 to F, then rolls over and moves its neighbour on.
    A row on the screen is $20. The screen is $0400 to $05FF, and the middle is $0500.

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