BASIC on the Altair

Finding and fixing a mistake

A program can run without an error message and still give the wrong answer. Use temporary PRINT instructions to see what happens inside a loop, find one misplaced line, and check the repair.

View lesson source ↗

Continue your session, or load BASIC

If the terminal below already shows your earlier BASIC session, press the browser's RUN button to continue it. You do not need to load the tape again. This tab keeps the machine, program, terminal, and pending input as you move between BASIC lessons, visit All lessons, or refresh. Returning always pauses execution first. Other tabs work independently; a newly opened or duplicated tab may begin with a copy of this tab's saved session.

Begin a lesson's commands at OK. If your previous program is still asking a question at ?, finish that conversation first; the ticket loop finishes when you answer 0. The lesson's setup explains which stored lines to keep or replace. Typing NEW at OK clears the stored program and variables without reloading BASIC.

If this is a fresh machine, follow the loading steps below.

This is the original 4K BASIC 3.2 interpreter running on our Altair. An interpreter is a program that reads your commands and carries them out. Before it can understand PRINT, the Altair must load that interpreter into memory.

  1. Get the selected paper-tape file from the machine guide (opens in a new tab). Return here and choose it in Paper tape below. The file stays on your computer.
  2. Raise only switches A11 and A10. Leave PC at 0000: do not press EXAMINE. You may then close the front panel to bring the terminal closer.
  3. Press the browser's RUN button. Click Terminal keyboard and answer each question when it appears:
When you see Enter
MEMORY SIZE? Just Enter, to let BASIC find the available memory.
TERMINAL WIDTH? Just Enter, to keep the default.
WANT SIN? Y then Enter. We will not need this math function today.

When 727 BYTES FREE and OK appear, BASIC is ready. Leave the processor running for the lesson. Switching tabs pauses it; press the browser's RUN button when you return.

If you need to start loading again, use Start fresh, raise A11 and A10 again, then RUN. This discards the current BASIC program and uses the tape you already selected. Reset CPU + serial does not reload BASIC.

The saved session stays in this browser tab's temporary storage. It is not sent to the site. Copy your numbered lines somewhere to keep them beyond the browser session. If the browser cannot save or restore a session, a message beside the machine explains that; you can still use the current page.

A computer waiting for your instructions

From paper tape to BASIC.

Resume a saved session with RUN. To load BASIC for the first time, choose the 4K BASIC 3.2 tape (guide opens in a new tab), raise the loading switches, then RUN. Loading instructions

This tab can keep your BASIC session between lessons. Checking for a saved session…

The file stays on your computer. Selecting a verified tape prepares a fresh, stopped machine.

No tape selected.

Front panel · address, data, and sense switches

To load BASIC, raise only A11 and A10 and leave PC at 0000. Do not press EXAMINE after raising the loading switches. You can then close this panel to bring the terminal closer.

Operate the front panel

ALTAIR 8800

Memory and sense switches

Address lights PC · next instruction

0 decimal · 0000 hex

Data lights The byte at that location

0 decimal · 00 hex

A lit lamp is 1; a dark lamp is 0. The digits help you read the lights.

Data / address switches Up = 1 · Down = 0

15–8 · Address + sense

7–0 · Address or data

All switches as an address
0 decimal · 0000 hex
Lower eight as a byte
0 decimal · 00 hex

Setting the switches prepares a number. It does not read or write memory. The CPU can read the upper eight switches at port FF, even while running.

Select the address in the switches and read its byte.

Move to the next address and read its byte.

Write the lower eight switches to the selected address.

Move to the next address, then write the lower eight switches.

EXAMINE would select address 0. DEPOSIT would write 0 to the selected address, 0.

Last memory operation

Enable JavaScript to operate the panel.

EXAMINE and NEXT change PC. Only 0000–0FFF contain RAM; other addresses read FF and discard deposits. Lights show instruction boundaries, not individual bus cycles.

Stopped · 0 instructions

RUN lets the processor execute; STOP pauses it. At BASIC’s prompt, type RUN to run your BASIC program. Break sends Control-C to BASIC.


  
  

0 keyboard bytes queued

Use uppercase commands. Enter sends a line; Backspace sends BASIC’s erase character (_). Paste one line at a time. Only the machine’s echoes appear above.

Inspect the running machine

    

Latest twelve instructions on this page · PC before → after (hexadecimal)


  

Reset keeps RAM and the switches but resets the CPU and serial device. Start fresh discards the program, restores the bootstrap, rewinds the selected tape, and clears the terminal; raise the switches again before RUN.

An answer we can check

Load or resume BASIC using the instructions above, and begin at OK. Even if you continued from the previous lesson, start a new program here. NEW removes the program currently in memory.

This version of the running-total program has a mistake. Enter it exactly as shown so that we can investigate together. Type one numbered line at a time, pressing Enter and waiting for its complete echo before the next:

Type, then Enter

NEW

BASIC replies

OK

Type one line at a time, then Enter

10 FOR N=1 TO 5
15 LET T=0
20 LET T=T+N
30 NEXT N
40 PRINT T
50 END

We want the program to add 1 + 2 + 3 + 4 + 5. Before running it, work out that sum: the answer should be 15.

Type, then Enter

RUN

BASIC replies

 5

OK

BASIC accepts every instruction and finishes normally, but the result is wrong. Finding and fixing a mistake like this is called debugging. We need to compare what the program actually does with what we intended.

Try the program ↑

Look inside the loop

At the moment, we see T only after the loop has finished. We can also print it just after each addition.

For a shorter investigation, change the limit to 3. The intended answer is now 1 + 2 + 3, which is 6. Add two temporary PRINT instructions:

Type one line at a time, then Enter

10 FOR N=1 TO 3
22 PRINT "SO FAR"
24 PRINT T

Lines 22 and 24 sit between the addition on line 20 and NEXT on line 30. Each trip through the loop will print SO FAR followed by the value just stored in T. They let us watch the calculation without changing it.

Predict the three running totals. If the program worked, they would be 1, then 3, then 6. Now look at what it actually prints:

Type, then Enter

RUN

BASIC replies

SO FAR
 1
SO FAR
 2
SO FAR
 3
 3

OK

The last number is the final result from line 40. The earlier numbers, each following SO FAR, show how we reached it.

N being added Intended T after the addition Actual T after the addition
1 1 1
2 3 2
3 6 3

The first addition is right. The second is the first place where the values differ: we wanted the old total of 1 plus the new N of 2, but we got only 2. The earlier total seems to have been lost.

Watch the intermediate values ↑

Find the instruction responsible

Use LIST to inspect the instructions in the order BASIC stores them:

Type, then Enter

LIST

BASIC replies

10 FOR N=1 TO 3
15 LET T=0
20 LET T=T+N
22 PRINT "SO FAR"
24 PRINT T
30 NEXT N
40 PRINT T
50 END
OK

On the first trip, line 15 sets T to zero and line 20 adds 1. So far, that is what we want.

When NEXT starts another trip, execution returns to the instruction after FOR: line 15. That sets T to zero again, throwing away the 1 we just added. Line 20 then adds 2 to zero, giving 2. The same thing happens with 3.

The addition itself is right. The instruction that starts a new total is in the wrong place: it runs on every trip instead of once before the loop.

Move the line and watch again

Put LET T=0 before FOR by entering it as line 5. Then delete line 15 by typing its number alone. Adding line 5 does not remove line 15 for us; we need both edits:

Type one line at a time, then Enter

5 LET T=0
15

Keep the temporary PRINT lines for now. They can show whether our change had the effect we expected:

Type, then Enter

RUN

BASIC replies

SO FAR
 1
SO FAR
 3
SO FAR
 6
 6

OK

The running totals now match our prediction, and the final answer is 6. Line 5 starts T at zero before the loop. Each later trip keeps the previous total and adds the next N.

Repair the program ↑

Remove the temporary output

We added lines 22 and 24 to investigate the mistake. Delete them now that they have done their job, then list the finished program:

Type one line at a time, then Enter

22
24
LIST

BASIC replies

5 LET T=0
10 FOR N=1 TO 3
20 LET T=T+N
30 NEXT N
40 PRINT T
50 END
OK

Type, then Enter

RUN

BASIC replies

 6

OK

Removing those PRINT instructions changes what we see, but does not change the calculation. We still get the sum of 1 + 2 + 3.

Check more than one case

A correct answer for one limit is a useful check. Try a loop with just one trip as well. It should add only 1:

Type one line at a time, then Enter

10 FOR N=1 TO 1
RUN

BASIC replies

 1

OK

Finally, return to the original limit of 5 and check the answer that first revealed the mistake:

Type one line at a time, then Enter

10 FOR N=1 TO 5
RUN

BASIC replies

 15

OK

Type, then Enter

RUN

BASIC replies

 15

OK

Both runs start a fresh total at line 5 and produce 15. We have checked one trip, several trips, and a repeated run. Those checks support the repair, and following the instructions explains why it works.

Check the repaired program ↑

A method to keep

When a program gives a surprising result:

  1. Choose a small example whose answer you can work out yourself.
  2. Print intermediate values and find the first point where they differ from your prediction.
  3. Use LIST to follow the instructions that produced those values.
  4. Make a change you can explain, then run the same example again.
  5. Remove temporary output and check a few other cases.

We did not need a new BASIC command to do this. PRINT and LIST gave us enough information to connect a wrong answer to the instruction that caused it.

You can keep experimenting in the terminal. This tab carries the BASIC machine and program between these lessons, including a visit to All lessons, and restores them paused after a refresh. Press the browser's RUN button to continue. Start fresh discards the program and reloads the retained tape. Copy your numbered lines somewhere for a lasting copy; browser session storage is temporary.

The machine guide (opens in a new tab) explains the hardware, loading process, and recovery controls. These lessons use that same machine and original interpreter.