User:Jeffrey Henning/sandbox

Tiny BASIC is a family of dialects of the BASIC programming language that can fit into 4 or fewer KBs of memory. Tiny BASIC was designed in response to the open letter published by Bill Gates complaining about users "pirating" Altair BASIC, which was sold for $150. The Tiny BASIC language was specified first and then programmers were invited to implement it for different microprocessors and to share their source code openly. Dr. Li-Chen Wang, author of Palo Alto Tiny BASIC, coined the term "copyleft" to describe this. Tiny BASIC is an example of a free software project that existed before the free software movement. Community response to Tiny BASIC was so overwhelming that what had been planned as three newsletters was relaunched as Dr. Dobb's Journal, the first regular periodical to focus on microcomputer software, which lasted in print form for 34 years.

The small size and free source code made these implementations invaluable in the early days of microcomputers in the mid-1970s, when RAM was expensive and typical memory size was only 4 to 8 KB.

To meet these strict size limits, Tiny BASIC dialects were typically different from other implementations of BASIC in key ways: As this was a community call for BASIC implementations, anyone could create a Tiny BASIC dialect, and the dialects varied widely in language structure. Some truncated keywords, some allowed abbreviations, some offered nonstandard ways of accessing RAM to work around the lack of arrays and string handling.
 * The source code was available for free as type-in programs
 * Math was purely integer based
 * Only 26 variables, named A to Z, were available; RUN did not necessarily reset these variables to zero
 * The arguments of IF and GOTO could be numeric expressions:
 * IF executed its subsequent statement on any non-zero value
 * GOTO and GOSUB could take an expression rather than a line number, providing an assigned GOTO rather than the switch statement of the ON-GOTO/GOSUB structure more typical of BASIC.

Tiny BASIC implementations are still used today, for programming microcontrollers such as the Arduino.

The overall design for Tiny BASIC was published in the September 1975 issue of the People's Computer Company (PCC) newsletter, along with the intermediate language source code. The newsletter provided references to compiler texts, and singled out UIUC BASIC. In June 1974, Alfred Weaver, Michael Tindall, and Ronald Danielson of the University of Illinois at Urbana-Champaign had proved it was possible to produce "A BASIC Language Interpreter for the Intel 8008 Microprocessor," in their paper of the same name, though their application was deployed to an 8008 simulator for the IBM 360/75 and required 16kB.

Implementations of Tiny BASIC that met and often exceeded the design criteria were soon being forwarded to the PCC, most notably Tiny BASIC Extended by Dick Whipple and John Arnold which ran in 3K of RAM, added FOR...NXT loops, and allowed a single numeric array. The duo wrote Tiny BASIC Extended directly in machine code, using octal.

Palo Alto Tiny BASIC was adapted for the Sharp PC-1211 handheld computer. Other Tiny BASIC implementations were later developed for programmable calculators, but modern calculator BASICs differ widely from Tiny BASIC, with unique syntax and many additional functions. A last vestige of Tiny BASIC in these implementations is the restriction of variable names (A-Z and one array, Z, in Casio BASIC, similar to TI-BASIC, which adds numbered strings and lists).

Tiny BASIC was designed to use as little memory as possible, and this is reflected in the paucity of features as well as details of its interpreter system. Early microcomputers lacked the RAM and secondary storage for a BASIC compiler, which was more typical of timesharing systems.

Like most BASICs of the era, Tiny Basic was interactive with the user typing statements into a command line. As microcomputers of the era were often used with teletype machines or "dumb" terminals, direct editing of existing text was not possible and the editor instead used takeout characters, often the backslash, to indicate where the user backed up to edit existing text.

This syntax, as simple as it was, added one innovation: GOTO and GOSUB could take an expression rather than just a line number, providing an assigned GOTO rather than the switch statement of the GOTO/GOSUB ... OF ..., a structure then supported in HP Time-Shared BASIC and predating ON ... GOTO. The syntax allowing IF-THEN statement (as opposed to just a line number to branch to) was not yet supported in Dartmouth BASIC as this time but had been introduced by Digital and copied by Microsoft.

Implementation in a virtual machine
The Design Note specified a virtual machine, in which the Tiny BASIC interpreter is itself run on a virtual machine interpreter. The designer's idea to use an application virtual machine goes back to Val Schorre (with META II, 1964) and Glennie (Syntax Machine). The choice of a virtual machine approach economized on memory space and implementation effort, although the BASIC programs run thereon were executed somewhat slowly.

Dialects that used the virtual machine included Tiny BASIC Extended, Tom Pittman's Tiny BASIC and NIBL. Other dialects such as Denver Tiny BASIC (DTB) and Palo Alto Tiny BASIC were direct interpreters. Some programmers, such as Fred Greeb with DTB, treated the IL program as pseudocode for the algorithm to implement in assembly language; Denver Tiny BASIC did not use a virtual machine, but it did closely follow the IL program.

This is a representative excerpt from the 120-line IL program:

A common pattern in the program is to test for a keyword or part of a keyword, then act on that information. Each test is an assertion as to what is next in the line buffer. If the assertion fails, control jumps to a subsequent label (usually looking for a new keyword or token). Here the system advances its buffer cursor over any spaces and tests for GO and if it fails to find it then jumps to line S3. If it finds it, execution continues with the next IL command. In this case, the system next tests for TO, skipping to line S2 if it fails (a test for SUB, to see if this is instead a GOSUB command). If it passes, control continues; in this case, calling an IL subroutine that starts at label EXPR, which parses an expression. In Tiny BASIC, GOTO X*10+100 (a computed GO TO) is as legal as GOTO 100 and is the alternative to the ON-GOTO of larger BASIC implementations. The subroutine EXPR pushes the result of the expression onto the arithmetic stack (in this case, the line number). DONE verifies no other text follows the expression and gives an error if it does. XFER pops the number from the stack and transfers execution (GOes TO) the corresponding line number, if it exists.

The following table gives a partial list of the 32 commands of the virtual machine in which the first Tiny BASIC interpreter was written.
 * TST lbl, string


 * If string matches the BASIC line, advance cursor over string and execute the next IL instruction; if the test fails, execute the IL instruction at the label lbl


 * CALL lbl : Execute the IL subroutine starting at lbl; save the IL address following the CALL on the control stack


 * DONE : Report a syntax error if after deleting leading blanks the cursor is not positioned to reach a carriage return


 * XFER : Test value at the top of the AE stack to be within ranqe. If not, report an error. If so, attempt to position cursor at that line. If it exists, begin interpretation there; if not, report an error.


 * JUMP lbl : Continue execution of the IL at the label specified

Tom Pittman, discussing the IL, says: "The TINY BASIC interpreter was designed by Dennis Allison as a recursive descent parser. Some of the elegant simplicity of this design was lost in the addition of syntactical sugar to the language but the basic form remains. The IL is especially suited to Recursive Descent parsing of TINY BASIC because of the general recursive nature of its procedures and the simplicity of the TINY BASIC tokens. The IL language is effectively optimized for the interpretation of TINY. Experience has shown that the difficulty of adding new features to the language is all out of proportion with the nature of the features. Usually it is necessary to add additional machine language subroutines to support the new features. Often the difficulty outweighs the advantages."
 * RTN : Return to the IL location specified at the top of the control stack
 * PRS : Print characters from the BASIC text up to but not including the closing quotation mark
 * PRN : Print number obtained by popping the top of the expression stack
 * SPC : Insert spaces to move the print head to next zone
 * NLINE : Output a CRLF to the printer

Deviations from the design
Defining Tiny BASIC for the Homebrew Computer Club, Pittman wrote, "Tiny BASIC is a proper subset of Dartmouth BASIC, consisting of the following statement types only: LET, PRINT, INPUT, IF, GOTO, GOSUB, RETURN, END, CLEAR, LIST, RUN. Arithmetic is in 16-bit integers only with the operators + - * / and nested parentheses. There are only the 26 single letter variable names A, B, ...Z, and no functions. There are no strings or arrays... Tiny BASIC specifies line numbers less than 256." He then went on to describe his implementation: "This language has been augmented to include the functions RND, USR, and PEEK and POKE, giving the user access to all his system components in the 6800 from the BASIC program."

Many implementers brought their own experiences with HP Time-Shared BASIC or DEC BASIC-PLUS to their designs and relaxed the formal Tiny BASIC language specification. Of the seven prominent implementations published by 1977: As an alternative to tokenization, to save RAM, TBX, DTB, and MINOL truncated keywords: PR for PRINT, IN for INPUT, RET for RETURN. The full, traditional keywords were not accepted. In contrast, PATB allowed accepted traditional keywords but also allowed any keyword to be abbreviated to its minimal unique string, with a trailing period. For instance, PRINT could be typed P., although PR. and other variations also worked. This system was retained in Level I BASIC for the TRS-80, which used PATB, and was also later found in Atari BASIC and the BASIC of various Sharp Pocket Computers.
 * All added some sort of random number function, typically RND.
 * All enabled LET to be optional and most let expressions in assignment statements contain relational operators.
 * All but 6800TB supported statement delimiters in lines, typically : although TBX used $ and PATB used ;.
 * In IF statements, all but MINOL removed the need for expressions to contain relational operators (e.g., IF X THEN LET Y=X was valid). The majority removed THEN, made it optional, or supported implied GOTO.
 * Many modified PRINT to support print zones, using, to go to the next zone and ; to not advance the cursor.
 * All but 6800TB and DTB added NEW.
 * All but 6800TB and MINOL added a function to return memory size: TBX had SZE, DTB and PATB had SIZE, L1B had MEM, and NIBL had TOP.
 * Four implementations added arrays, whether a single, undimensioned array in PATB and L1B or DIMensionable arrays in TBX and DTB.
 * Four implementations added the REMark statement.
 * Only PATB, NIBL, and L1B offered FOR-TO-STEP/NEXT, although TBX supported FOR 1,10$NXT.
 * Only NIBL had any nod towards structured programming, with DO/UNTIL, despite Allison's lament in Issue 2 about problems with BASIC.

Dialects
The most prominent dialects of Tiny BASIC were the original Design Note, Tiny BASIC Extended, Palo Alto Tiny BASIC, and 6800 Tiny BASIC. However, many other versions of Tiny BASIC existed.

List of prominent dialects
Tiny BASIC was first published in a newsletter offshoot of the People's Computer Company, a newsletter which became Dr. Dobb's Journal, a long-lived computing magazine. About ten versions were published in the magazine.

TBX was also known as Texas Tiny BASIC.

Both SCELBAL and 6800 Tiny BASIC were announced in the magazine but did not publish their source code.

Wang also wrote a STARTREK program in his Tiny BASIC that appeared in the July 1976 issue of the People's Computer Company Newsletter.

He later adapted the language into 3K Control Basic for Cromemco, adding variable names of the form letter-digit (e.g.,  to  ), logic functions, a   command to execute machine language routines, more  -formatting options, and others (  and   instead of   and  ; I/O port functions).

Palo Alto Tiny BASIC was adapted for many other implementations, including Level I BASIC (1977), BASIC for the Sharp PC-1211 pocket computer (1980), and Astro BASIC (1982, by Jamie Fenton).

MINOL
Written by a junior in high school, MINOL was the only implementation that didn't support the full Design Note, lacking operator precedence, having only three relops (<, =, #), omitting GOSUB and RETURN. It only supported unsigned 8-bit precision (in contrast to signed 16-bit precision for every other implementation) and line numbers from 0 to 254.

No spaces were permitted except in strings; ! returns a random number, $ before an expression loads a string at that address; OS returns to operating system. Memory was addressable as if it were a two-dimensioned array of high and low bytes (e.g., "(0,0)" to "(255,255)"); CALL calls a machine language subroutine.

Miscellaneous dialects
Many dialects appeared in various other publications.

Inspired by PCC's call for Tiny BASICs, Robert Uiterwyk wrote MICRO BASIC 1.3 for the SWTPC (a 6800 system), which SWTPC published in the June 1976 issue of the SWTPC newsletter. Uiterwyk had handwritten the language on a legal tablet. He later expanded the language to 4K, adding support for floating point; this implementation was unique among BASIC interpreters by using Binary Coded Decimal to 9 digits of precision, with a range up to 10E99, and by being published for free as a "Floppy ROM" magazine insert. An 8K version added string variables and trigonometry functions. Both the 4K and 8K versions were sold by SWTPC. In January, 1978, Uiterwyk sold the rights of the source code to Motorola.

Thomas F. Waitman wrote a Tiny BASIC in 1976 for the Hewlett-Packard HP-2640 and HP-2645 terminals (which used the Intel 8008 and 8080 processors), which was published in the Hewlett-Packard Journal.

Published in the December 1976 issue of Interface Age was LLL (Lawrence Livermore Laboratory) BASIC, the first draft of which was developed by Steve Leininger from Allison's specification before Leininger left National Semiconductor for Tandy Corporation. The final interpreter was developed by John Dickenson, Jerry Barber, and John Teeter at the University of Idaho on a contract with LLL. Taking 5K, it included a floating point package, developed by David Mead, Hal Brand, and Frank Olken. The program was placed into the public domain by LLL, which developed the system under the auspices of the U.S. Energy Research and Development Administration.

4K BASICs
Altair BASIC, 4K BASIC, could run within a 4kB RAM machine, leaving only about 790 bytes free for program code. The Tiny BASIC initiative started in response to the $150 charge for Altair 4K BASIC.

In 1975, Steve Wozniak joined the newly formed Homebrew Computer Club, which had fellow members Li-Chen Wang (Palo Alto Tiny BASIC) and Tom Pittman (6800 Tiny BASIC). Wozniak concluded that his machine would have to have a BASIC of its own, which would, hopefully, be the first for the MOS Technology 6502 processor. As the language needed 4 kB RAM, he made that the minimum memory for the design. Integer BASIC was originally published on Compact Cassette in 1976.

In 1977, Radio Shack (as it was known then) released their first computer, the TRS-80, a Z80 system with Level I BASIC in a 4kB ROM. Tandy-employee Steve Leininger had written the first draft of the NIBL (National Industrial Basic Language) interpreter for the SC/MP while employed at National Semiconductor. Unable to take that source code with him, he adapted Li-Chen Wang's Palo Alto Tiny BASIC for the original prototype of the TRS-80 Model I. He extensively revised the interpreter, adding floating-point support, simple black-and-white graphics, and READ/DATA/RESTORE statements.

Originally developed in 1979, Sinclair 4K BASIC, written by John Grant, used as its language definition the 1978 American National Standards Institute (ANSI) Minimal BASIC standard, but was itself an incomplete 4Kb implementation with integer arithmetic only.

Microcontroller dialects
Tiny BASIC implementations have been adapted for processor control and for microcontrollers such as the Arduino:
 * Stephen A. Ness wrote XYBASIC for the Mark Williams Company in 1977, a 4K integer implementation. The language was often used for process control applications.
 * Arduino BASIC - Adapted from Gordon Brandly's 68000 Tiny BASIC, ported to C by Mike Field.
 * Tiny Basic Plus - Adapted from Arduino BASIC by Scott Lawrence.
 * Half-Byte Tiny Basic - Adapted from Arduino BASIC.
 * Tiny Basic on the Micro: Bit - Adapted from Palo Alto Tiny BASIC.

Dialects compared
The following table compares the language feature of Tiny BASIC implementations against other prominent BASICs that preceded them.