'''Applesoft BASIC''' was a dialect of [[BASIC programming language|BASIC]] supplied on the [[Apple II family|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. The first version of Applesoft was released in 1977 only on cassette tape and lacked support for high-resolution graphics. Applesoft II, which was made available on cassette and disk and in the ROM of the Apple II Plus and subsequent models, was released in 1978. It is this latter version, which has some syntax differences from the first as well as support for the Apple II high-resolution graphics modes, that most people mean by the term "Applesoft."
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." Apple reportedly obtained an eight-year license for Applesoft BASIC from Microsoft for a flat fee of $21,000, renewing it in 1985 through an arrangement that gave Microsoft the rights and source code for Apple's Macintosh version of BASIC.
Applesoft was similar to (and indeed had a common code base with) BASIC implementations on other [[MOS Technology 6502|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 (as in Integer BASIC and [[C (programming language)|C]]); it is instead a [[garbage collection (computer science)|garbage-collected]] object (as in [[Scheme (programming language)|Scheme]] and [[Java (programming language)|Java]]). This allows for string arrays; <code>DIM A$(10)</code> resulted in a [[array|vector]] of eleven string variables numbered 0 to 10.
* Multidimensional [[array]]s.
* Single-precision [[floating point]] variables with an 8-bit exponent and a 31-bit significand and improved math capabilities, including trigonometry and logarithmic functions.
* Commands for high-resolution graphics.
* <code>CHR$, ASC, STR$,</code> and <code>VAL</code> functions for converting between string and numeric types
* <code>LET</code> statement optional
* [[User Defined Function|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.
* PEEK and POKE commands that let the user read the contents of a memory location (in decimal) or stuff a numeric value (specified in decimal) into any desired memory location.
Whereas Wozniak originally referred to his Integer BASIC as "Game BASIC," having written it so he could write a Breakout game for his new computer, few [[action game]]s 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 converted integer numbers to real before performing operations on them, converting the result back to an integer only if it was to be assigned to a (16-bit signed) integer variable.
* So-called [[shape table]]s are a slow alternative to [[bitmap]]s. 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|Apple II<small>GS</small>'s]] 16-color mode. ([[Beagle Bros]] offered machine-language workarounds for these problems.)
* The program was stored as a [[linked list]] of lines; a <code>[[GOTO]]</code> took [[Computational complexity theory|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 (one could also PRINT an ASCII bell character to sound the system alert beep). The language was not fast enough to produce more than a baritone buzz from repeated clicks anyway. However, 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 [http://everything2.com/?node=Applesoft+BASIC Everything2], licensed under GFDL.''
==Apple Business BASIC==
Apple Business BASIC shipped with the [[Apple ///]] computer. Donn Denman ported Applesoft BASIC to [[Apple SOS|SOS]] and reworked it to take advantage of the extended memory of the Apple ///. Following the trend of avoiding absolute addresses, the PEEK and POKE commands were replaced with INVOKE and PERFORM statements that loaded and executed separately-assembled code modules.
== Trivia ==
{{trivia|date=November 2007}}
* Despite its heritage and common code base with other 6502 BASICs, Applesoft lacked commands common to Microsoft BASIC interpreters, such as INSTR (which searched for a substring in a given string), PRINT USING (which formatted numbers with commas and currency signs according to a format string), and INKEY$ (which checked for a keypress without stopping the program as Applesoft's GET did). It seems likely that memory constraints were at the root of these differences, as the Apple II ROM had only 10 kilobytes available for the interpreter, and the improved hi-res graphics support was clearly a higher priority. Microsoft's CLS command (for clearing the screen) was renamed HOME in Applesoft.
* 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's garbage collector was notoriously slow (O(''n''²)). If a program had a large number of string variables, garbage collection, which occurred when the interpreter ran out of memory for a new string allocation, could seemingly lock up the computer for several minutes. Since users could not know when the computer was running low on string memory, these pauses seemed random and inexplicable. Though third parties provided some improvements, Apple did not truly fix the problem until ProDOS, which included a new garbage collector that did the same job in seconds as part of BASIC.SYSTEM.
* 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 byte from the program 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]].
== See also ==
* [[Chinese BASIC]]: A Chinese-localized version of Applesoft BASIC.
[[Category:Apple II software]]
[[Category:BASIC programming language family]]
[[Category:Microsoft BASIC]]
[[ko:애플소프트 베이직]]
[[nl:Applesoft BASIC]]
[[de:Applesoft BASIC]]