Tiny BASIC


Tiny BASIC is a dialect of the BASIC programming language that can fit into as little as 2 or 3 KB of memory. This small size made it invaluable in the early days of :Category:Early microcomputers|microcomputers in the mid-1970s, when typical memory size was only 4 to 8 KB. To meet these strict size limits, math was purely integer based and it lacked arrays or string manipulation.
The language was written, in part, as an alternative to Microsoft BASIC. MS BASIC would also run in 4 KB machines, but left only 790 bytes free for the programs. More free space was a significant advantage of Tiny BASIC.
Another reason for Tiny BASIC's popularity was an open letter published by Bill Gates complaining about users "ripping off" MS BASIC. Tiny BASIC was published openly and later invented the term "copyleft" to describe this. This made it popular in the burgeoning early microcomputer market.
Tiny BASIC was published in a newsletter offshoot of the People's Computer Company. Dozens of versions were created for almost every platform of the era, and there were many variations and additions that were published over time. The newsletter eventually became Dr. Dobb's Journal, a long-lived computing magazine. Tiny BASIC is an example of a free software project that existed before the free software movement.

History

, a member of the Computer Science faculty at Stanford University, wrote a specification for a simple version of the BASIC programming language. He was urged to create the standard by Bob Albrecht of the Homebrew Computer Club, who had seen BASIC on minicomputers and felt it would be the perfect match for new machines like the MITS Altair 8800, which had been released in January 1975. Allison's proposed design only used integer arithmetic and did not support arrays or string manipulation. The goal was for the program to fit in 2 to 3 kilobytes of memory.
The overall design for Tiny BASIC was published in the September 1975 issue of the People's Computer Company newsletter, along with an 8080-version of the intermediate language runtime. New implementations 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.
Questions and comments poured in, and by the end of the year, Albrecht promised to collect these into a separate newsletter and publish at least three editions. The first edition was published in January 1976 as "Dr. Dobb's Tiny BASIC Journal: Calisthenics & Orthodontia, Running Light Without Overbyte". It included a reprint of the original September article, Tiny BASIC Extended, and many notes and comments from users.
Response to the first issue was so impressive that the introduction to the second issue stated they had already decided to continue publishing the new newsletter under the name Dr. Dobb's Journal. Over the next several issues, additional versions of the language were published, and similar articles began appearing in other magazines like Interface Age.
By the middle of 1976, Tiny BASIC interpreters were available for the Intel 8080, the Motorola 6800 and MOS Technology 6502 processors. This was a forerunner of the free software community's collaborative development before the internet allowed easy transfer of files, and was an example of a free software project before the free software movement. Computer hobbyists would exchange paper tapes, cassettes or even retype the files from the printed listings.
Jim Warren, editor of Dr. Dobb's, wrote in the July 1976 ACM Programming Language newsletter about the motivations and methods of this successful project. He started with this: "There is a viable alternative to the problems raised by Bill Gates in his irate letter to computer hobbyists concerning 'ripping off' software. When software is free, or so inexpensive that it's easier to pay for it than to duplicate it, then it won't be 'stolen'." The Bill Gates letter was written to make software into products. The alternative method was to have an experienced professional do the overall design and then outline an implementation strategy. Knowledgeable amateurs would implement the design for a variety of computer systems. Warren predicted this strategy would be continued and expanded.
The May 1976 issue of Dr. Dobbs had Li-Chen Wang's Palo Alto Tiny BASIC for the Intel 8080 microprocessor. The listing began with the usual title, author's name and date but it also had "@COPYLEFT ALL WRONGS RESERVED". A fellow Homebrew Computer Club member, Roger Rauskolb, modified and improved Li-Chen Wang's program and this was published in the December 1976 issue of Interface Age magazine. Roger added his name and preserved the COPYLEFT Notice. Other versions of Tiny BASIC existed, such as the one written by Thomas F. Waitman in 1976 for the Hewlett-Packard HP-2640 and HP-2645 terminals. Thomas F. Waitman wrote articles for the Hewlett-Packard Journal.

TRS-80

In 1977, RadioShack released their first computer, TRS-80. It had a BASIC interpreter in ROM, which was derived from "Palo Alto Tiny BASIC". Dr. Wang's version occupied less than 2 KB; the RadioShack derivation replaced the integer representation of numbers with floating point representation and added some I/O support, e.g. for the cassette tape interface. This version was known as Level I BASIC, and fit in 4 KB ROM.

Description

Basic concepts

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.
Like most BASICs of the era, TB 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.
If the user typed a statement into the command line the system examined it to see if it started with a number. If it did not, the line was immediately parsed and operated on, potentially generating output via. This was known as "direct mode".
If the line was entered with a leading number, the number was converted from decimal format, like "50", and converted to a 8-bit value, in this case, $32 hexidecimal. This number was used as an index into an array-like storage area where the rest of the line was stored in exactly the format it was typed. When the user typed into the command line the system would loop over the array, convert the line number back to decimal format, and then print out the rest of the text in the line.
When a program was present in memory and the user types in the command, the system enters "indirect mode". In this mode, a pointer is set to point to the first line of the program, for instance, 10. The original text for that line is then retrieved from the store and run as if the user had just typed it in direct mode. The pointer then advances to the next line and the process continues.

Formal grammar

The grammar is listed below in Backus-Naur form. In the listing, an asterisk denotes zero or more of the object to its left — except for the first asterisk in the definition of "term", which is the multiplication operator; parentheses group objects; and an epsilon signifies the empty set. As is common in computer language grammar notation, the vertical bar distinguishes alternatives, as does their being listed on separate lines. The symbol "CR" denotes a carriage return. A BREAK from the console will interrupt execution of the program.

line ::= number statement CR | statement CR
statement ::= PRINT expr-list
IF expression relop expression THEN statement
GOTO expression
INPUT var-list
LET var = expression
GOSUB expression
RETURN
CLEAR
LIST
RUN
END
expr-list ::= *
var-list ::= var *
expression ::= term *
term ::= factor *
factor ::= var | number |
var ::= A | B | C... | Y | Z
number ::= digit digit*
digit ::= 0 | 1 | 2 | 3 |... | 8 | 9
relop ::= < | > | =
string ::= " * "

Palo Alto Tiny BASIC

One of the most popular of the many versions of Tiny BASIC was Palo Alto Tiny BASIC, or PATB for short. PATB first appeared in the May 1976 edition of Dr. Dobbs, written in a custom assembler language with non-standard mnemonics. This led to further ports that worked with conventional assemblers on the 8080.
One of the most notable changes in PATB is the addition of the FOR...NEXT loop. In the original TB, loops could only be implemented using and. As in Microsoft BASIC, the upper and lower bounds of the loop were set on loop entry, and did not change during the loop, so if one of the bounds was based on a variable expression, changing the variable did not change the bound. The modifier was optional, as in MS.
Another significant change was the ability to place several statements on a single line. For reasons not explained, PATB used the semicolon, to separate statements, rather than the already common colon,.
Other changes include the addition of a single numeric array, with the variable name, in addition to, and the use of for not-equals in comparisons, as opposed to.
PATB used words for error messages instead of numbers. To reduce the amount of memory required, there were only three messages and they consisted of single words. The system would respond with for syntax errors, for run-time errors like GOTOs to a line that didn't exist or numeric overflows, and for out-of-memory problems.
PATB also changed the editor in order to ease the typing of programs, which in the era of mechanical terminals was often slow and expensive. PATB allowed any keyword to be abbreviated to its minimal unique string, with a trailing period. For instance, could be typed, although and other variations also worked. This system was retained in Level I BASIC for the TRS-80, which used PATB, and was also found in Atari BASIC or the BASIC of various Sharp Pocket Computers.

Implementation in virtual machine

For some implementations, including the first Tiny BASIC and Tom Pittman's Tiny BASIC, a virtual machine was used, others such as Palo Alto Tiny BASIC and 6800 Tiny BASIC, were direct interpreters. In a virtual machine implementation, 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 and Glennie.
The following table gives a partial list of the commands of the virtual machine in which the first Tiny BASIC interpreter was written. The length of the whole interpreter program was only 120 virtual machine operations. Thus the choice of a virtual machine approach economized on memory space and implementation effort, although the BASIC programs run thereon were executed somewhat slowly.
TST lbl, stringIf 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
JUMP lblContinue execution of the IL at the label specified
CALL lblExecute the IL subroutine starting at lbl; save the IL address following the CALL on the control stack
RTNReturn to the IL location specified at the top of the control stack
DONEReport a syntax error if after deleting leading blanks the cursor is not positioned to reach a carriage return
PRSPrint characters from the BASIC text up to but not including the closing quotation mark
PRNPrint number obtained by popping the top of the expression stack
SPCInsert spaces to move the print head to next zone
NLINEOutput a CRLF to the printer