Applesoft BASIC

From Computer History Wiki
Jump to: navigation, search

Applesoft BASIC (sometimes called "Applesoft II") was the second dialect of BASIC supplied on the Apple II computer, superseding Integer BASIC. Applesoft BASIC was supplied by Microsoft and its name is derived from the names of both Apple and Microsoft.

Apple's customers were demanding a version of BASIC that supported floating point calculations. As Steve Wozniak, the creator of Integer BASIC and the only person who understood it well enough to add floating point features, was busy with the Disk II drive and controller and with Apple DOS, Apple turned to Microsoft, who was the BASIC vendor of choice after their success with Altair BASIC, and licensed a 10 KB assembly language version of BASIC dubbed "Applesoft." It was similar to (and indeed shared a common code base with) BASIC implementations on other 6502-based computers, such as Commodore BASIC: it used line numbers, and spaces were not necessary in lines.

While Applesoft was slower than Integer BASIC, it had many features that the older BASIC lacked:

  • Atomic strings. A string is no longer an array of characters (like in Integer BASIC and C); it is instead a garbage-collected object (like in Scheme and Java). This allows for string arrays; DIM A$(10) resulted in a vector of eleven string variables numbered 0 to 10.
  • Multidimensional arrays.
  • Single-precision floating point variables with an 8-bit exponent and a 31-bit significand and improved math capabilities, including a trigonometry library.
  • Commands for high-resolution graphics.
  • CHR$, ASC, STR$, and VAL functions for converting between string and numeric types
  • LET statement optional
  • User defined functions (simple one-line functions written in BASIC, with a single parameter)
  • Error-trapping, allowing BASIC programs to handle unexpected errors by means of a subroutine written in BASIC.

Relatively few action games were written in Applesoft BASIC, for several reasons:

  • In this era of carefully counting clock cycles and limited memory, it was inefficient to write speed-dependent programs that ran on a runtime interpreter.
  • The use of real numbers for all math operations created unnecessary overhead and degraded performance. Applesoft actually converted integer numbers to real before performing operations on them, then converted them back to integers if necessary.
  • So-called shape tables are a slow alternative to bitmaps. No provision existed for mixing text and graphics, except for the limited "Hardware split screen" of the Apple II (four lines of text at the bottom of the screen). Many graphics programs thus contained their own bitmap character generator routines. No provision was added in the 128 KB Apple IIe and Apple IIc models' BASIC interpreters for the new machines' extra memory and double-resolution graphics, or for the Apple IIGS's 16-color mode. (Beagle Bros offered machine-language workarounds for these problems.)
  • The program was stored as a linked list of lines; a GOTO took O(n) (linear) time, and although Applesoft programs were not very long compared to today's software, on a 1 MHz 6502 this could be a significant bottleneck.
  • No sound support aside from a PEEK command that could be used to click the speaker. The language was not fast enough to produce more than a baritone buzz from repeated clicks anyway. However, compiled assembly language was fast enough, and music spanning several octaves could be played by repeated calls to a machine-language tone generator.

Here's Hello World in Applesoft BASIC:

10 TEXT:HOME
20 ?"HELLO WORLD"

Multiple commands could be included on the same line of code if separated by a colon (:). The ? can be used in Applesoft BASIC as a shortcut for "PRINT", though spelling out the word is not only acceptable but canonical -- Applesoft converted "?" in entered programs to "PRINT", which would appear when a program was listed. So the program above would actually appear in a LIST command as shown below:

10  TEXT : HOME
20  PRINT "HELLO WORLD"

This article includes text from Everything2, licensed under GFDL.

Trivia

  • There was a well-documented bug in Applesoft BASIC that could actually crash the interpreter if ONERR GOTO was in effect and numerous program errors occurred. Apple provided a short assembly-language routine which could be POKEd into RAM and CALLed to ameliorate the problem to an extent. Later it was discovered by an enterprising hacker that the required code was actually in the Applesoft ROM (though it was never executed) and could be called there instead.
  • Applesoft could be extended by two means: the ampersand (&) command and the USR() function. Machine-language routines could be attached to the ampersand and USR hooks in the computer's memory and call Applesoft ROM code to retrieve the values of parameters that followed them. A number of third-party packages were produced to add functionality to Applesoft by one or the other of these mechanisms. Additionally, CHRGET, a vector at location $B1, fetches the next character from program text and provided a hook that allowed external routines to change the interpreter's behavior.
  • Applesoft, like Integer BASIC before it, did not come with any built-in commands for dealing with files or disks. The Apple II disk operating system, known simply as DOS, thus intercepted all input typed at the BASIC command prompt to determine whether it was a DOS command. Similarly, all output was scrutinized for a Control-D character (ASCII 4), which BASIC programs would send before printing a disk command to get DOS's attention. ProDOS followed this lead, although the BASIC command interpreter was placed in a separate program called BASIC.SYSTEM.
  • Neither Apple nor Microsoft ever made source code for Applesoft BASIC available. However, Glen Bredon included a program with his Merlin assembler that would generate a commented copy of a BASIC disassembly from the ROMs in a user's computer (using the ROMs to decrypt the provided comments and so forth), cleverly skirting copyright law.
  • Both Integer BASIC and Applesoft used the technique of tokenizing to reduce the memory requirements of programs and to speed their interpretation. As code was entered, BASIC keywords would be converted to single-byte tokens; the process was reversed when the program was listed. Integer BASIC used characters with codes above 127 for normal text and codes below 128 for tokens; Applesoft used exactly the reverse.
  • Due to Applesoft BASIC's slow performance, BASIC compilers were much sought-after tools for BASIC programmers. The first, Microsoft's The AppleSoft Compiler (TASC), was actually written in Applesoft and then used to compile itself. A later product, the Einstein Compiler, was somewhat more sophisticated and offered better performance both in compilation and in execution. The ultimate BASIC compiler was the Beagle Compiler, written by Alan Bird and published by Beagle Bros; it integrated closely with ProDOS (the current Apple II OS at the time of its release), compiled programs in seconds rather than minutes, and achieved superior code execution performance by optimizing integer math and pre-computing addresses of GOTO targets, among other tricks. Unlike previous compilers, the Beagle Compiler did not truly compile BASIC programs to machine code, but rather converted them to a highly optimized bytecode that was interpreted by a runtime module, much like the UCSD p-System.