BASIC interpreter
A BASIC interpreter enables users to enter and run BASIC programs and was, for the first part of the microcomputer era, the default application that computers would launch. Users were expected to use the BASIC interpreter to enter in programs (often from printed listings) or to load programs from storage (often cassette tapes).
BASIC interpreters are of historical importance. Microsoft’s first product for sale was a BASIC interpreter (Altair BASIC), which paved the way for the company’s success. Before Altair BASIC, microcomputers were sold as kits that needed to be programmed in machine code (for instance, the Apple I); after Altair, microcomputers were expected to ship with BASIC interpreters of their own (e.g., the Apple II, which had multiple implementations of BASIC). A backlash against the price of Microsoft’s Altair BASIC also led to early collaborative software development for Tiny BASIC implementations in general and Palo Alto Tiny BASIC specifically.
History
In January 1975 the Altair 8800 was announced and sparked off the microcomputer revolution. In March, Steve Wozniak attended the first meeting of the Homebrew Computer Club and began formulating the design of his own computer. One of the most important pieces of software for the Altair, and one of the most heavily pirated, was Altair BASIC from the recently formed Microsoft.[1] Wozniak concluded that his machine would have to have a BASIC of its own. When the Apple II shipped in the summer of 1977, Integer BASIC was supplied in ROM, while Applesoft BASIC shipped on cassette. [2]
Description
Language design
Wozniak's references for BASIC were a copy of 101 BASIC Computer Games and an HP BASIC manual.[3] He did not know that HP's BASIC was very different from the DEC BASIC variety used in 101 Games, which was also the basis of Microsoft BASIC for the Altair. Based on these sources, Wozniak began sketching out a syntax chart for the language.[3]
Coding
Wozniak wrote the code by hand, translating the assembler code instructions into their machine code equivalents and then uploading the result to his computer.[4] He wrote a machine code monitor, "mini-assembler", and disassembler to create and debug assembly language programs. Wozniak hand-assembled the monitor as the Apple II's first program, then used it to write Integer BASIC.[5][6][7]
Without any training on how to write a computer language, he used his HP calculator experience to implement a stack machine to interpret expressions. [3]
Program editing
Most BASIC implementations of the era acted as both the language interpreter as well as the line editing environment. When BASIC was running, a >
command prompt was displayed where the user could enter statements.[8][a]
Statements that were entered with leading numbers are entered into the program storage for "deferred execution",[9] either as new lines or replacing any that might have had the same number previously.[10] Statements that were entered without a line number were referred to as commands, and ran immediately.[b] Line numbers could typically be from 1 to 32767[11][c] and lines could contain up to 128 characters.[13]
Some implementations, such as Integer BASIC, also included the AUTO
command to automatically enter line numbers at a given starting number like AUTO 100
, adding 10 to the last number with every new line. AUTO 300,5
would begin numbering at line 300 by fives; 300, 305, etc. Automatic numbering was turned off by entering MAN
.[14]
Debugging
As in most BASICs, programs were started with the RUN
command, and as was common, could be directed at a particular line number like RUN 300
.[15] Execution could be stopped at any time using Ctrl+C[16] and then restarted with CONT
inue (CON
in Integer BASIC.[17]
For step-by-step execution, the TRACE
instruction could be used at the command prompt or placed within the program itself. When it was turned on, line numbers were printed out for each line the program visited. The feature could be turned off again with NOTRACE
.[18]
Variable names
Dartmouth BASIC and HP-BASIC limited variable names to at most two characters (either a single letter or a letter followed by one digit). MS-BASIC allowed a letter followed by an optional letter or digit but ignored subsequent characters. Integer BASIC was unusual in supporting any length variable name (e.g., SUM, GAMEPOINTS, PLAYER2), provided it did not contain a reserved word. [19]
Symbol table
Only single-dimension arrays were allowed, limited in size only by the available memory.[20] Some interpreters, such as Integer BASIC, copied HP-BASIC and defined string variables as arrays of characters that had to be DIM
ensioned prior to use.
Mathematics
Integer BASIC, as its name implies, uses integers as the basis for its math package. These were stored internally as a 16-bit number, little-endian (as is the 6502). This allowed a maximum value for any calculation between -32767 and 32767; although the format could also store the value -32768, BASIC could not display that number. Calculations that resulted in values outside that range produced an error.[21]
Many interpreters supported mathematics using integers only, lacking floating-point support. Using integers allowed numbers to be stored in a much more compact 16-bit format that could be more rapidly read and processed than the 32- or 40-bit floating-point formats found in most BASICs of the era. However, this limited its applicability as a general-purpose language.[d]
Infix operators included +
(addition), -
(subraction), *
(multiplication), /
(division), MOD (remainder) and exponent using the ^
character. Binary operators included AND
, OR
and NOT
. Binary comparisons included the standard set of =
, >
, <
, >=
, <=
, <>
and the HP-inspired #
, which was equivalent to <>.[23]
Mathematical functions were sparse; only ABS
(absolute value), SGN
(sign) and RND
(random number) were supported.[24] In contrast to MS-derived versions, where the parameter was ignored and RND
always returned a value 0..<1, Integer BASIC used the parameter; RND(6)
returned an integer from 0 to 5.[25]
Strings
Another difference with other BASICs of the era is that Integer BASIC treated strings as arrays of characters, similar to the system in C or Fortran 77. Substrings were accessed using array slicing rather than string functions. This style was introduced in HP Time-Shared BASIC, and could also be found in other contemporary BASICs patterned on HP, like North Star BASIC and Atari BASIC. It contrasted with the style found in BASICs derived from DEC, including Microsoft BASIC, where strings are an intrinsic variable-length type.[26] Before MS-derived BASICs became the de facto standard, this style was not uncommon; North Star BASIC[27] and Atari BASIC[28] used the same concept, as did others.
Strings in Integer Basic used a fixed amount of memory regardless of the number of characters used within them, up to a maximum of 255 characters.[29] This had the advantage of avoiding the need for the garbage collection of the heap that was notoriously slow in MS BASIC[e] but meant that strings that were shorter than the declared length was wasted.
Substring access was provided through array slicing syntax. For instance, PRINT A$(0,5)
printed the first six characters of A$, characters 0 through 5.[31][31][f] Concatenation was provided using the same system, A$(5)="ABC"
replaced any characters starting at position 5 with the string "ABC".[32] This contrasts with the DEC/MS-style string handling which uses string functions like MID$
to access substrings and +
for concatenation.[33][g]
As many of the features that would be provided by string functions were instead provided by array slicing, the selection of string functions was reduced. LEN$
returned the length of a string[29] and ASC
returned the ASCII numeric code for the first letter in a string.[34] It lacked an equivalent of the CHR$
that returned the ASCII character with a given numeric code.[35]
Graphics and sound
Most BASIC interpreters differed widely in graphics and sound, which varied dramatically from microcomputer to microcomputer. For instance, the Apple II supported a game controller, a paddle controller, which had two controllers on a single connector. The position of the controller could be read using the PDL
function, passing in the controller number, 0 or 1, like A=PDL(0):PRINT A
, returning a value between 0 and 255.[36][h]
The Apple machines did not include dedicated sound hardware, only a simple "beeper". Producing sounds was accomplished by PEEK
ing the memory-mapped location of the speaker, -16336.[i]
Support for graphics was more detailed. Graphics mode was turned on with the GR
statement and off with TEXT
.[38] Drawing was modal and normally started by issuing a command to change the color, which was accomplished by setting a pseudo-variable; COLOR=12
would set the drawing color to 12, light green. One could then PLOT 10,10
to produce a single spot of that color,[39] HLIN 0,39 AT 20
to draw a horizontal line at row 20 that spanned the screen, or VLIN 5,15 AT 7
to draw a shorter vertical line down column 7.[40] A=SCRN X,Y
returned the color of the screen at X,Y.[34][j]
Integer BASIC included a TAB
feature, which positioned the cursor on a given column from 0 to 39. It differed from the versions found in most BASICs in that it was a command with a following number, as opposed to a function with the value in parenthesis; one would move the cursor to column 10 using TAB 10
in Integer BASIC[41] whereas in MS this would be PRINT TAB(10)
. Additionally, the VTAB
command worked similar to TAB
but added vertical spaces instead of horizontal. For unexplained reasons, in this case the coordinates were from 1 to 24 rather than 0 to 23.[42]
Input/output
Integer BASIC lacked any custom input/output commands, and also lacked the DATA
statement and the associated READ
. To get data into and out of a program, the input/output functionality was redirected to a selected card slot with the PR#x
and IN#x
, which redirected output or input (respectively) to the numbered slot. From then on, data could be sent to the card using conventional PRINT
commands and read from it using INPUT
.[34]
Included assembler
The Integer BASIC ROMs also included a machine code monitor, "mini-assembler", and disassembler to create and debug assembly language programs. Wozniak hand-assembled the monitor as the Apple II's first program, then used it to write Integer BASIC.[5][6][7]
Other notes
Integer BASIC included a POP
command to exit from loops.[34] This popped the topmost item off the FOR stack. Atari BASIC also supported the same command,[43] while North Star BASIC used EXIT
.[44]
Implementation
Integer BASIC read the lines typed in by the user from a buffer and ran them through a parser which output a series of tokens. As part of this process, simple syntax errors were detected and listed. If the parsing was successful, the line number (if present) was converted from ASCII decimal format into a 16-bit integer and any keywords into a 7-bit integer token.[5]
Numeric literals, like the value 500, were converted into their 16-bit (two-byte) binary representation, in this case, $01F4 hexidecimal. To indicate this was a value and not a keyword, a single byte between $B0 and $B9 was inserted in front of the two-byte value.[k] String literals, like "HELLO WORLD" were instead converted by setting the high bit of each character so that A
was stored as $C1. Variable names were converted in the same fashion, with the letters converted to have their high-bit turned on, and any digits in the name represented by the corresponding $B0 through $B9, so that the variable A5
would be tokenized as $C1B5.[5]
If the line was entered without a line number, the code was then executed directly from the buffer. If it had a line number, it was copied from the buffer into the program storage area.[5]
The runtime interpreter used two stacks for execution: one for statement keywords and the other for evaluating the parameters. Each statement was given two priorities: one that stated where it should occur in a multi-step operation, like a string of mathematical operations to provide order of operations, and another that suggested when evaluation should occur, for instance, calculating internal values of a parenthesis formula. When variables were encountered, their name was parsed and then looked up in the variable storage area. If it was not found, it was added to the end of the list. The address of the variable's storage, perhaps freshly created, was then placed on the evaluation stack.[5]
ROM details
SWEET16
In addition to Integer BASIC, the Apple ROMs contained a custom assembler language known as SWEET16. SWEET16 is based on bytecodes that run within a simple 16-bit virtual machine. This model was used so memory could be addressed via indirect 16-bit pointers and 16-bit math functions calculated without the need to translate those to the underlying multi-instruction 8-bit 6502 code. The entire virtual machine was written in only 300 bytes. Code can call SWEET16 by issuing a subroutine call, and then return to normal 6502 code when the 16-bit operations are complete.[45]
SWEET16 was not used by the core BASIC code, but was later used to implement several utilities. Notable among these was the line renumbering routine, which was included in the Programmer's Aid #1 ROM, added to later Apple II models and available for user installation on earlier examples.[46]
Floating point
Although Integer BASIC contained its own math routines, the Apple II ROMs also included a complete floating-point library located in ROM memory between $F425-F4FB and $F63D-F65D. The source code was included in the Apple II manual. BASIC programs requiring floating-point calculations could CALL
into these routines.[47]
Performance
Because Integer BASIC processed more of the original source code into tokens, the runtime was faster than versions that required additional runtime parsing. For comparison, Tiny BASIC tokenized only the line number,[48] while MS BASICs tokenized only the keywords. So for instance, while Integer BASIC would convert the line 100 GOTO 200
entirely into tokens that could be immediately read and performed, in MS BASIC only the line number and GOTO would be tokenized, the "100" was left in its original format and had to be re-parsed into a 16-bit integer every time the line was encountered.[49]
Additionally, working solely with integer math provides another major boost in speed. This is due both to the smaller 16-bit format requiring fewer memory accesses, as well as removing the need to move the floating-point decimal after calculations. As many computer benchmarks of the era were small and often performed simple math that did not require floating-point, Integer BASIC trounced most other BASICs.[l]
On one of the earliest known microcomputer benchmarks, the Rugg/Feldman benchmarks, Integer BASIC was well over twice as fast as Applesoft BASIC on the same machine.[51] In the Byte Sieve, where math was less important but array access and looping performance dominated, Integer BASIC took 166 seconds while Applesoft took 200.[52] It did not appear in the Creative Computing Benchmark, which was first published in 1983, by which time Integer BASIC was no longer supplied by default.[53]
The following test series, taken from both of the original Rugg/Feldman articles,[51][50] show Integer's performance relative the MS-derived BASIC on the same platform.
System | CPU | BASIC | Test 1 | Test 2 | Test 3 | Test 4 | Test 5 | Test 6 | Test 7 |
---|---|---|---|---|---|---|---|---|---|
Apple II | 6502 @ 1 MHz | Integer BASIC | 1.3 | 3.1 | 7.2 | 7.2 | 8.8 | 18.5 | 28.0 |
Apple II | 6502 @ 1 MHz | Applesoft BASIC | 1.3 | 8.5 | 16.0 | 17.8 | 19.1 | 28.6 | 44.8 |
Sample code
Notes
- ^ To make it clear which BASIC was running, Applesoft used the
]
prompt. - ^ In what MS referred to more descriptively as "immediate mode".
- ^ Helmers says the lowest line number is 1.[12]
- ^ Or as Bill Gates put it, "more powerful BASIC... using floating point".[22]
- ^ And completely broken in some versions.[30]
- ^ HP also allowed [ and ] in place of ( and ).
- ^ It also contrasts with Dartmouth BASIC which used the
CONVERT
command to convert a string into an array of ASCII values which the user then manipulated and converted back to string format with a secondCONVERT
. - ^ The manual suggests, but does not outright state, that the actual range of values is less than 0 to 255.[36]
- ^ The negative number is a side-effect of the integers being stored in signed format, so any memory location over 32767 appeared as a negative value in BASIC.[37]
- ^ Note the odd syntax of the SCRN, which is technically a function because it returns a value, but does not use function-like syntax which would be
A=SCRN(X,Y)
. - ^ The first digit of the original value was used to select a value from 0 to 9, so in this example, the token would be $B5 for the first digit of 500.
- ^ Bill Gates complained about this, stating that it was unfair to compare Integer BASIC to a "real" BASIC like MS.[50]
References
Citations
- ^ Fisk, Nathan (2009). Understanding Online Piracy. ABC-CLIO. p. 14.
- ^ Hertzfeld 1985.
- ^ a b c Wozniak 2014.
- ^ Weyhrich 2001, The [Integer] BASIC, which we shipped with the first Apple II's, was never assembled — ever. There was one handwritten copy, all handwritten, all hand-assembled..
- ^ a b c d e f Wozniak 1977, p. 42.
- ^ a b Helmers 1978, p. 18.
- ^ a b Weyhrich 2001.
- ^ Raskin 1978, p. 11.
- ^ Raskin 1978, p. 46.
- ^ Raskin 1978, pp. 49–55.
- ^ Raskin 1978, p. 48.
- ^ Helmers 1978, p. 24.
- ^ Raskin 1978, p. 118.
- ^ Raskin 1978, pp. 65–67.
- ^ Raskin 1978, p. 100.
- ^ Raskin 1978, p. 15.
- ^ Raskin 1978, p. 52.
- ^ Raskin 1978, p. 107.
- ^ Raskin 1978, p. 38.
- ^ Raskin 1978, p. 94.
- ^ Raskin 1978, p. 27.
- ^ Rugg, Tom; Feldman, Phil (October 1977). "BASIC timing comparisons… revised and updated". Kilobaud. pp. 20–25.
- ^ Raskin 1978, p. 61.
- ^ Raskin 1978, p. 40,120.
- ^ Raskin 1978, p. 40.
- ^ "Integer, Floating Point and String Variables". C64 Programmer's Manual. Commodore.
- ^ North Star BASIC version 6 (PDF). North Star Corporation. 1977.
- ^ {The ATARI BASIC Reference Manual. Atari Inc. 1980.
- ^ a b Raskin 1978, p. 89.
- ^ "Create your own Version of Microsoft BASIC".
- ^ a b Raskin 1978, p. 88.
- ^ Raskin 1978, p. 92.
- ^ altair 8080 basic manual (PDF). MITS. April 1977. p. 30.
- ^ a b c d Raskin 1978, p. 120.
- ^ Mini 1977, p. 17.
- ^ a b Raskin 1978, p. 36.
- ^ Mini 1977, p. 18.
- ^ Raskin 1978, p. 31.
- ^ Raskin 1978, p. 32.
- ^ Raskin 1978, p. 33.
- ^ Raskin 1978, p. 73.
- ^ Raskin 1978, p. 74.
- ^ Atari BASIC Reference Manual (PDF). Atari. 1983. p. 25.
- ^ North Star BASIC version 6 (PDF). North Star Corporation. 1977. p. 8.
- ^ Wozniak 1977, p. 43.
- ^ Apple Programmers Aid (PDF). Apple. 1978.
- ^ Apple II Reference Manual (PDF). January 1978. pp. 94–95.
- ^ Allison, Dennis (1976). "Build Your Own BASIC". Dr. Dobb's Journal. Vol. 1, no. 1. p. 11.
- ^ Hardiman, Roger. "Altair BASIC 3.2 (4K) - Annotated Disassembly". p. 1.11. Archived from the original on 5 November 2001.
- ^ a b Rugg, Tom; Feldman, Phil (October 1977). "BASIC timing comparisons… revised and updated". Kilobaud. pp. 20–25.
- ^ a b Rugg, Tom; Feldman, Phil (June 1977). "BASIC Timing Comparisons… information for speed freaks". Kilobaud. pp. 66–70.
- ^ Gilbreath, Jim (September 1981). "A High-Level Language Benchmark". Byte. p. 192.
- ^ Ahl, David (November 1983). "Benchmark Comparison Test". Creative Computing. p. 260.
Bibliography
- Apple II Mini Manual. Apple. 1977.
- Hertzfeld, Andy (June 1985). "The sad story of MacBasic". Folklore.
{{cite web}}
: Invalid|ref=harv
(help) - Helmers, Carl (March 1978). "An Apple to Byte". Byte. pp. 18–24, 30–32, 35, 40–46.
{{cite magazine}}
: Invalid|ref=harv
(help) - Raskin, Jef (1978). Apple II BASIC Programming Manual (PDF). Apple Computer.
{{cite book}}
: Invalid|ref=harv
(help) - Weyhrich, Steven (12 December 2001). "History part 3: The Apple II". Retrieved 2007-09-16.
{{cite web}}
: Invalid|ref=harv
(help) - Wozniak, Steven (May 1977). "System Description / The Apple-II". Byte. pp. 34–43.
{{cite magazine}}
: Invalid|ref=harv
(help) - Williams, Gregg; Moore, Rob (December 1984). "The Apple Story / Part 1: Early History". Byte. pp. A67 – A71. Retrieved 23 October 2013.
{{cite magazine}}
: Invalid|ref=harv
(help) - Wozniak, Steven (1 May 2014). "How Steve Wozniak Wrote BASIC for the Original Apple From Scratch". Gizmodo.
{{cite web}}
: Invalid|ref=harv
(help) - Wozniak, Steven (3 January 2018). "Apple BASIC". Woz.org.
{{cite web}}
: Invalid|ref=harv
(help)