# Integer BASIC

Integer BASIC is a BASIC interpreter written by Steve Wozniak for the Apple I and Apple II computers. Originally available on cassette for the Apple I in 1976, then included in ROM on the Apple II from its release in 1977, it was the first version of BASIC used by many early home computer owners.
The only numeric data type was the integer; floating-point numbers were not supported. Using integers allowed numbers to be stored in a compact 16-bit format that could be more rapidly read and processed than the 32- or 40-bit floating-point formats found in most BASICs of the era. This made it so fast that Bill Gates complained when it outperformed Microsoft BASIC in benchmarks. However, this also limited its applicability as a general-purpose language.
Another difference with other BASICs of the era is that Integer BASIC treated strings as arrays of characters, similar to the system in C or Fortran 77. Substrings were accessed using array slicing rather than string functions. This style was introduced in HP Time-Shared BASIC, and could also be found in other contemporary BASICs patterned on HP, like North Star BASIC and Atari BASIC. It contrasted with the style found in BASICs derived from DEC, including Microsoft BASIC.
The language was initially developed under the name GAME BASIC and referred to simply as Apple BASIC when it was introduced on the Apple I. It became Integer BASIC when it was ported to the Apple II and shipped alongside Applesoft BASIC, a port of Microsoft BASIC which included floating-point support. Integer BASIC was phased out in favor of Applesoft BASIC starting with the Apple II Plus in 1979.

## History

As a senior in high school, Steve Wozniak's electronics teacher arranged for the leading students in the class to have placements at local electronics companies. Wozniak was sent to Sylvania where he programmed in FORTRAN on an IBM 1130. That same year, General Electric placed a terminal in the high school that was connected to one of their mainframes running their time-sharing BASIC service, which they were heavily promoting at the time. After being given three days of access, the students were asked to write letters on why the school should receive a terminal permanently, but their efforts were ultimately unsuccessful.
Some years later, Wozniak was working at Hewlett-Packard (HP) running simulations of chip designs and logic layout for calculators. HP made major inroads in the minicomputer market with their HP 2000 series machines running a custom timesharing version of BASIC. For approximately $100,000, one could build up a reasonably equipped machine that could support between 16 and 32 users running BASIC programs. While expensive, it was still a fraction of the cost of the mainframe machines and, for heavy users, less than the timesharing services. HP followed this with the HP 9830, a desktop-sized machine for US$10,000 (equivalent to $78,000 in 2023) that also ran BASIC, which Wozniak had access to.
   I sniffed the wind and knew that the key to making my computer good (popular) was to include a high-level language and that it had to be BASIC.
In January 1975 the Altair 8800 was announced and sparked off the microcomputer revolution. In March, Wozniak attended the first meeting of the Homebrew Computer Club and began formulating the design of his own computer. One of the most important pieces of software for the Altair, and one of the most heavily pirated, was Altair BASIC from the recently formed Microsoft. 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.
Wozniak's references for BASIC were a copy of 101 BASIC Computer Games and an HP BASIC manual. He did not know that HP's BASIC was very different from the DEC BASIC variety used in 101 Games, which was also the basis of Microsoft BASIC for the Altair. Based on these sources, Wozniak began sketching out a syntax chart for the language. The design initially included floating-point support, but still hoping he might publish the first BASIC on the 6502 and become "a star", he decided to abandon floating-point and write a separate integer math system to save a few weeks programming time.
Wozniak would later describe his language as "intended primarily for games and educational uses". Referring to it throughout development as "GAME BASIC", Wozniak wrote the code by hand, translating the assembler code instructions into their machine code equivalents and then uploading the result to his computer. Without any training on how to write a computer language, he used his HP calculator experience to implement a stack machine to interpret expressions. Once the basic routines were up and running, he worked on the other commands one-by-one in a modular fashion. With every visit to the Homebrew club, he demonstrated a few more features added in the last month.
   It was the most satisfying day of my life... I demonstrated Breakout [at Homebrew]-totally written in BASIC... After designing hardware arcade games, I knew that being able to program them in BASIC was going to change the world.
In early 1976 ads for its Apple I computer, Apple Inc made the claims that "our philosophy is to provide software for our machines free or at minimal cost" and "yes folks, Apple BASIC is Free". This was printed shortly after Bill Gates's infamous Open Letter to Hobbyists that suggested that people were robbing him by copying versions of Altair BASIC.
Wozniak had helped Steve Jobs, who worked for Atari, with a redesign of Breakout. At some later point, he decided to see whether one could write the game in BASIC. He added commands to read paddle controllers and over a series of quick edits had a version of the game up and running. To improve its playability, he added a speaker to make clicks when the ball hit things. While showing it to Jobs, Wozniak demonstrated that he could quickly change the colors that his game used, just by altering the source code. Wozniak later wrote that he had proved that "software was much more flexible than hardware", and that he and Jobs realized that "now, anyone could create arcade games without having to design it in hardware."
Wozniak did complete a floating-point library for the 6502 and published it in the August 1976 edition of Dr. Dobb's Journal. This library was later made part of the ROMs for the Apple II. Wozniak began work on back-porting the floating-point code into Apple BASIC, but got sidetracked in the task of designing a floppy disk controller for what became the Disk II. Mike Markkula said the company would go to the Consumer Electronics Show in Las Vegas if the disk system was ready in time, so Wozniak and Randy Wigginton worked on it non-stop through the 1977 holidays.
When he returned to the topic of floating-point in BASIC, Jobs complained it was taking too long. Without Wozniak being aware, the company had already arranged a license with Microsoft to receive their recently completed 6502 version of the Altair code. Examining the MS code, Wozniak decided that it was easier to add graphics support to their code than add floating-point his own BASIC, as the latter required hand-patching of the original machine code while MS's was written in assembler and more easily modified. The development of Apple's BASIC ended in favor of what became Applesoft BASIC. Wozniak later noted, "My biggest disappointment was going to the awful string functions like LEFT$(VAR, 5) and MID$(VAR2,5,3) instead of my own".
When the Apple II shipped in the summer of 1977, Integer BASIC was supplied in ROM, while Applesoft BASIC shipped on cassette. This changed with the introduction of the Apple II Plus in 1979, when Applesoft was put in the ROM.

## Description
### Program editing

Like most BASIC implementations of the era, Integer BASIC acted as both the language interpreter as well as the line editing environment. When BASIC was running, a > command prompt was displayed where the user could enter statements. Unlike later home computer platforms, BASIC was not the default environment when the Apple II started, it normally started in the monitor. BASIC was started by pressing Ctrl+BReturn.
Statements that were entered with leading numbers are entered into the program storage for "deferred execution", either as new lines or replacing any that might have had the same number previously. Statements that were entered without a line number were referred to as commands, and ran immediately. Line numbers could be from 0 to 32767, and lines could contain up to 128 characters.
Integer BASIC also included the AUTO command to automatically enter line numbers at a given starting number like AUTO 100, adding 10 to the last number with every new line. AUTO 300,5 would begin numbering at line 300 by fives; 300, 305, etc. Automatic numbering was turned off by entering MAN.
One interesting feature of the editor was that a section of the screen could be set aside as the "window", where live updates took place. This was normally the entire screen, but it could be limited to a smaller area by POKEing values into memory locations 32 through 35. This feature could be used to create an editable text area while the rest of the screen was in graphics mode.

### Debugging

As in most BASICs, programs were started with the RUN command, and as was common, could be directed at a particular line number like RUN 300. Execution could be stopped at any time using Ctrl+C and then restarted with CONtinue, as opposed to the more typical CONT.
For step-by-step execution, the TRACE instruction could be used at the command prompt or placed within the program itself. When it was turned on, line numbers were printed out for each line the program visited. The feature could be turned off again with NOTRACE.
A somewhat unusual feature was the DSP (for "display") command. When encountered in a program, from that point on any changes to a variable's value would be displayed. For instance, DSP X would display the value of X every time it changed, along with the line number where the change occurred. As with TRACE, DSP was turned off with NODSP.

### Variable names

Where Dartmouth BASIC and HP-BASIC limited variable names to at most two characters (either a single letter or a letter followed by one digit), and where MS-BASIC allowed a letter followed by an optional letter or digit (ignoring subsequent characters), Integer BASIC was unusual in supporting any length variable name (e.g., SUM, GAMEPOINTS, PLAYER2). The only caveat was that variable names could not contain reserved words; for example, THISCOLOR and COLORFUL were invalid variable names because they contained the keyword COLOR, a system command. Additionally, lines were limited to 128 characters, so variable names could not exceed that length.

### Mathematics

Integer BASIC, as its name implies, uses integers as the basis for its math package. These were stored internally as a 16-bit number, little-endian (as is the 6502). This allowed a maximum value for any calculation between -32767 and 32767; although the format could also store the value -32768, BASIC could not display that number. Calculations that resulted in values outside that range produced a >32767 ERR.
Infix operators included + (addition), - (subtraction), * (multiplication), / (division), MOD (remainder) and exponent using the ^ character. Binary operators included AND, OR and NOT. Binary comparisons included the standard set of =, >, <, >=, <=, <> and the HP-inspired #, which was equivalent to <>.
Only single-dimension arrays were allowed, limited in size only by the available memory. Mathematical functions were sparse; only ABS (absolute value), SGN (sign) and RND (random number) were supported. In contrast to MS-derived versions, where the parameter was ignored and RND always returned a value 0..<1, Integer BASIC used the parameter; RND(6) returned an integer from 0 to 5.

### Strings

Integer BASIC's string handling was based on the system in HP BASIC. This treated string variables as arrays of characters which had to be DIMed prior to use. This is similar to the model in C or Fortran 77. This is in contrast to MS-like BASICs where strings are an intrinsic variable-length type. Before MS-derived BASICs became the de facto standard, this style was not uncommon; North Star BASIC and Atari BASIC used the same concept, as did others.
Strings in Integer Basic used a fixed amount of memory regardless of the number of characters used within them, up to a maximum of 255 characters. This had the advantage of avoiding the need for the garbage collection of the heap that was notoriously slow in MS BASIC but meant that strings that were shorter than the declared length was wasted.
Substring access was provided through array slicing syntax. For instance, PRINT A$(0,5) printed the first six characters of A$, characters 0 through 5. Concatenation was provided using the same system, A$(5)="ABC" replaced any characters starting at position 5 with the string "ABC". This contrasts with the DEC/MS-style string handling which uses string functions like MID$ to access substrings and + for concatenation.
As many of the features that would be provided by string functions were instead provided by array slicing, the selection of string functions was reduced. LEN returned the length of a string and ASC returned the ASCII numeric code for the first letter in a string. It lacked an equivalent of the CHR$ that returned the ASCII character with a given numeric code.

### Graphics and sound

When launched, the only game controller for the Apple was the paddle controller, which had two controllers on a single connector. The position of the controller could be read using the PDL function, passing in the controller number, 0 or 1, like A=PDL(0):PRINT A, returning a value between 0 and 255.
The Apple machines did not include dedicated sound hardware, only a simple "beeper". Producing sounds was accomplished by PEEKing the memory-mapped location of the speaker, -16336. Repeatedly PEEKing that value produced tones, and the manual suggested using a mathematical expression to do this, like S=PEEK(-16336)-PEEK(-16336)-PEEK(-16336)-PEEK(-16336)-PEEK(-16336).
Support for graphics was more detailed. Graphics mode was turned on with the GR statement and off with TEXT. Drawing was modal and normally started by issuing a command to change the color, which was accomplished by setting a pseudo-variable; COLOR=12 would set the drawing color to 12, light green. One could then PLOT 10,10 to produce a single spot of that color, HLIN 0,39 AT 20 to draw a horizontal line at row 20 that spanned the screen, or VLIN 5,15 AT 7 to draw a shorter vertical line down column 7. A=SCRN X,Y returned the color of the screen at X,Y.

### Input/output

Integer BASIC lacked any custom input/output commands, and also lacked the DATA statement and the associated READ. To get data into and out of a program, the input/output functionality was redirected to a selected card slot with the PR#x and IN#x, which redirected output or input (respectively) to the numbered slot. From then on, data could be sent to the card using conventional PRINT commands and read from it using INPUT.

### Other notes

Integer BASIC included a TAB feature, which positioned the cursor on a given column from 0 to 39. It differed from the versions found in most BASICs in that it was a command with a following number, as opposed to a function with the value in parentheses; one would move the cursor to column 10 using TAB 10 in Integer BASIC whereas in MS this would be PRINT TAB(10). Additionally, the VTAB command worked similar to TAB but added vertical spaces instead of horizontal. For unexplained reasons, in this case the coordinates were from 1 to 24 rather than 0 to 23.
Integer BASIC included a POP command to exit from loops. This popped the topmost item off the FOR stack. Atari BASIC also supported the same command, while North Star BASIC used EXIT.
The Integer BASIC ROMs also included a machine code monitor, "mini-assembler", and disassembler to create and debug assembly language programs. Wozniak hand-assembled the monitor as the Apple II's first program, then used it to write Integer BASIC.

### Reserved words
#### Apple BASIC

Apple BASIC had the following commands:

#### Integer BASIC

Integer BASIC added the following:

## Implementation

Integer BASIC read the lines typed in by the user from a buffer and ran them through a parser which output a series of tokens. As part of this process, simple syntax errors were detected and listed. If the parsing was successful, the line number (if present) was converted from ASCII decimal format into a 16-bit integer and any keywords into a 7-bit integer token.
Some keywords were represented by multiple tokens; for instance, where Microsoft BASIC had one token for the keyword PRINT, Integer BASIC had three tokens: one if the keyword was followed by no arguments, one if followed by an arithmetic expression, and one if followed by a string literal.
Numeric literals, like the value 500, were converted into their 16-bit (two-byte) binary representation, in this case, $01F4 hexadecimal. To indicate this was a value and not a keyword, a single byte between $B0 and $B9 was inserted in front of the two-byte value. String literals, like "HELLO WORLD" were instead converted by setting the high bit of each character so that A was stored as $C1. Variable names were converted in the same fashion, with the letters converted to have their high-bit turned on, and any digits in the name represented by the corresponding $B0 through $B9, so that the variable A5 would be tokenized as $C1B5.
If the line was entered without a line number, the code was then executed directly from the buffer. If it had a line number, it was copied from the buffer into the program storage area.
The runtime interpreter used two stacks for execution: one for statement keywords and the other for evaluating the parameters. Each statement was given two priorities: one that stated where it should occur in a multi-step operation, like a string of mathematical operations to provide order of operations, and another that suggested when evaluation should occur, for instance, calculating internal values of a parentheses formula. When variables were encountered, their name was parsed and then looked up in the variable storage area. If it was not found, it was added to the end of the list. The address of the variable's storage, perhaps freshly created, was then placed on the evaluation stack.

## ROM details
### SWEET16

In addition to Integer BASIC, the Apple ROMs contained a custom assembler language known as SWEET16. SWEET16 is based on bytecodes that run within a simple 16-bit virtual machine. This model was used so memory could be addressed via indirect 16-bit pointers and 16-bit math functions calculated without the need to translate those to the underlying multi-instruction 8-bit 6502 code. The entire virtual machine was written in only 300 bytes. Code can call SWEET16 by issuing a subroutine call, and then return to normal 6502 code when the 16-bit operations are complete.
SWEET16 was not used by the core BASIC code, but was later used to implement several utilities. Notable among these was the line renumbering routine, which was included in the Programmer's Aid #1 ROM, added to later Apple II models and available for user installation on earlier examples.

### Floating point

Although Integer BASIC contained its own math routines, the Apple II ROMs also included a complete floating-point library located in ROM memory between $F425-$F4FB and $F63D-$F65D. The source code was included in the Apple II manual. BASIC programs requiring floating-point calculations could CALL into these routines.

## Performance

Because Integer BASIC processed more of the original source code into tokens, the runtime was faster than versions that required additional runtime parsing. For comparison, Tiny BASIC tokenized only the line number, while MS BASICs tokenized only the keywords. So for instance, while Integer BASIC would convert the line 100 GOTO 200 entirely into tokens that could be immediately read and performed, in MS BASIC only the line number and GOTO would be tokenized, the "200" was left in its original ASCII format and had to be re-parsed into a 16-bit integer every time the line was encountered.
Additionally, working solely with integer math provides another major boost in speed. This is due both to the smaller 16-bit format requiring fewer memory accesses, as well as removing the need to move the floating-point decimal after calculations. As many computer benchmarks of the era were small and often performed simple math that did not require floating-point, Integer BASIC trounced most other BASICs.
On one of the earliest known microcomputer benchmarks, the Rugg/Feldman benchmarks, Integer BASIC was well over twice as fast as Applesoft BASIC on the same machine. In the Byte Sieve, where math was less important but array access and looping performance dominated, Integer BASIC took 166 seconds while Applesoft took 200. A review of Galfo Systems' Integer BASIC Compiler found that it produced code that is about 1.5 to eight times faster than compiled Applesoft BASIC, ten times faster than interpreted Integer BASIC, and 15 times faster than interpreted Applesoft. Integer BASIC did not appear in the Creative Computing Benchmark, which was first published in 1983, by which time the language was no longer included with Apple computers.
The following test series, taken from both of the original Rugg/Feldman articles, show Integer's performance relative to the MS-derived BASIC on the same platform.
| System | CPU | BASIC | Test 1 | Test 2 | Test 3 | Test 4 | Test 5 | Test 6 | Test 7 |
| Apple II | 6502 @ 1 MHz | Integer BASIC | 1.3 | 3.1 | 7.2 | 7.2 | 8.8 | 18.5 | 28.0 |
| Apple II | 6502 @ 1 MHz | Applesoft BASIC | 1.3 | 8.5 | 16.0 | 17.8 | 19.1 | 28.6 | 44.8 |
Here is a summary of what each test did:
* Test 1: for/next loop to 1000.
* Test 2: loop with compare to 1000.
* Test 3: same as 2 with added multiplication, division, addition subtraction by same variable.
* Test 4: same as 3 with added multiplication, division, addition subtraction by constants.
* Test 5: same as 4 with added subroutine call.
* Test 6; same as 5 with added inner loop.
* Test 7: same as 6 with added table population.

## Sample code

The following is a version of Breakout written in the 1977 version of Integer BASIC for the Apple II, which was listed in the Apple II Mini Manual. There are a number of known bugs in this version.
The program starts by setting the display to TEXT and then CALL -936 to clear the screen. Lines 20 through 27, and the associated subroutines at line 100 and 200, are the color selection code Wozniak demonstrated for Jobs. Line 30 sets up the text window with POKE 32,20 and then uses a series of COLOR and VLIN statements to draw the playfield and the score display in the text window. The entire main loop runs from line 40 through 90 with associated subroutines. Another large amount of code near the end of the program is concerned with printing the final score. Other notes of interest include the # (not-equal) comparisons on line 20, the production of a high-pitch sound using a string of PEEKs on line 65 compared to a lower-pitched tone using a loop on line 70, and the mix of graphics and text on a single display.