Atari BASIC
Encyclopedia
Atari BASIC is a BASIC interpreter for the Atari 8-bit family
Atari 8-bit family
The Atari 8-bit family is a series of 8-bit home computers manufactured from 1979 to 1992. All are based on the MOS Technology 6502 CPU and were the first home computers designed with custom coprocessor chips...

 of 6502
MOS Technology 6502
The MOS Technology 6502 is an 8-bit microprocessor that was designed by Chuck Peddle and Bill Mensch for MOS Technology in 1975. When it was introduced, it was the least expensive full-featured microprocessor on the market by a considerable margin, costing less than one-sixth the price of...

-based home computer
Home computer
Home computers were a class of microcomputers entering the market in 1977, and becoming increasingly common during the 1980s. They were marketed to consumers as affordable and accessible computers that, for the first time, were intended for the use of a single nontechnical user...

s. The interpreter originally shipped on an 8 KB
Kilobyte
The kilobyte is a multiple of the unit byte for digital information. Although the prefix kilo- means 1000, the term kilobyte and symbol KB have historically been used to refer to either 1024 bytes or 1000 bytes, dependent upon context, in the fields of computer science and information...

 cartridge; on later XL/XE model computers it was built in, with an option to disable it, and started when the machines were booted
Booting
In computing, booting is a process that begins when a user turns on a computer system and prepares the computer to perform its normal operations. On modern computers, this typically involves loading and starting an operating system. The boot sequence is the initial set of operations that the...

 with no other cartridges in place. The complete commented source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...

 and design specifications of Atari BASIC were published as a book in 1983. This marked the first time source code was made available for a commercial language.

Nomenclature and symbols

In the nomenclature of the time when these machines were designed, "K" was taken to mean one kilobyte
Kilobyte
The kilobyte is a multiple of the unit byte for digital information. Although the prefix kilo- means 1000, the term kilobyte and symbol KB have historically been used to refer to either 1024 bytes or 1000 bytes, dependent upon context, in the fields of computer science and information...

, so that is how it is expressed here. Similarly, further, on the family of processors use by Atari machines, in the assembly language "$" introduced a hexadecimal
Hexadecimal
In mathematics and computer science, hexadecimal is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F to represent values ten to fifteen...

 number, or it was suffixed subscripted with its radix
Radix
In mathematical numeral systems, the base or radix for the simplest case is the number of unique digits, including zero, that a positional numeral system uses to represent numbers. For example, for the decimal system the radix is ten, because it uses the ten digits from 0 through 9.In any numeral...

, so, for example, "" is "12810", "$80", or "8016". If a number is expressed with no radix, decimal (10) is assumed, and a leading 0 does not imply octal
Octal
The octal numeral system, or oct for short, is the base-8 number system, and uses the digits 0 to 7. Numerals can be made from binary numerals by grouping consecutive binary digits into groups of three...

. These are used instead of more modern conventions because they appear as such in many of the sources and will be seen often by those using simulators and so forth.

Background

The machines that would become the Atari 8-bit family
Atari 8-bit family
The Atari 8-bit family is a series of 8-bit home computers manufactured from 1979 to 1992. All are based on the MOS Technology 6502 CPU and were the first home computers designed with custom coprocessor chips...

 had originally been developed as second-generation games consoles intended to replace the Atari 2600
Atari 2600
The Atari 2600 is a video game console released in October 1977 by Atari, Inc. It is credited with popularizing the use of microprocessor-based hardware and cartridges containing game code, instead of having non-microprocessor dedicated hardware with all games built in...

. Ray Kassar, the new president of Atari, decided to challenge Apple Computer
Apple Computer
Apple Inc. is an American multinational corporation that designs and markets consumer electronics, computer software, and personal computers. The company's best-known hardware products include the Macintosh line of computers, the iPod, the iPhone and the iPad...

 by building a home computer instead. This meant Atari needed the BASIC
BASIC
BASIC is a family of general-purpose, high-level programming languages whose design philosophy emphasizes ease of use - the name is an acronym from Beginner's All-purpose Symbolic Instruction Code....

 programming language, then the standard language for home computers.

Atari did what many of the other home computer companies did: they purchased the source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...

 to the MOS 6502 version of Microsoft 8K BASIC
Microsoft BASIC
Microsoft BASIC was the foundation product of the Microsoft company. It first appeared in 1975 as Altair BASIC, which was the first BASIC, and the first high level programming language available for the MITS Altair 8800 hobbyist microcomputer....

, intending to port
Porting
In computer science, porting is the process of adapting software so that an executable program can be created for a computing environment that is different from the one for which it was originally designed...

 it to run on their new machines. But the name was something of a misnomer, as the 8K referred to its original size on the Intel 8080
Intel 8080
The Intel 8080 was the second 8-bit microprocessor designed and manufactured by Intel and was released in April 1974. It was an extended and enhanced variant of the earlier 8008 design, although without binary compatibility...

's instruction set
Instruction set
An instruction set, or instruction set architecture , is the part of the computer architecture related to programming, including the native data types, instructions, registers, addressing modes, memory architecture, interrupt and exception handling, and external I/O...

. The lower code density of the 6502 expanded the code to about 9 kB.

Atari felt that they needed to expand the language to add better support for the specific hardware features of their computers, similar to what Apple had done with their Applesoft BASIC
Applesoft BASIC
Applesoft BASIC was a dialect of Microsoft BASIC supplied with the Apple II series of computers. It superseded Integer BASIC and was the BASIC in ROM in all Apple II series computers after the original Apple II model. It was also referred to as FP because of the command used to invoke it instead...

. This increased the size from 9K to around 11K. Atari had designed their ROM layout in 8K b/locks, and paring down the code from 11K to 8K turned out to be a significant problem. Adding to the problem was the fact that the 6502 code supplied by Microsoft was undocumented.

Six months later, they were almost ready with a shippable version of the system. But Atari had a deadline with the Consumer Electronics Show
Consumer Electronics Show
The International Consumer Electronics Show is a major technology-related trade show held each January in the Las Vegas Convention Center, Las Vegas, Nevada, United States. Not open to the public, the Consumer Electronics Association-sponsored show typically hosts previews of products and new...

 (CES) approaching and decided to ask for help.

Shepardson Microsystems

In September 1978 Atari asked Shepardson Microsystems
Shepardson Microsystems
Shepardson Microsystems, Inc. was a small company producing operating systems and programming languages for the Atari 8-bit and Apple II computer families...

 (SMI) to bid on completing BASIC. Shepardson had written a number of programs for the Apple II family, which used the same 6502 processor, and were in the middle of finishing a new BASIC for the Cromemco
Cromemco
Cromemco was a Mountain View, California microcomputer company known for its high-end Z80-based S-100 bus computers in the early days of the home computer revolution. The Cromemco Dazzler was the first color graphics card available for personal computers....

 S-100 bus
S-100 bus
The S-100 bus or Altair bus, IEEE696-1983 , was an early computer bus designed in 1974 as a part of the Altair 8800, generally considered today to be the first personal computer...

 machines (Cromemco 32K Structured BASIC). SMI examined the existing work and decided it was too difficult to continue paring it down; instead they recommended developing a completely new version that would be easier to fit into 8K. Atari accepted the proposal, and when the specifications were finalized in October 1978, Paul Laughton and Kathleen O'Brien began work on the new language.

The result was a rather different version of BASIC, known as ATARI BASIC. In particular, the new BASIC dealt with character strings more like Data General
Data General
Data General was one of the first minicomputer firms from the late 1960s. Three of the four founders were former employees of Digital Equipment Corporation. Their first product, the Data General Nova, was a 16-bit minicomputer...

's BASIC than Microsoft's, which used strings similar to those from DEC BASIC.

The contract specified a delivery date on or before April 6, 1979 and this also included a File Manager System (later known as DOS 1.0). Atari's plans were to take an early 8K version of Microsoft BASIC to the 1979 CES and then switch to the new Atari BASIC for production. Because of a bonus clause
Bonus clause
A bonus clause is a clause in a contract that rewards the contractor for doing more than the letter of the contract; particularly, to finish the job early...

 in the contract, development proceeded quickly and an 8K cartridge was available just before the release of the machines. Because Atari BASIC was delivered before Microsoft BASIC, Atari took it with them to the CES.

Releases

Shepardson's programmers found problems during the first review and managed to fix some of them, but Atari had already committed ATARI BASIC to manufacturing, and the technology of the time did not permit changes. So it was manufactured with known bugs, and became known (as a retronym
Retronym
A retronym is a type of neologism that provides a new name for an object or concept to differentiate the original form or version of it from a more recent form or version. The original name is most often augmented with an adjective to account for later developments of the object or concept itself...

) Revision A.
  • Revision A – First Atari BASIC cartridge. 8K ROM. This version contained a bug in a subroutine that copied memory. Under certain conditions, deleting lines of code led to a crash condition known as the "2-line lockup".
  • Revision B – Fixed all of the major software bug
    Software bug
    A software bug is the common term used to describe an error, flaw, mistake, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or causes it to behave in unintended ways. Most bugs arise from mistakes and errors made by people in either a program's...

    s in Revision A. While fixing the 2-line bug they re-introduced the same problem in a more common routine, thereby dramatically increasing the number of crashes. Found built-in on the 600XL and early 800XLs. Never supplied on cartridges.
  • Revision C – Eliminated memory leak in Revision B. Found on later 800XLs, the 800XLF, XEGS and all XE computers. Limited cartridge production run.


A BASIC programmer can find out the version by examining a well-known location in memory. Entering the command PRINT PEEK(43234) at the READY prompt will give a result of 162 for Revision A, 96 for Revision B, and 234 for Revision C.

Program editing

Atari BASIC uses a line editor
Line editor
A line editor is a text editor computer program that manipulates text primarily by the display, modification, and movement of lines. Line editors precede screen-based text editors and originated in an era when a computer operator typically interacted with a teleprinter , with no video display, and...

, like most BASICs of the era. Unlike many BASICs, however, Atari BASIC immediately checks the line for syntax error
Syntax error
In computer science, a syntax error refers to an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language....

s. If a problem is found it shows the line again, highlighting the text near the error in inverse video. This can make catching syntax errors on the Atari much easier than on other editors; most BASICs will not display the errors until the program is executed. (The Sinclair ZX family of machines also adopts the approach of checking each line as it was entered, although it differs by not even allowing the line to be entered until it is syntactically correct, which can be a hindrance to the programmer when writing a line of code but wanting to look up something elswehere in the program.)

When not running a BASIC program, the Atari is in direct mode
Direct mode
Direct mode, also known as immediate mode is a computing term referring to the input of textual commands outside the context of a program. The command would be executed immediately and the results printed on screen, in contrast to programming mode where nothing would be executed until a specific...

 or immediate mode. Program lines can be entered by starting with a line number, which will insert a new line or amend an existing one. Lines without a line number are executed straight away, hence the name immediate mode.

When the programmer types RUN the program executes from the first statement. It can also be given a line number, so that RUN 2000 will run the program from line 2000, or the first line after that if line 2000 does not exist.

Unlike most other BASICs, Atari BASIC allows all commands to be executed in both modes. For instance most BASICs only allow LIST to be used in immediate mode, while Atari BASIC also allows it to be used inside a program. This is sometimes used as part of a way to produce self modifying code.

Program lines ("logical lines") can be up to three screen lines ("physical lines") of 40 characters, so 120 characters total. The cursor can be moved freely in these lines, unlike in other BASICs where to get "up" a line one has to continuously scroll leftwards until the cursor is wrapped at the left margin (and similarly to go down when wrapping at the right margin) – though that works too, except the cursor when wrapping left to right or right to left does not move up or down a line. The OS handles tracking whether a physical line flowed to the next on the same logical line; the three-line limit is fairly arbitrary but keeps lines below 128 characters and so reduces the chances of buffer overflow
Buffer overflow
In computer security and programming, a buffer overflow, or buffer overrun, is an anomaly where a program, while writing data to a buffer, overruns the buffer's boundary and overwrites adjacent memory. This is a special case of violation of memory safety....

.

The cursor can always be moved freely around the screen, and it will wrap on all sides. Hitting "ENTER" will send the tokenizer the (logical) line on which the cursor sits. So, in the example pictured above (with "PRUNT"), all the author needs to do to fix the error is move the cursor over the "U", type (the editor only has an overwrite mode) and hit .

This is quite a frequent editing technique for, say, renumbering lines. Atari BASIC has no built-in renumbering command, but one can quickly learn to overwrite the numbers on a set of lines then just hit "ENTER" repeatedly to put them back into the program. Indeed, a slightly cryptic but essentially simple idiom allows this to be done by the program itself as it is running, producing self-modifying code
Self-modifying code
In computer science, self-modifying code is code that alters its own instructions while it is executing - usually to reduce the instruction path length and improve performance or simply to reduce otherwise repetitively similar code, thus simplifying maintenance...

. This is not an artifact or cheat around the system but inherent in the combined behavior of the editor and tokenizer.

Character set

The Atari variation on ASCII
ASCII
The American Standard Code for Information Interchange is a character-encoding scheme based on the ordering of the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that use text...

, called ATASCII
ATASCII
The ATASCII character set, from ATARI Standard Code for Information Interchange, alternatively ATARI ASCII, is the variation on ASCII used in the Atari 8-bit family of home computers. The first of this family were the Atari 400 and 800, released in 1979, and later models were released throughout...

, has 128 (8016) characters mostly corresponding to ASCII but with a few exceptions. All characters have printable forms unlike ASCII where codes 0-31 (0-1F16) are "control codes" that perform special functions such as requesting a paper feed or ringing an attention bell. Characters 128-255 (8016-FF16) are displayed as the inverse video of characters 0-127 (0016-7F16). Variable names must be composed of upper-case alphabetic (65-90, 4116-5A16) and numeric (48-57, 3016-3916) characters, starting with an alphabetical character, and for strings terminating with a dollar sign (36, 2416).

The character set has a full ensemble of lower case characters and some graphics characters, but it is frequent to see programs mostly in upper case. This may be simply because of conventions, previous machines such as the Apple II series
Apple II series
The Apple II series is a set of 8-bit home computers, one of the first highly successful mass-produced microcomputer products, designed primarily by Steve Wozniak, manufactured by Apple Computer and introduced in 1977 with the original Apple II...

 not having lower case; or that some devices such as daisy wheel printer
Daisy wheel printer
Daisy wheel printers use an impact printing technology invented in 1969 by David S. Lee at Diablo Data Systems. It uses interchangeable pre-formed type elements, each with typically 96 glyphs, to generate high-quality output comparable to premium typewriters such as the IBM Selectric, but two to...

s may not have lower case; or that the lower-case font is not very attractive, in particular struggling to squeeze ascenders and descender
Descender
In typography, a descender is the portion of a letter that extends below the baseline of a font. The line that descenders reach down to is known as the beard line....

s into the 8×8 fixed grid used to define each glyph
Glyph
A glyph is an element of writing: an individual mark on a written medium that contributes to the meaning of what is written. A glyph is made up of one or more graphemes....

, or the 8×7 fonts used on many dot matrix printer
Dot matrix printer
A dot matrix printer or impact matrix printer is a type of computer printer with a print head that runs back and forth, or in an up and down motion, on the page and prints by impact, striking an ink-soaked cloth ribbon against the paper, much like the print mechanism on a typewriter...

s such as Atari's own.

On Revision C ROMs there is an alternative font including characters with diacritic
Diacritic
A diacritic is a glyph added to a letter, or basic glyph. The term derives from the Greek διακριτικός . Diacritic is both an adjective and a noun, whereas diacritical is only an adjective. Some diacritical marks, such as the acute and grave are often called accents...

s, intended for European users, but this is rarely used. Adding user-defined fonts is relatively easy and each takes 1 kilobyte (128 characters × 8 bits high × 8 bits wide).

The ANTIC chip uses one byte to indicate the start page of a font (the memory consisting of 256 pages of 256 bytes). But only one font can be used at a time without machine code display list interrupts
Raster interrupt
A raster interrupt is a computer interrupt signal that is used for display timing purposes. It is usually, though not always, generated by the system's graphics chip....

 to change the font midway down the screen. Similarly on later GTIA graphics processors, a little-known 8×10 font mode exists, where two lines top or bottom of a character are assumed blank, thus keeping the actual glyphs to 8×8 but allowing ascenders or descenders without these characters touching those above or below; this is very rarely used, partly because dot matrix printers can not easily support it. The ease of implementing other fonts means many are freely available, with font editors and so forth too.

The tokenizer

Like most BASIC interpreters, Atari BASIC uses a token structure to handle lexical
Lexical analysis
In computer science, lexical analysis is the process of converting a sequence of characters into a sequence of tokens. A program or function which performs lexical analysis is called a lexical analyzer, lexer or scanner...

 processing for better performance and reduced memory size. The tokenizer converts lines using a small buffer in memory, and the program is stored as a parse tree
Parse tree
A concrete syntax tree or parse tree or parsing treeis an ordered, rooted tree that represents the syntactic structure of a string according to some formal grammar. In a parse tree, the interior nodes are labeled by non-terminals of the grammar, while the leaf nodes are labeled by terminals of the...

. The token output buffer (addressed by a pointer at LOMEM – 80, 8116) is 1 page (256 bytes) long, and any tokenized statement that is larger than the buffer will generate a BASIC error (14 – line too long). Indeed, the syntax checking described in the "Program editing" section is a side effect of converting each line into a tokenized form before it is stored. Sinclair BASIC
Sinclair BASIC
Sinclair BASIC is a dialect of the BASIC programming language used in the 8-bit home computers from Sinclair Research and Timex Sinclair...

 uses a similar approach, though it varies between models.

The output from the tokenizer is then moved into more permanent storage in various locations in memory. A set of pointers (addresses) indicates these locations: variables are stored in the variable name table (pointed to at VNTP – 82, 8316) and the values are stored in the variable value table (pointed to at VVTP – 86, 8716). Strings have their own area (pointed to at STARP – 8C, 8D16) as does the runtime stack (pointed to at RUNSTK – 8E, 8F16) used to store the line numbers of looping statements (FOR...NEXT) and subroutines (GOSUB...RETURN). Finally, the end of BASIC memory usage is indicated by an address stored at MEMTOP – 90, 9116) pointer.

By indirecting
Indirection
In computer programming, indirection is the ability to reference something using a name, reference, or container instead of the value itself. The most common form of indirection is the act of manipulating a value through its memory address. For example, accessing a variable through the use of a...

 the variable names in this way, a reference to a variable needs only two bytes to address its entry into the appropriate table; the whole name does not need to be stored each time. This also makes variable renaming relatively trivial if the program is in storage, as it is simply a case of changing the single instance of its name in the table and the only difficulty is if the name changes length (and even then, only if it gets longer): indeed, obfuscated code
Obfuscated code
Obfuscated code is source or machine code that has been made difficult to understand for humans. Programmers may deliberately obfuscate code to conceal its purpose or its logic to prevent tampering, deter reverse engineering, or as a puzzle or recreational challenge for someone reading the source...

 can be produced for a finished program by renaming variables in the name tables – possibly all to the same name. This doesn't confuse the interpreter since internally it is using the index values not the names. Of course, new code will be difficult to add because the tokenizer has to search the name table to find a variable's index, and can get confused if names are not unique (though it is OK to have names in both the "string" and "variable" namespaces, e.g. HELLO = 10 and HELLO$ = "WORLD", because they have separate tables, that is to say, separate namespace
Namespace
In general, a namespace is a container that provides context for the identifiers it holds, and allows the disambiguation of homonym identifiers residing in different namespaces....

s.)

Atari BASIC uses a unique way to recognize abbreviated reserved words. In Microsoft BASIC there are a few predefined short forms, (like ? for PRINT and ' for REM). Atari BASIC allows any keyword to be abbreviated using a period, at any point in writing it. So L. will be expanded to LIST, as will LI. and (redundantly) LIS.. To expand an abbreviation the tokenizer will search through its list of reserved words and find the first that matches the portion supplied. To improve the chance of a programmer's correctly guessing an abbreviation, to save typing, and to improve the speed of the lookup, the list of reserved words is sorted to place the more-commonly used commands first. REM is at the very top, and can be typed in just as .. This also speeds lexical analysis generally, since although the time to search is in theory proportional to the length of the list, in practice it will find common keywords very quickly, to the extent that good programmers know when a line is syntactically incorrect even before the parser says so, because the time taken to search the list to find it wanting gives an indication that something is wrong.

So whereas Microsoft BASIC uses separate tokens for its few short forms, ATARI BASIC has only one token for each reserved word – when the program is later LISTed it will always write out the full words (since only one token represents all possible forms, it can do no other). There are two exceptions to this: PRINT has a synonym, ?, and LET has a synonym which is the empty string (so 10 LET A = 10 and 10 A = 10 mean the same thing). These are separate tokens, and so will remain as such in the program listing.

Some other contemporary BASICs have variants of keywords that include spaces (for example GO TO). Atari BASIC does not. The main exception here is keywords for communicating with peripheral
Peripheral
A peripheral is a device attached to a host computer, but not part of it, and is more or less dependent on the host. It expands the host's capabilities, but does not form part of the core computer architecture....

s (see the "Input/Output" section, below) such as OPEN # and PRINT #; it rarely occurs to many programmers that the " #" is actually part of the tokenized keyword and not a separate symbol; and that for example "PRINT" and "PRINT #0" are the very same thing, just presented differently. It may be that the BASIC programmers kept the form # to conform with other BASICs (The syntax derives from Fortran
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...

), though it is entirely unnecessary, and probably a hindrance, for tokenizing, and is not used in other languages for the Atari 8-bit family.

Expanding tokens in the listing can cause problems when editing. The Atari line input buffer is three lines (120 characters); up to three "physical lines" make one "logical line". After that a new "logical line" is automatically created. This doesn't matter much for output but it does for input, because the operating system will not return characters to the tokenizer after the third line, treating them as the start of a new "logical line". (The operating system keeps track of the mapping between physical and logical lines as they are inserted and deleted; in particular it marks each physical line with a flag for being a continuation line or a new logical line.) But using abbreviations when typing in a line can result, once they have been expanded on output, to a line that is longer than three lines and, a more minor concern, some whitespace characters can be omitted on input, so for example PRINT"HELLO" will be listed as PRINT "HELLO", one character longer. If one then wants to edit the line, now split across two logical lines, one must replace the expanded commands back with their abbreviations to be submit them back to the tokenizer. The moral of this is, generally, don't try to squeeze more out of a line than is reasonable, or, in the alternate, obfuscate variables by makinggggggthemverrrryverrrrylongindeed.

Literal line numbers in statements such as GOTO are calculated at run time using the same floating-point mathematical routines as other BASIC functions. This calculation allows subroutine
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

s to be referred to by variables: for instance GOTO EXITOUT is as good as GOTO 2000, if one sets EXITOUT to 2000. This is much more useful than it might sound; literals are stored in the 6-byte floating-point variable format, but variables are stored as a two-byte pointer to their place in the variable value table, at VVTP, so by using a variable the GOTO target is just two bytes instead of six. Of course the actual number takes another six, but this is only stored once, so if the same line number is used more than twice, one saves some memory, though the line number takes a little longer to look up. (The design choice to store what can only legally be integers as floating-point numbers is discussed below.)

It is quite common in BASIC to use GOTO or GOSUB to a line number, so real savings can be made to replace these numbers with variables. It also means that if the programmer is careful always to use the variable and not the literal, subroutines can be easily renumbered (moved around in the program), because only the variable value needs to be changed. This makes it common to see, at the start of an Atari BASIC program, a sequence of LET statements assigning line numbers to variables.

String handling

Atari BASIC differs dramatically from Microsoft-style BASICs in the way it handles strings. In BASICs following the Microsoft model, strings are special types that allow for variable length and various operations. Atari BASIC has no strings of this sort, instead using arrays of characters, rather like Fortran
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...

. This allowed the BASIC language programmers to remove all the special-purpose code needed for handling dynamic resizing of strings, reusing instead the code already being used to handle arrays of numbers. A string is allocated a maximum size using the DIM statement, although its actual length can vary at runtime from 0 to this maximum size.

Of course, strings are not used by end programmers in the same way as arrays of numbers – at least not normally – so Atari BASIC also includes a selection of commands for "slicing" up arrays. A$ refers to the entire string, whereas A$(4,6) "slices" out the three characters 4, 5 and 6. In theory, this is a more elegant solution than Microsoft BASIC's LEFT$, MID$, and RIGHT$ solution, as this syntax replaces three separate commands with a single one.

Although this simplification reduces the size of Atari BASIC and offers some theoretical performance benefits, it is also a hindrance to porting BASIC programs from other computers to the Atari. When the Atari was first produced it was the norm for programs to be provided as listings in magazines for programmers to type in. They would have to scan them for instances of LEFT$, RIGHT$ and so on, do some mental arithmetic and replace them with slicing commands. Because strings were allocated a fixed size it generally means that programmers will pessimize or guesstimate
Guesstimate
Guesstimate is an informal English contraction of guess and estimate, first used by American statisticians in 1934 or 1935. It is defined as an estimate made without using adequate or complete information, or, more strongly, as an estimate arrived at by guesswork or conjecture...

 the likely maximum size, allocating, perhaps 256 bytes for a string that only ever stores someone's first name.

Strings in Atari BASIC cannot themselves be members of arrays, so arrays of strings have to be implemented by the programmer. Strings can move around in memory, so it is not generally possible for example to store their memory addresses in an array. For short strings of approximately the same length, instead an array is generally built using padding so that the strings are all the same length and the nth string in the array is n×l characters into it, where l is the length of the string. According to Bill Wilkinson, the chief programmer at SMI, it was unfeasible to implement string arrays with strings that were larger than one page (256 characters).

CIO overview

The Atari OS
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

 includes a subsystem for peripheral
Peripheral
A peripheral is a device attached to a host computer, but not part of it, and is more or less dependent on the host. It expands the host's capabilities, but does not form part of the core computer architecture....

 device input/output (I/O) known as CIO (Central Input/Output). All I/O went through a central point of entry (E45C16) passing the address of an I/O Control Block (IOCB), a 16-byte structure that defines which device was meant, and what kind of operation (read, write, seek etc.). There are 8 such IOCBs, allocated at fixed locations in page 3 of memory from 38016 to 3FF16.

Most progams therefore can be written independently
Device independent
A program or file is device independent when its function is universal on different types of device.For the World Wide Web, this means writing simple common denominator Hypertext Markup Language and Cascading Style Sheets so that most Web user agents on most devices can render it acceptably.For...

 of what device they might use, as they all conform to a common interface – this was very rare on home computers when Atari BASIC was first made. virtual
Virtuality (software design)
Virtuality is a term used by Ted Nelson for what he considers the central issues of software design. "Virtuality" refers to the seeming of anything, as opposed to its reality. . Everything has a reality and a virtuality. Nelson divides virtuality into two parts: conceptual structure and feel...

 devices such as the screen, S: and the editor, E: did have special operations, for example to draw graphics or to ask for line input (in fact E: was pretty much a combination of S: and the keyboard, K:), but these were done in a uniform way and new device drivers could be written fairly easily that would automatically be available to ATARI BASIC and indeed any other program using the Atari OS, for example to provide support for new hardware devices such as mouse pointers, or software devices such as an 80-column display (using typically a 4×8 pixel font). Existing drivers could be supplanted or augmented by new ones since the driver table was searched newest-to-oldest, so a replacement E:, for example could displace the one in ROM to provide an 80-column display, or to piggy-back on it to generate a checksum
Checksum
A checksum or hash sum is a fixed-size datum computed from an arbitrary block of digital data for the purpose of detecting accidental errors that may have been introduced during its transmission or storage. The integrity of the data can be checked at any later time by recomputing the checksum and...

 whenever a line was returned – this technique is used for some of the program listing checkers that provide a checksum for each line.

CIO access in BASIC

Atari BASIC supports CIO access with reserved words OPEN #, CLOSE #, PRINT #, INPUT #, GET #, PUT #, NOTE #, POINT # and XIO #. There are routines in the OS for graphics fill and draw, but they are not available as specific BASIC keywords (e.g. DRAW or FILL, as in other BASICs), but can be got at through the general CIO entry point, which has the BASIC command XIO.

It is odd that a machine that is superlatively endowed with graphics capabilities did not expose them to BASIC, when they are already there in the hardware and the OS. One might assume the BASIC programmers ran out of time or space to add them to the BASIC tokenizer: they are available in variants such as Turbo-BASIC XL
Turbo-Basic XL
Turbo-Basic XL is an advanced version of BASIC for the Atari 8-bit family of home computers. It is a compatible superset of the more common ATARI BASIC whose most important feature is the vastly improved execution speed...

, and in other languages.

Up to eight IOCBs can be in use at a time, numbered 0 through 7 (0 was, by default, the editor E:). The BASIC statement OPEN # was used to prepare a device for I/O access:

REM Opens the cassette device on channel 1 for reading in BASIC
OPEN #1,4,0,"C:MYPROG.DAT"

Here, OPEN # means "ensure channel 1 is free" (an error otherwise results), call the C: driver to prepare the device (this will set the cassette tape spools onto tension and advance the heads keeping the cassette tape player "paused"; the 4 means "for read" (other codes were 8 for write, 12 = 8 + 4 for "read-and-write", and so forth), and the third number provides extra auxiliary information, here not used and set by convention to 0. The C:MYPROG.DAT is the name of the device and the filename, as it happens, files on cassette were not named by this device. The string gives the device name and optionally a filename. Physical devices can have numbers (mainly disks, printers and serial devices), so "P1:" might be the plotter and "P2:" the daisy-wheel printer, or "D1:" may be one disk drive and "D2:" another, "R1:" may be a modem and "R2:" an oscilloscope (R for RS-232
RS-232
In telecommunications, RS-232 is the traditional name for a series of standards for serial binary single-ended data and control signals connecting between a DTE and a DCE . It is commonly used in computer serial ports...

, provided by an add-on interface and not built into the OS), and so on; if not present, 1 is assumed.

Reserved IOCBs in Atari BASIC

ATARI BASIC disallows access to IOCB 0 (the editor, E:) and reserves IOCB 7 for printing and cassette operations using the built-in commands LPRINT, SAVE, LOAD, CSAVE, CLOAD, though there is nothing to stop printers or the cassette being used on other channels too. IOCB 6 is used for accessing the graphics screen device (S:) for drawing lines, filling shapes and so on. SAVE and LOAD output the compact tokenized form of the BASIC program, LIST and ENTER output and input the text source, just as if they were being sent to or from the editor.

For the other CIO functions, Atari BASIC uses the XIO statement. This just primes an IOCB and calls the CIO entry point; any of the other commands (PRINT, INPUT and so on) can be achieved with the more general form XIO.

But the form of XIO is not very friendly for BASIC users, and it is mostly used for unusual functions that are specific to a particular device. For example, an M: device exists called "Multi-Mouse" that allows an Atari ST mouse, 8-bit trakball, touch tablet, or joystick, to be treated as a device whereby the position of the mouse cursor is set or got with NOTE and POINT commands. It should be remembered here that POINT does not mean, as it does in many BASICs, draw a point on the screen, but point a IOCB channel at a specific place in a file. In Atari DOS the two parameters to NOTE and POINT are the disk sector and offset, which is not very portable. In SpartaDOS they make up the offset from the start of the file. In the Multi-Mouse driver M:, they are the X and Y position of the mouse cursor.

Error Handling

I/O routines returned error codes of 128-255 (8016-FF16) via the processor's Y register and setting the carry flag of the processor. Setting the carry flag is a neat trick since the caller can immediately branch-on-carry (BCC or BCS instructions) to an error routine, a brief, quick and relocatable 6502 instruction (2 bytes, 2 cycles), without having to test Y for the (we hope) normal case where there is no error.

As with other aspects of the CIO, error codes were common across devices but could be extended for particular devices. Error handlers could thus be written quite generically, to fail gracefully
Fault-tolerant system
Fault-tolerance or graceful degradation is the property that enables a system to continue operating properly in the event of the failure of some of its components. A newer approach is progressive enhancement...

, maybe put out a message, ask the user whether to retry, propagate the error, and so on.

There were no user-friendly messages for standard error codes in the OS itself. They would be interpreted by the application.

Atari BASIC (and other languages) thus had the freedom to return error codes less than 128, and these meant different things in different languages. There was nothing to stop a perverse implementer using error codes of 128 or above, but no incentive to do so.

Hardware support

The Atari 8-bit family of hardware includes quite a sophisticated graphics system, stemming from its basis in video games consoles.

Unlike many other home computers, the graphics "mode" – the size of pixels and the number of colors that could be displayed – is not fixed for the whole display but described line-by-line in a small microcontrol language to create a display list. Each entry in this list describes one or more lines on the TV display, top-to-bottom. A dedicated graphics microprocessor, "ANTIC", reads these out during the horizontal blanking interval to determine how to display the next TV line. Lines can be narrow (256 pixels wide at highest resolution), normal (320 pixels) or wide (384 pixels). Characters are 8×8 and one display list entry would thus describe 8 TV lines; the ANTIC keeps a counter (a shift register
Shift register
In digital circuits, a shift register is a cascade of flip flops, sharing the same clock, which has the output of any one but the last flip-flop connected to the "data" input of the next one in the chain, resulting in a circuit that shifts by one position the one-dimensional "bit array" stored in...

) of which line in the font to read the pixel data from for each of those lines, thus only requiring one entry for the whole 8 lines in the display. Similarly for larger, coarser graphics modes, the ANTIC chip kept track of the information so that it would display the data on more than one TV line.

The ANTIC thus has to reference the RAM and asserts control over the address bus
Address bus
An address bus is a computer bus that is used to specify a physical address. When a processor or DMA-enabled device needs to read or write to a memory location, it specifies that memory location on the address bus...

 and data bus to do so. This "steals" cycles from the main 6502 CPU. The Sinclair ZX81
Sinclair ZX81
The ZX81 was a home computer produced by Sinclair Research and manufactured in Scotland by Timex Corporation. It was launched in the United Kingdom in March 1981 as the successor to Sinclair's ZX80 and was designed to be a low-cost introduction to home computing for the general public...

 had SLOW and FAST commands to switch the display off and on, because writing the display took about three quarters of the whole processor time. Because the Atari's display rendering is on a separate chip, even though it has to fetch memory
Instruction cycle
An instruction cycle is the basic operation cycle of a computer. It is the process by which a computer retrieves a program instruction from its memory, determines what actions the instruction requires, and carries out those actions...

, its impact on the main processor is generally negligible.

That being said, American machines are clocked
Clock rate
The clock rate typically refers to the frequency that a CPU is running at.For example, a crystal oscillator frequency reference typically is synonymous with a fixed sinusoidal waveform, a clock rate is that frequency reference translated by electronic circuitry into a corresponding square wave...

 to American NTSC
NTSC
NTSC, named for the National Television System Committee, is the analog television system that is used in most of North America, most of South America , Burma, South Korea, Taiwan, Japan, the Philippines, and some Pacific island nations and territories .Most countries using the NTSC standard, as...

 video systems, at about 1.97 Mhz, to fit the 60 Hz vertical blank and correspondingly shorter horizontal blank; but European PAL
PAL
PAL, short for Phase Alternating Line, is an analogue television colour encoding system used in broadcast television systems in many countries. Other common analogue television systems are NTSC and SECAM. This page primarily discusses the PAL colour encoding system...

 models are clocked at 3 Mhz to fit a 50 Hz vertical blank. Because European machines have less work to do for the graphics output, and because of the higher clock rate, it makes them (as a rule of thumb) about 25% quicker than American ones.

Later (XL and XE) machines had an additional graphics processor called CTIA (later GTIA) offering additional modes, providing additional graphics modes
Software-driven graphics modes for the Atari 8-bit computers
This article describes software driven graphics modes for the Atari 8-bit computers; that is, pseudo-graphics modes whose capabilities are reliant on additional software rather than being supported directly in hardware.-Any Point, Any Color:...

, but for the purposes of discussing Atari BASIC these do not affect the basic description as they were programmed much the same way.
Sprites (Player/Missile Graphics)

A hardware sprite
Sprite (computer graphics)
In computer graphics, a sprite is a two-dimensional image or animation that is integrated into a larger scene...

 system is also handled by ANTIC. A sprite is essentially a glyph 8 pixels wide and 256 TV lines tall, and has two colors: the background (0 in the glyph) and the foreground (1). ANTIC mixes the "foreground" color with the pixel beneath it, and displays the pixel "behind" the background color (that is, the main display) without change.

The sprite actually extends for the whole height of the display including the screen border. But the top and bottom are just a solid color. That is, the sprite is essentially a stripe down the screen where some bits can be turned on or off. It could be considered an extremely tall character in a font.

ANTIC will automatically overlay the sprite "stripe" at a horizontal position, so that where there is a 0 in the sprite definition the normal graphics pixel is displayed, but where there is a 1 the sprite color is mixed with the underlying color and the result of that mixing is displayed. Moving the sprite vertically is achieved by block moving (or rotating) the definition of its glyph in memory. This is painfully slow in Atari BASIC but is quite fast in the 6502 machine language, even though it lacks a block-move instruction like the 8080, because the sprite is exactly 256 bytes long and so the indexing can be easily accommodated in a byte-wide register on the 6502.

Although these sprites have quite severe limitations because of their width and color restrictions, careful use of them can make graphics programming, particularly games, significantly simpler since it reduces the need to manipulate display memory for fast-moving objects, such as the "player" and his weapons in a shoot 'em up
Shoot 'em up
Shoot 'em up is a subgenre of shooter video games. In a shoot 'em up, the player controls a lone character, often in a spacecraft or aircraft, shooting large numbers of enemies while dodging their attacks. The genre in turn encompasses various types or subgenres and critics differ on exactly what...

 game. The official ATARI name for the sprite system is indeed "Player/Missile Graphics".

There are five sprites; four "players" eight bits wide and four "missiles" two bits wide. But they can be overlapped to make multi-colored sprites, at the expense of reducing the number of sprites available for other uses.

The ANTIC microprocessor has many "registers" by which colors for them can be set, and to set their horizontal position and turn them on and off.

Colors generally are not specified as an RGB color but as an index into a palette. For example, for a four-colour graphics mode, only four colours can be displayed, numbered 0 to 3, but the color values can be chosen from any of the 256 in the physical color palette. For 2-color modes there is actually only one color, which can be displayed at two levels of luminance. Black-and-white works well here, as does the blue-on-blue that Atari uses by default, but for other colors it may not look so good.

The display list can specify for each line the address in memory from which to fetch the data (with some trivial memory alignment restrictions), but if this is not specified then ANTIC will fetch the data from the next address in memory from where it finished reading the previous line.
Interrupts

Short sections of machine language code can be executed during the horizontal blanking interval, and this is typically done to change the values in the color registers, horizontal sprite positions and so forth thus giving the appearance of more colors or more flexible sprites than the hardware provides ab initio. This machine code has to be very short as there are not many clock cycles available during each horizontal blank. These routines are known as DLIs (display list interrupts) but are simply off limits to Atari BASIC as it is far too slow to perform even the simplest of tasks. Strictly these should be called "DLI routines" but are usually just called "DLIs".

During the vertical blanking interval
Vertical blanking interval
The vertical blanking interval , also known as the vertical interval or VBLANK, is the time difference between the last line of one frame or field of a raster display, and the beginning of the first line of the next frame. It is present in analog television, VGA, DVI and other signals. During the...

, a much longer interval, another interrupt is generated and the Operating System hooks into this to perform some housekeeping tasks. Again, this is not available to Atari BASIC directly, although with some manipulation and severe restrictions (because BASIC was not designed to be re-entrant) it is possible for a VBI (vertical blank interrupt) routine to call a BASIC routine. The VBI is a favourite place to stick some code that needs to execute frequently.

Operating system support

The operating System provided several standard "Graphics modes" by which it set up a display list automatically, and allocated memory, at the top end of free memory. These provided a range of graphics modes including text modes, graphics modes and mixed text-and-graphics modes. It was only these predefined modes that were available to Atari BASIC.

The lack of an OS routine for a general-purpose memory move routine perhaps exhibited in artifacts of the graphics design. For example, once a graphics channel had been opened, it was not possible (or at least easy) to move it down in memory so that other memory could be reserved above it, which meant that generally a program would allocate "more than enough" memory above the high memory pointer (HIMEM) then set the graphics mode (with the GRAPHICS statement) to have the operating system allocate memory for the display below the new high water mark.

Most of ANTIC's registers were write-only, their values could not be read (or rather they could be, but were meaningless or returned different values from those written as they were multiplexed
Multiplexer
In electronics, a multiplexer is a device that selects one of several analog or digital input signals and forwards the selected input into a single line. A multiplexer of 2n inputs has n select lines, which are used to select which input line to send to the output...

). The Operating System kept copies of the values written in "shadow registers" in pages 0 and 2 of memory (page 1 was the hardware stack on 6502 processors), thus allowing programs to read the values. The values written here were rewritten to the ANTIC registers during the vertical blank interrupt.

The operating system provided access to the graphics in two ways: by allowing direct reads and writes (in Atari BASIC, through the PEEK and POKE commands to the memory being used to hold the graphics, by making available the address of the start of that memory in a well-known location, and to the shadow registers), and also by providing a CIO device, "S:", through which CIO commands could be issued. The S: device supported the general-purpose XIO command used to implement PLOT, DRAWTO and FILL (unfortunately the last was not exposed to Atari BASIC, and was also rather tricky to get right at the best of times, as it used a rather primitive scanline fill that filled lines left-to-right, bottom-to-top but stopped as soon as a boundary was met, rather than providing a full flood fill
Flood fill
Flood fill, also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array. It is used in the "bucket" fill tool of paint programs to determine which parts of a bitmap to fill with color, and in games such as Go and Minesweeper for determining...

).

In a way there was some confusion and overlap here in the design. For example, the CIO NOTE and POINT commands could be considered analogous to reading and writing the position of the cursor, but instead they had a separate interface through well-defined memory locations. Thus the exposure of the graphics API to BASIC and other languages was perhaps not as orthogonal and device-independent as it could have been.

Atari BASIC support

Atari BASIC supported graphics using the statements COLOR, SETCOLOR, CLEAR, PLOT, DRAWTO, LOCATE and GRAPHICS.

Because of the support provided by the operating system, Atari BASIC implemented most of its graphics statements as simple calls to those routines or just set the memory registers for the cursor position and so on. In many cases it simply left programmers to use PEEK and POKE statements. It could be argued that some statements such as SETCOLOR were not only redundant but confusing, since they simply set one color value in a shadow register and could be as easily, and more quickly, done with a POKE command; the ROM space used by these routines could perhaps have been better used to implement other things.

The lack of a FILL command is a notable omission considering that the routine, however primitive, was available in the operating system. It could be achieved with the general-purpose XIO command, but was rather fiddly:

REM The co-ordinates of the corners of the fill quadrilateral have to be set up
REM before calling XIO, using POKE into the IOCB. This is quite a trick because
REM it's not easy to find out where the IOCB is. Anyway, then we do:
XIO 18,#6,12,0,"S:"
REM XIO # = Extended IO.
REM 18 = Fill (17=Drawto).
REM #6 = On Channel 6, mapped to the graphics screen device.
REM 12 = Read/write.
REM 0 = Redundant (unused).
REM "S:" = Logical device, used only for OPEN and some disk
REM commands with a target such as for a RENAME
REM Redundant here but used by convention

There was no BASIC support for sprites, although they were not particularly difficult to program in BASIC using PEEK and POKE, but this could not be done particularly fast in, say, games. Similarly, setting up a custom display list could be done, but was rather difficult, and was far more easily and effectively done in machine language. Sprite data could be defined using a DATA statement, and then POKEd into memory. However, scrolling a sprite vertically, for example, was not quick in BASIC as there was no "block memory move" statement and required a slow FOR loop of PEEKs and POKEs.

Hardware support

In comparison to the BASICs of some competing machines at the time, Atari BASIC had good built-in support of sound, (SOUND statement), graphics (GRAPHICS, SETCOLOR, COLOR, PLOT and DRAWTO) and peripheral units like joysticks (STICK, STRIG) and paddles (PADDLE, PTRIG). Other home computer users were often left with cryptic POKE
PEEK and POKE
In computing, PEEK is a BASIC programming language extension used for reading the contents of a memory cell at a specified address. The corresponding command to set the contents of a memory cell is POKE.-Statement syntax:...

s for such programming.

That being said, the parameters for many of these commands were cryptic, and essentially little better than machine code. SOUND took four numeric parameters for pitch, tone, volume and channel (the Atari 8-bits had 4-channel sound); the GRAPHICS statement took three to handle the numerous graphics modes, SETCOLOR and COLOR each took a number of parameters with different meanings depending on the graphics mode and often which did not match between the two, and so forth. It may be an example of Conway's law
Conway's Law
Conway's Law is an adage named after computer programmer Melvin Conway, who introduced the idea in 1968:...organizations which design systems ... are constrained to produce designs which are copies of the communication structures of these organizations....

: clever designers made excellent hardware, by and large following a common model (memory-mapped register addressing for ANTIC
ANTIC
Alphanumeric Television Interface Controller is an early video system chip used in the Atari 8-bit family of microcomputers as well as the Atari 5200 in the 1980s. The chip was patented by Atari, Inc. in 1981...

, GTIA and Pokey
Atari POKEY
The Pot Keyboard Integrated Circuit is a digital I/O chip found in the Atari 8-bit family of home computers and many arcade games in the 1980s. It was commonly used to sample potentiometers and scan matrices of switches...

, for example), but the lack of the teams' interaction made them work in curiously different ways. One may wonder why it would be thought so important to include two key words for examining the state of paddles – something that could be done easily with a single PEEK and indeed in every respect more efficiently than a PADDLE statement – yet not have a FILL command that was already coded in the OS and would have been uniquely advanced for the BASICs of the time.

Similarly, advanced aspects of the hardware such as sprite
Sprite (computer graphics)
In computer graphics, a sprite is a two-dimensional image or animation that is integrated into a larger scene...

s were completely out of bounds for BASIC programmers, and the lack of access to timers made sound programming difficult, particularly because North American machines ran on different clock speeds from the rest of the world (basically because they were tied to the speed of the television system).

Performance

Running on the original equipment, Atari BASIC is slower than other BASICs on contemporaneous equipment for the same home market, sometimes by a surprising amount, especially when one takes into account the fact that the Atari's CPU was clocked almost twice as fast as that of most other 6502-based computers of that era. Most of these problems stemmed from two particularly poorly implemented bits of code.

One is a side effect of how Atari BASIC recalculates line numbers as the program is run. This means that a GOTO has to run a small amount of additional code in order to find the line to jump to. This would normally be a minor issue, but the same code is also used to implement NEXT in a FOR...NEXT loop, so it dramatically lowers performance of these very common loops (indeed, the only loop structure in Atari BASIC). It is obvious that a line number less than 6553610 (1000016) can be stored in a 16-bit unsigned integer, but presumably the designers chose to store it as floating point for other reasons.

Atari BASIC does not do well with integer variables; all numbers are stored as floating point. Atari BASIC relied on the Atari OS's built-in floating point routines (BCD notation), which are relatively slow compared to other representations, even on the same hardware. But most of the slowness of the implementation lies in a particularly poor implementation of the multiply subroutine used throughout the math libraries. This is really not a problem of the language itself but of the underlying OS, but it adds to the general poor performance. More spectactularly, really, the fact that simple integer operations are converted back and forth to floating point really highlights the flaw, especially considering that the Atari's best features rely on special hardware (for graphics, sound and so on) that deals purely in integers: bytes or two-byte words. There is not even in Atari BASIC an easy way to perform bitwise operation
Bitwise operation
A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. This is used directly at the digital hardware level as well as in microcode, machine code and certain kinds of high level languages...

s.

The MOS 6502 processor had a special mode for dealing with BCD (the SED and CLD instructions to treat each 4 bits of a byte
Nibble
In computing, a nibble is a four-bit aggregation, or half an octet...

 as a BCD digit), and perhaps that was particularly attractive to the designers for implementing floating point as BCD. The now almost universal IEEE 754 standard of representation of floating point numbers was still at the design stage when the Atari 8 bit family and its contemporaries first came to market, so the design of an FP implementation was very much up to the OS or BASIC designer.

Several commercial and shareware BASICs were available on the platform that addressed some or all of these issues, resulting in performance that was 3 to 5 times faster than the Atari version. Using these BASICs, the Atari was one of the fastest home computers of its era.

Atari later sold a diskette
Floppy disk
A floppy disk is a disk storage medium composed of a disk of thin and flexible magnetic storage medium, sealed in a rectangular plastic carrier lined with fabric that removes dust particles...

-based version of Microsoft BASIC, Atari Microsoft BASIC
Atari Microsoft BASIC
The Atari Microsoft BASIC and Atari Microsoft BASIC II variants of the BASIC programming language were cartridge or floppy disk packaged versions of the Microsoft BASIC dialect ported to the Atari 8-bit machines....

, and later managed to fit it onto a cartridge as well, but no compiler or runtime was available for redistribution.

Advanced techniques

Despite its small footprint (8 kilobytes), Atari BASIC has some features that give it some powers of more-advanced, larger versions of BASIC.

Subroutines

Atari BASIC has no implementation of subroutine
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

s, or rather, it does not have a concept of local variable
Local variable
In computer science, a local variable is a variable that is given local scope. Such a variable is accessible only from the function or block in which it is declared. In programming languages with only two levels of visibility, local variables are contrasted with global variables...

s. In Fortran terminology, all variables are COMMON.

But programmers can simulate user functions because of the way the GOSUB command can reference a variable. For example, a programmer could start a subroutine at line 10000 and have the program initialize a variable with that number, e.g. LET TEST = 10000. The calling code can then initialize some mutually understood variables and use the statement GOSUB TEST to invoke the subroutine. The subroutine starting at line TEST can then do its operation on the predetermined variables and put return results into variables available after RETURN.

By extension, if the two agree on two variables, an array called, say, STACK and a numeric variable called STACKTOP, then a stack can be implemented in software whereby local variables are pushed and popped to the stack and so implement local variables. For example:


10 DIM STACK(100)
20 STACKTOP = 0
35 REM LINE NUMBERS OF SOME FUNCTIONS FOLLOW
40 FACTORIAL = 8000
60 PUSHSTACK = 2100
70 POPSTACK = 2200
75 REM LET'S COMPUTE EIGHT FACTORIAL
80 LET STACKVALUE = 8: GOSUB PUSHSTACK
90 GOSUB FACTORIAL
100 GOSUB POPSTACK
110 PRINT "EIGHT FACTORIAL IS "; STACKVALUE
120 END
2099 REM PUSHSTACK SUBROUTINE
2100 STACK(STACKTOP) = STACKVALUE: STACKTOP = STACKTOP + 1: RETURN
2199 REM POPSTACK SUBROUTINE
2200 STACKTOP = STACKTOP - 1: STACKVALUE = STACK(STACKTOP): RETURN
7999 REM FACTORIAL SUBROUTINE
8000 GOSUB POPSTACK
8010 IF STACKVALUE <= 2 THEN GOSUB PUSHSTACK: RETURN
8020 FACTORIAL = STACKVALUE
8030 STACKVALUE = STACKVALUE - 1: GOSUB PUSHSTACK
8040 GOSUB POPSTACK: FACTORIAL = FACTORIAL * STACKVALUE
8050 RETURN


BASIC or FORTRAN aficionados may notice that line 8010 can be optimized because a GOSUB followed by a RETURN is the same as a GOTO, because the subroutine will do the RETURN for us:

8010 IF STACK VALUE <= 2 THEN GOTO PUSHSTACK


This is of course a cute example of why the GOTO
Goto
goto is a statement found in many computer programming languages. It is a combination of the English words go and to. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control...

 statement is Considered Harmful
Considered harmful
In computer science and related disciplines, considered harmful is a phrase popularly used in the titles of diatribes and other critical essays ....

.

Includes

Because Atari BASIC can read in lines of code from any device, not just the editor, it is possible to save blocks of code and then read them in and merge them into a single program just as if they had been typed into the editor. Of course this means the lines being read in must have line numbers that are not used in the main program. The code to be merged is written to a device as text using the LIST command, and can be put back into the program with the ENTER command. So the stream of text on the device is, from the BASIC interpreter's point of view, no different from that had it been typed into the editor.

By carefully using blocks of line numbers that do not overlap, programmers can build libraries of subroutines (simulating functions as above) and merge them into new programs as needed.

Embedded machine language

Atari BASIC does not have a built-in assembly language processor. Machine code is generally stored as bytes in strings. Machine code functions are invoked from Atari BASIC with the USR statement, which works in much the same way as GOSUB, but with fewer guarantees.

String variables can hold any of the 256 characters available in the ATASCII
ATASCII
The ATASCII character set, from ATARI Standard Code for Information Interchange, alternatively ATARI ASCII, is the variation on ASCII used in the Atari 8-bit family of home computers. The first of this family were the Atari 400 and 800, released in 1979, and later models were released throughout...

 character set and thus each byte of memory reserved for a string variable can hold any number from 0 to 255, including the characters 3410 (2216, "quote") and 15510 (9B16, "ENTER"), although these are tricky to type in. Short relocatable 6502 machine language routines can be converted to ATASCII characters and stored in the string variable. The machine language routine can be called as a function with the USR command specifying the address of the string variable as the location in memory to execute. For example, if the machine language code is stored in a string named ROUTINE$ it can be called with parameters as ANSWER=USR(ADR(ROUTINE$),VAR1,VAR2). Parameters are pushed onto the hardware stack (in Page 1) as 16-bit integers; variables are pushed as their addresses. The return value is in the A and X registers and interpreted by BASIC as a 16-bit integer; it cannot be pushed to the stack as there is no concept of a stack frame, and for the same reason there is no concept of a void return, but typically if the machine code subroutine does not return anything useful, the value – just whatever happens to be in those registers at the time – is just ignored.

These routines have to use relocatable machine code: that is to say, they cannot use instructions like JMP or JSR that use absolute addresses, except to well-known addresses in the OS and so forth; they can only use branch instructions such as BCC (branch if carry clear) which jump backwards or forwards by roughly 12810 (8016) because the strings could be moved in memory. For this reason page 6 (060016–06FF16), a page of memory not used by BASIC or the operating system, is very popular for storing small routines; but of course one runs the danger that another routine may also wish to be stored there.

On the 6502, relocation is not trivial. These days we expect programs to sit pretty much anywhere in memory; the loader and processor collaborate to make that happen. But microprocessors of that era did not do that. The 6502 was especially hindered by having very few indirection instructions, and those it had were asymmetric: the X and Y registers indirect in different directions. This leads either to rather clumsy code that is forever moving stuff between registers, or clever but obtuse code that keeps them where they need to be even if it would seem more obvious to stick something else there. The 6502 instruction set is small enough that, over a short time, programmers can model the entire processor in their heads, even down to knowing how many cycles each instruction takes, and then start making clever tricks.

As well as using machine code for advanced functions, fairly trivial USR routines are sometimes used simply to gain access to functions in the Atari OS that have not been provided through Atari BASIC: for example block serialization to and from devices (Atari BASIC only lets it be done byte by byte, with GET and PUT, which takes far longer for just shuffling back and forth through the OS layers than actually writing the one byte of data), or for reading and writing blocks of memory (the PEEK and POKE commands were also unnecessarily slow because of the numeric problems described above).

Machine code can also be stored as numbers in DATA statements, but this is pathological in that a byte is then stored as a six-byte floating point number, plus several other overheads, simply to be placed somewhere else as a byte. This method is sometimes used for very short routines where size isn't important but ease of use is (no special loaders or clever typing routines are required), or for one-off programs that then write out the resulting block of bytes (probably stored in a string) is written out as a program that can be read in later byte-for-byte.

Atari BASIC keywords

ABS:Returns the absolute value
Absolute value
In mathematics, the absolute value |a| of a real number a is the numerical value of a without regard to its sign. So, for example, the absolute value of 3 is 3, and the absolute value of -3 is also 3...

 of a number

ADR:Returns the address of a variable
AND:Logical conjunction
Logical conjunction
In logic and mathematics, a two-place logical operator and, also known as logical conjunction, results in true if both of its operands are true, otherwise the value of false....


ASC:Returns the ATASCII value of a character
ATN:Returns the arctangent of a number
BYE:Transfers control to the Operating System ("Memo Pad" on early models)
CHR$:Returns a character given an ATASCII value
CLOAD:Loads a binary program from cassette tape
CLOG:Returns the common logarithm
Common logarithm
The common logarithm is the logarithm with base 10. It is also known as the decadic logarithm, named after its base. It is indicated by log10, or sometimes Log with a capital L...

 of a number

CLOSE:Closes an I/O channel
CLR:Clears variables' memory and program stack
COLOR:Chooses which logical color to draw in
COM:Same as DIM
CONT:Resumes execution of a program after a STOP
COS:Returns the cosine of a number
CSAVE:Saves a binary program to cassette tape
DATA:Defines lists of numeric or string values

DEG:Switches trigonometric functions to compute in degrees
Degree (angle)
A degree , usually denoted by ° , is a measurement of plane angle, representing 1⁄360 of a full rotation; one degree is equivalent to π/180 radians...


DIM:Defines the size of a string or array
DOS:Transfers control to the Disk Operating System, allowing return of control
DRAWTO:Draws a line to given coordinates
Cartesian coordinate system
A Cartesian coordinate system specifies each point uniquely in a plane by a pair of numerical coordinates, which are the signed distances from the point to two fixed perpendicular directed lines, measured in the same unit of length...


END:Finishes execution of the program
ENTER:Loads a text program from an external device
EXP:Exponential function
Exponential function
In mathematics, the exponential function is the function ex, where e is the number such that the function ex is its own derivative. The exponential function is used to model a relationship in which a constant change in the independent variable gives the same proportional change In mathematics,...


FOR:Starts a for loop
For loop
In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement....


FRE:Returns the amount of free memory in bytes
GET:Reads one byte from an I/O channel
GOSUB
GOSUB
GOSUB is a command in many versions of the BASIC computer programming language. A GOSUB statement jumps to a line elsewhere in the program. That line and the following lines up to a RETURN are used as a simple kind of a subroutine without parameters or local variables.The GOSUB command may be used...

:Jumps to a subroutine at a given line in the program, placing the return address on the stack
GOTO
Goto
goto is a statement found in many computer programming languages. It is a combination of the English words go and to. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control...

 and GO TO:Jumps to a given line in the program
GRAPHICS:Sets the graphics mode
IF:Branches depending on whether a condition is true
INPUT:Retrieves a stream of text from an I/O channel
INT:Returns the floor
Floor function
In mathematics and computer science, the floor and ceiling functions map a real number to the largest previous or the smallest following integer, respectively...

 of a number

LEN:Returns the length of a string

LET:Assigns a value to a variable
LIST:Lists (part of) the program to an external device
LOAD:Loads a binary program from an external device
LOCATE:Stores the logical color or ATASCII character at given coordinates
LOG:Returns the natural logarithm
Natural logarithm
The natural logarithm is the logarithm to the base e, where e is an irrational and transcendental constant approximately equal to 2.718281828...

 of a number

LPRINT:Prints text to a printer device
NEW:Erases the program from memory
NEXT:Continues the next iteration of a FOR loop
NOT:Logical negation
Negation
In logic and mathematics, negation, also called logical complement, is an operation on propositions, truth values, or semantic values more generally. Intuitively, the negation of a proposition is true when that proposition is false, and vice versa. In classical logic negation is normally identified...


NOTE:Returns the current position on an I/O channel
ON:A computed goto - performs a jump based on the value of an expression
OPEN:Initialises an I/O channel
OR:Logical disjunction
Logical disjunction
In logic and mathematics, a two-place logical connective or, is a logical disjunction, also known as inclusive disjunction or alternation, that results in true whenever one or more of its operands are true. E.g. in this context, "A or B" is true if A is true, or if B is true, or if both A and B are...


PADDLE:Returns the position of a paddle controller
Paddle (game controller)
A paddle is a game controller with a round wheel and one or more fire buttons, where the wheel is typically used to control movement of the player object along one axis of the video screen...


PEEK
PEEK and POKE
In computing, PEEK is a BASIC programming language extension used for reading the contents of a memory cell at a specified address. The corresponding command to set the contents of a memory cell is POKE.-Statement syntax:...

:Returns the value at an address in memory
PLOT:Draws a point at given coordinates
POINT:Sets the current position on an I/O channel

POKE
PEEK and POKE
In computing, PEEK is a BASIC programming language extension used for reading the contents of a memory cell at a specified address. The corresponding command to set the contents of a memory cell is POKE.-Statement syntax:...

:Sets a value at an address in memory
POP:Removes a subroutine return address from the stack
POSITION:Sets the position of the graphics cursor
PRINT and ?:Writes text to an I/O channel
PTRIG:Returns whether a paddle trigger is pressed
PUT:Writes one byte to an I/O channel
RAD:Switches trigonometric functions to compute in radian
Radian
Radian is the ratio between the length of an arc and its radius. The radian is the standard unit of angular measure, used in many areas of mathematics. The unit was formerly a SI supplementary unit, but this category was abolished in 1995 and the radian is now considered a SI derived unit...

s

READ:Reads data from a DATA statement
REM:Marks a comment in a program
RESTORE:Sets the position of where to read data from a DATA statement
RETURN:Pops a subroutine return address from the stack and jumps to it
RND:Returns a pseudorandom number
Pseudorandom number generator
A pseudorandom number generator , also known as a deterministic random bit generator , is an algorithm for generating a sequence of numbers that approximates the properties of random numbers...


RUN:Starts execution of a program, optionally loading it from an external device
SAVE:Writes a binary program to an external device
SETCOLOR:Maps a logical color to a physical color
SGN:Returns the signum
Signum
Signum is Latin for "sign" and may refer to:* Signum function or sign function in mathematics* Signum, a part of the female Lepidoptera genitalia* Signum , a 1995 by German industrial music artist P·A·L* Signum Framework...

 of a number

SIN:Returns the sine
Sine
In mathematics, the sine function is a function of an angle. In a right triangle, sine gives the ratio of the length of the side opposite to an angle to the length of the hypotenuse.Sine is usually listed first amongst the trigonometric functions....

 of a number


SOUND:Starts or stops playing a tone on a sound channel
SQR:Returns the square root
Square root
In mathematics, a square root of a number x is a number r such that r2 = x, or, in other words, a number r whose square is x...

 of a number

STATUS:Returns the status of an I/O channel
STEP:Indicates the increment used in a FOR loop
STICK:Returns a joystick position
STOP:Stops the program, allowing later resumption
STRIG:Returns whether a joystick trigger is pressed
STR$:Converts a number to string form
THEN:Indicates the statements to execute in an IF statement
TO:Indicates the limiting condition in a FOR statement
TRAP:Sets to jump to a given line if an error occurs
USR:Calls a machine code routine
VAL:Returns the numeric value of a string
XIO:General-purpose I/O routine

Running without Atari BASIC

On the XL/XE models, Atari BASIC could be disabled by holding down the OPTION key while booting the computer. The XEGS would disable BASIC if powered without the keyboard attached.

If another cartridge were inserted it may also disable Atari BASIC, if they used the same address space.

See also

  • BASIC A+ – An extended BASIC for the Atari, from Optimized Systems Software
    Optimized Systems Software
    Optimized Systems Software was a small company producing operating systems and programming languages for the Atari 8-bit and Apple II computer families...

     (OSS)
  • Turbo-Basic XL
    Turbo-Basic XL
    Turbo-Basic XL is an advanced version of BASIC for the Atari 8-bit family of home computers. It is a compatible superset of the more common ATARI BASIC whose most important feature is the vastly improved execution speed...

    - Freeware BASIC compatible with Atari BASIC, also available with a compiler for greater speed and extra commands.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK