Commodore BASIC - Technical Details

Technical Details

A convenient feature of Commodore's ROM-resident BASIC interpreter and KERNAL was the full-screen editor. Although Commodore keyboards only featured two cursor keys which alternated direction when the shift key was held, the screen editor allowed users to enter direct commands or to input and edit program lines from anywhere on the screen. If a line was prefixed with a line number, it was tokenized and stored in program memory. Lines not beginning with a number would be executed simply by pressing the RETURN key whenever the cursor happened to be on the line. This marked a significant upgrade in program entry interfaces compared to other common home computer BASICs at the time, which typically used line editors, invoked by a separate EDIT command, or a "copy cursor" that truncated the line at the cursor's position.

It also had the capability of saving named files to any device, including the cassette – a popular storage device in the days of the PET, and one that remained in use throughout the lifespan of the 8-bit Commodores as an inexpensive form of mass storage. Due to the cassette's use as an analog audio medium, they were as ubiquitous during the era as CDs are today. Most systems only supported filenames on diskette, which made saving multiple files on other devices more difficult. The user of one of these systems was required to note the recorder's counter display at the location of the file, but this was inaccurate and prone to error. Many non-Commodore users worked around the problem by only recording one file per tape. With the PET, when the user requested to load a file by name from the cassette, the device would read data sequentially, ignoring any non-matching filenames until the named file was reached and read into memory. The file system was also supported by a powerful record structure that could be loaded or saved to files. Another difference between the cassette transfer implementations of the Commodore and other systems was that Commodore tapes were encoded digitally, where other manufacturers used a less expensive analog interface. Users of these systems could thus connect a standard tape recorder instead of a specialized Datassette, but the analog signal was much less reliable.

Like the original Microsoft BASIC interpreter, on which it is based, Commodore BASIC is slower than native machine code. Test results have shown that copying 16 kilobytes from ROM to RAM takes less than a second in machine code, but over a minute in BASIC. To execute faster than the interpreter, programmers started using various techniques to speed up execution. One was to store often-used integer values in variables rather than using literal values, as interpreting a variable name was faster than interpreting a literal number. When speed was important, some programmers converted sections of BASIC programs to 6502 assembly language and executed them from BASIC using the SYS command; at other times, when speed was too fast, programmers dropped back to BASIC, and polled various addresses in memory (such as $A6 for the C-64, or $D0 for the C-128, denoting size of the keyboard queue) before they could start executing again.

Commodore BASIC keywords could be abbreviated by entering first an unshifted keypress, and then a shifted version of the next keypress. This set the high bit, causing the interpreter to stop reading and parse the statement according to a lookup table. This meant the statement up to where the high bit was set was accepted as a substitute for typing the entire command out. However, as BASIC keywords were stored in memory as single byte tokens in any case, this was a convenience for statement entry rather than an optimization.

In the default uppercase-only character set, shifted characters appear as a graphics symbol; e.g. the command, GOTO, could be abbreviated G{Shift-O} (which resembled GΓ onscreen). Most such commands were two letters long, but in some cases they were longer. In cases like this, there was an ambiguity, so more unshifted letters of the command were needed, such as GO{Shift-S} (GO♥) being required for GOSUB. Some commands had no abbreviated form, either due to brevity or ambiguity with other commands. For example, the command, INPUT had no abbreviation because its spelling collided with the separate INPUT# keyword, which was located nearer to the beginning of the keyword lookup table.

By abbreviating keywords, it was possible to view more code than would otherwise be possible on a single line (line lengths were usually limited to 2 or 4 screen lines, depending on the specific machine). This allowed for a slight saving on the overhead to store otherwise necessary extra program lines, but nothing more. All BASIC commands were tokenized and took up 1 byte (or two, in the case of several commands of BASIC 7 or BASIC 10) in memory no matter which way they were entered.

In the rare situation when commercial BASIC software was meant to be LIST-ed, each token's keyword was spelled out in full, leading to a line that extended over more screen lines than could be handled by the Logical Line Link Table. If programmers intended editing of their software by users, the user might nevertheless have found it daunting to edit with the on-screen editing capabilities. LISTing these long lines on early Commodore 64s near the bottom of the screen could trigger the "push-wrap-crash" bug in the 40 column screen editor, causing the machine to crash or return an OUT OF MEMORY error.

Commodore BASIC lines did not need any spaces except where omitting one would be ambiguous, and many Commodore BASIC programs were written with no spaces, e.g., 100IFA=5THENPRINT"YES":GOTO160. Omitting spaces as such would lead to a more compact program, since the tokenizer never removes any space inserted between keywords: the presence of spaces results in extra 0x20 bytes in the tokenized program which are merely skipped during execution.

The order of execution of Commodore BASIC lines was not determined by line numbering; instead, it followed the order in which the lines were linked in memory: much like a modern singly linked list, each program line was stored in memory as a line number, a pointer, and then the tokenized code for the line. The pointer contained the address in memory of the next program line. While a program was being entered, BASIC would constantly reorder program lines in memory so that the line numbers and pointers were all in ascending order. However after a program was entered, manually altering the line numbers and pointers with the POKE commands could allow for out-of-order execution or even give each line the same line number. In the early days, when software written in BASIC was available commercially, this was a software protection technique used to discourage casual modification of the program.

Variable names were only significant to 2 characters; thus the variable names VARIABLE1, VARIABLE2 and VA all referred to the same variable.

The native number format of Commodore BASIC, like that of its parent MS BASIC, was floating point. Most of the contemporary BASIC implementations used one byte for the characteristic (exponent) and three bytes for the mantissa. This led to problems in business applications since the accuracy of a floating point number using a three-byte mantissa is only about 6.5 decimal digits, and round-off error is common. Commodore, however, used MS BASIC's four-byte mantissa, which made their BASIC much more adapted for business than most other BASICs of the era.

Also akin to MS BASIC, 16-bit signed integers (i.e. in the range -32768 to 32767) were available by postfixing a variable name with a percent symbol, and string variables were represented by postfixing the variable name with a dollar sign. Despite the 2 character limit on variable names, the variables AA$, AA, and AA% would each be understood as distinct.

Many BASIC extensions were released for the Commodore 64, due to the relatively limited capabilities of its native BASIC 2.0. One of the most popular extensions was the DOS Wedge, due to its inclusion on the Commodore 1541 Test/Demo Disk. This 1 KB extension to BASIC added a number of disk-related commands, including the ability to read a disk directory without destroying the program in memory. Its features were subsequently incorporated in various third-party extensions, such as the popular Epyx FastLoad cartridge. Other BASIC extensions added additional keywords to make it easier to code sprites, sound, and high-resolution graphics like Simons' BASIC.

From a modern programming point of view, the earlier versions of Commodore BASIC presented a host of bad programming traps for the programmer. As most of these issues derived from Microsoft BASIC, virtually every home computer BASIC of the era suffered from similar deficiencies. Every line of a Microsoft BASIC program was assigned a line number by the programmer. It was common practice to increment numbers by some value (5, 10 or 100) to make inserting lines during program editing or debugging easier, but bad planning meant that inserting large sections into a program often required restructuring the entire code. Later BASIC versions on Commodore and other platforms included a DELETE and RENUMBER command, as well as an AUTO line numbering command that would automatically select and insert line numbers according to a selected increment. In addition, all variables are treated as global variables. Clearly defined loops are hard to create, often causing the programmer to rely on the GOTO command (this was later rectified in BASIC 3.5 with the addition of the DO, LOOP, WHILE, UNTIL, and EXIT commands). Flag variables often needed to be created to perform certain tasks. Furthermore, the 80 character line limit in earlier versions of Commodore BASIC often meant splitting tasks up into multiple routines, often resulting in spaghetti code. Earlier BASICs from Commodore also lack debugging commands, meaning that bugs and unused variables are hard to trap.

Read more about this topic:  Commodore BASIC

Famous quotes containing the words technical and/or details:

    A technical objection is the first refuge of a scoundrel.
    Heywood Broun (1888–1939)

    If my sons are to become the kind of men our daughters would be pleased to live among, attention to domestic details is critical. The hostilities that arise over housework...are crushing the daughters of my generation....Change takes time, but men’s continued obliviousness to home responsibilities is causing women everywhere to expire of trivialities.
    Mary Kay Blakely (20th century)