Chapter 0 Exercise Set 1: Programming the Altair 8800

Running Your First Program

We will write a program that adds two numbers and stores their sum. Our plan is to have the program load the two input variables from memory locations 0030H and 0040H respectively, add them, and place their sum in memory location 0050H.

We will do this step-by-step, pausing along the way to explain what is happening.

These instructions work on the physical machine as well as our online simulator. See Appendix A to learn how to launch the online simulator.

Let’s go.

Exercise 1: Loading a program to load a variable from memory

In this first exercise we will write a few instructions that will load a value from memory into the Altair’s accumulator.

  1. Turn on the machine by pressing the OFF / ON switch down.

2. When you first turn the machine on, we can’t guarantee its initial state. RESET the machine by pressing the RESET switch up. Confirm that all of the lights on the ADDRESS row are off.

  1. Set the data switches to 00 111 010 and press DESPOSIT (push the DEPOSIT / DEPOSIT NEXT switch up). Confirm that your machine front panel matches the this one:

    Put 3AH in memory location 0
  2. Set the data switches to 00 110 000 and press DEPOSIT NEXT (push DEPOSIT / DEPOSIT NEXT down). Now confirm that the front panel looks like this:

    Put 30H in memory location 1
  3. Set all data switches to 0 (down) and press DEPOSIT NEXT again.

    Put 00H in memory location 2
  4. Deposit 11 000 011 in the next memory location.

    Put C3H in memory location 3
  5. Deposit 00 000 000 into the next two memory locations

6. If you’re on the physical machine, you’ll have to trust for now that you’ve keyed in the instructions correctly. If you’re using the online simulator, you have a convenient way to inspect the internal memory to be sure. Click on the Debug button located in the center above the machine.

Confirm that your Memory Dump looks like this:

Note

Only check the first 4 sets of 2-digit hex numbers. Your memory dump will be filled with random values. That’s how the real Altair machine works - when it is first turned on, the memory is filled with random noise. To make these screenshots easier to read, we’ve clicked the “ZERO ALL MEMORY” button before entering our program. There is not equivalent button on the real machine. The story of why memory is randomized at startup is cool, but it’s beyond the scope of this book.

Memory Dump

Let’s discuss what we’ve done so far. The first instruction, 3A, is the “load direct address”, or LDA instruction (see Data Transfer Instructions). It loads a byte from the memory address that follows it into the accumulator.

The memory address that follows it is a 16-bit value that addresses (indexes) one of the sequential memory bytes, starting at byte 0 and proceeding up to the limit of available RAM, with a maximum index of 65535.

For reasons of hardware efficiency related to the instruction clock that we will not have time to discuss here (though which would make a great class research topic), the two bytes of the 16-bit address are fetched in reverse order, so the second of the two bytes is listed first.

We want our program to fetch the byte at address 0030H, but we list it in our program as 30 00. It is important to understand this, so commit it to memory, both in your brain, and in the computer!

The finial instruction, C3, is the jump, or JMP instruction (see Control Instructions). JMP moves the program counter to a new memory address and proceeds from there. That address is 0000H, so this instruction effectively causes the program to repeat over and over. On the physical machine, you can’t guarantee the initial state of the memory so you’ll need to specifically set those zeros. On the simulator, the values in memory are already all set to 0, so we don’t need to manually enter those two bytes.

All together, we could write our program like this:

LDA 0030
JMP 0000

Exercise 2: Setting a variable in memory

In this second exercise we will set the value at memory location 0030H to the hex value 13H, and then run our program to see if it loads this value into register A (the accumulator).

  1. If your virtual Altair 8800 is not already in the state, put it back in that state by repeating the steps from exercise 1.

  2. Set the 16 address switches to 0 000 000 000 110 000. Take a moment to convince yourself that this is 0030H, then push the EXAMINE / EXAMINE NEXT switch up, in the EXAMINE direction.

  3. Set the data switches to 00 010 011.

    Note

    If you aren’t automatically taking a few seconds by now whenever you see these bit strings to determine in your head what their value is in both hex and decimal, you aren’t serious about learning how to program the Altair!

    Press the DEPOSIT switch. Your front panel should now look like this:

    Set ``0030H`` to ``13H``

    and the cpu and memory dumps should look like this:

    CPU and Address dump
  4. Return to the Sim tab and press the STOP / RUN switch down to run the program. Now take another look at the Debug tab, and you should see this:

    Load ``13H`` from ``0030H`` to register ``A``

    When you run the program, you’ll notice that the lights labelled A1 and A0 on the board start rapidly blinking (on the physical machine the blinking is so fast that it looks like the lights are all the way on) That’s because the program is jumping! It’s executing the instruction at memory address 0, then 1, then 2, then back to 0, indefinitely. To pause the execution, you can flip the STOP / RUN switch up.

    If you see A = 13, the program has successfully executed, loading the value from address 0030H into the accumulator. PC is the program counter, and it might show a different value depending on when you stop it.

    There isn’t a great way to verify correct execution on the physical machine at this stage. Continue on to Exercise 3, where you’ll be able to run and verify on the machine.

Exercise 3: Finishing our add program

Before we complete our add program, we’ll need to set the value of our second addend.

We want to put the value 17H into memory location 0040H.

Before we tell you how, see if you can figure it out on your own before reading on…

Ok, did you conclude that you need to:

  1. Set the address switches to 0 000 000 001 000 000B.

  2. Press the EXAMINE switch.

  3. Set the data switches to 00 010 111B.

  4. Press the DEPOSIT switch.

If you figured this out on your own, take a moment to pat yourself on the back. You’re on your way to success in this course!

If you couldn’t figure it out, it would serve you well to go over each of the steps we’ve done again (and again, and again..), carefully paying attention to each one until you feel confident you understand what it did.

To complete our add program, we still need to do the following:

  1. Move the first number from the accumulator so we can load the second number there.

  2. Load the second number into the accumulator.

  3. Add the two values, storing the result in the accumulator

  4. Write the result to memory address 0050H.

Here is the full program in hex, including what we did in the first two exercises, to finish those steps:

3A 30 00 47 3A 40 00 80 32 50 00 C3 00 00

asdf and here is a link to our simulator with this program already loaded

We’ll learn more about how this maps to instructions later, but for now, here’s a formatted version of the above hex program, with some comments that show the instruction names:

            ;step1 : Load the data from address
            ;        0030 into register B
3A 30 00    ;LDA 0030
47          ;MOV B,A

            ;step2 : Load the data from address
            ;        0040 into the Accumulator
            ;        Then add register B to it
3A 40 00    ;LDA 0040
80          ;ADD B
32 50 00    ;STA 0050

            ;step3 : Jump back to the start
            ;        and do it all again
C3 00 00    ;JMP 0000

Now let’s finish entering the program. We’ll just give you a higher level description of what to do, since you should be able to follow that now.

  1. Examine memory address 0003H, and change its value from 76H to 47H by depositing the latter value (replacing the previous one).

  2. Use DEPOSIT NEXT to set the subsequent values to 3A 40 00 80 32 50 00 C3 00 00.

  3. Run the program and look at the machine state using the Debug tab. If you see:

    Finished adding two numbers machine state

    If you don’t see that, you might need to hit the RESET switch before the RUN switch. RESET just moves the program counter back to memory address 0 - it does not clear the program you’ve entered.

    Now verify that it worked. Use the EXAMINE switch to check and make sure that the addresses 0030H and 0040H are both populated and 0050H is empty (you might need to intentionally zero-out 0050H), then hit RUN. It will loop infinitely until you hit STOP, and then you can inspect 0050H to confirm it’s populated with 2AH. You can zero-out 0050H and re-run as many times as you want.

    Congratulations! You’ve successfully written a machine language program to add two numbers.