Sunday, March 4, 2012

Apple 1 BASIC

The Replica 1 includes in ROM the BASIC interpreter written by Woz for the Apple 1. It was originally loaded from cassette tape (taking several minutes to load) and was 4K in size. It was quite impressive that Woz, a hardware engineer, single handedly wrote this in machine code, assembled it by hand and entered into the monitor a byte at a time to debug it. Apparently he was not even familiar with BASIC until he started developing the interpreter. But he knew that a simple programming language like BASIC was needed to make the Apple 1 accessible to beginning programmers.

While a pretty faithful implementation of BASIC, like all flavours of BASIC from that era, it has it's limitations and quirks.

Apple provided a manual for BASIC which can be found on the Internet and is on the Replica 1 CD-ROM:
Preliminary Apple BASIC Users Manual October 1976
That images was the Apple company logo in those days. This preliminary manual is the only version ever released. I get a kick out of the disclaimer on the second page:
Second page of BASIC User's Manual

Did they intentionally misspell the word "likely" ? It welcomes the reader to write to Apple to report errors or suggest improvements. I might just do that. I'm not sure the mailing address is still correct.

Limitations

The main limitation was the support for integer only math with no floating point (i.e. decimals) and numbers had to fit in the range of -32767 to +32767.

Variable names are limited to a letter or a letter and a digit (most early BASICs allowed variables to have longer names but only the first two characters were significant). String variables had to have their maximum size declared in a DIM statement. Arrays had to have their size declared using DIM. Arrays of strings was not supported, only numeric arrays. There was no DATA statement.

Due to limitations of the Apple 1 video hardware, there was no way to position the cursor anywhere on the screen except the current line -- it just supported a "dumb terminal" interface. Only upper case characters were supported (a hardware limitation even the early Apple II computers had).

BASIC would not automatically use all available memory. If you had more than 4K you needed to set the memory bounds with LOMEM and HIMEM commands.

To it's credit:

  • it was quite fast
  • it provided good string manipulation support (better than most BASICs of that era)
  • if necessary you could call machine language code with the CALL command

Quirks and Unique Features

This implementation of BASIC had a few quirks and unique features.

Array indexing started a 1, whereas most BASICS use 0. Many keywords could be executed as immediate mode commands outside of a program, include some commands that other BASICS didn't support this for, such as GOTO and DIM.

With 8-bit signed numbers, the smallest number that can be represented is -32768. For some reason BASIC only allowed numbers down to -32767, maybe to be consistent with the largest number 32767 and so that a common error message "*** >32767 ERR" could be used.

Program execution would stop if a key was pressed (any key, unlike Control-C in most Microsoft BASICs). Thus, it was easy to inadvertently hit a key when a program was running and have it stop. You could continue by typing GOTO and the line number where it reported execution had stopped.

The documentation says that if you don't declare a string's maximum length using a DIM statement it is assigned a size of zero, but it actually seems to defaults to a length of one. There was no CHR$() or ASC() functions to convert between numbers and characters, something commonly needed.

It supported "#" as a synonym for the "<>" (not equal) operator in comparisons. It didn't support "><" as a synonym for "<>" as some other BASIC did. When comparing strings in an IF statement it only accepts "#" and not "<>".

The AUTO command to prompt with line numbers seems to me like it would be rarely used in practice.

INPUT statements always added a "?" prompt.

The target of a GOTO can be a variable (e.g. X=100 : GOTO X). Most BASICs do not allow this although the ON GOTO feature is similar. You can do some interesting tricks with this but it can also be easy to introduce bugs in your code and make it hard to understand. It also makes writing a program that renumbers a BASIC program likely to fail.

Given it's limitations, even larger BASIC programs like Star Trek were ported to run on it. Apparently at least one early Apple 1 user developed and ran an accounting system for his business on it.

Woz later enhanced BASIC into what would become Integer BASIC for the Apple II. Woz never seemed to be able to get around to overcoming the limitation of integer math, and Apple contracted to have Microsoft write a version of their BASIC for what would become Applesoft BASIC, which had floating point support. Eventually Applesoft BASIC became the default on Apple II computers but Integer BASIC would would still be available for programs that needed it. Integer BASIC had the main advantage of being significantly faster and smaller than Applesoft BASIC.

In the next blog posting I'll discuss some other implementations of BASIC that you can run on the Replica 1.

No comments: