Converting Microsoft BASIC and Applesoft Listings to Atari BASIC
Most BASIC books and magazines of the late 1970s and early 1980s printed programs in Microsoft-derived dialects: Applesoft on the Apple II, Commodore's PET BASIC, TRS-80 Level II, or the generic Microsoft BASIC of Creative Computing's BASIC Computer Games. Atari BASIC is not a Microsoft BASIC, and those listings will not run as they stand. Most differences are mechanical, though, and once you know the patterns, a page of Applesoft takes only a few minutes to convert.
What you'll learn: the standard translations for strings, string arrays, TAB and print spacing, INPUT, ELSE, error handling and keyboard reading, the traps in DATA and number printing, and a worked conversion you can check line by line.
What you need: Atari BASIC. Every Atari-side example and behaviour below was tested on revision C. If you would rather run Microsoft code nearly unchanged, Atari's own Microsoft BASIC is another route (see Atari Microsoft BASIC: A Guide for Atari BASIC Programmers).
1. Strings: the biggest change
Microsoft strings are dynamic and are cut up with functions. Atari strings must be DIMmed with a maximum length, and are cut up with subscripts: A$(X,Y) means characters X through Y, and A$(X) means from X to the end. That is what Paul N. Havey's "Converting Applesoft Basic to Atari Basic" (The Creative Atari) set out in a table. With one correction (RIGHT$ needs the +1, which we confirmed by testing):
| Microsoft | Atari BASIC |
|---|---|
DIM nothing (strings are automatic) |
DIM A$(n) for every string, before use |
LEFT$(A$,X) |
A$(1,X) |
RIGHT$(A$,X) |
A$(LEN(A$)-X+1) |
MID$(A$,X,Y) (Y characters) |
A$(X,X+Y-1) |
MID$(A$,X) |
A$(X) |
A$=A$+B$ |
A$(LEN(A$)+1)=B$ |
C$=A$+B$ |
C$=A$:C$(LEN(C$)+1)=B$ |
LEN, ASC, CHR$, VAL, STR$ |
the same |
Watch out for these:
- A substring can't extend past the current length. Asking for characters 4 to 6 of a two-character string is an error (ERROR 5), where Microsoft BASIC quietly returns less. Guard with
LENfirst. - Assignment truncates silently. Put a seven-letter word into a string DIMmed to 5 and you get the first five letters, with no error.
- Empty string comparisons work:
IF A$="" THEN ...is fine.
2. String arrays
In Microsoft BASIC, DIM N$(10) makes ten strings. In Atari BASIC it makes one string of ten characters, so any program with string arrays needs restructuring. The standard solution, given by David E. Carew in The Creative Atari, is to keep all the strings in one long string, each in a fixed-width slot, and compute where each slot starts:
20 DIM N$(40),T$(8),LN(5)
30 FOR I=1 TO 5:READ T$:LN(I)=LEN(T$):T$(LEN(T$)+1)=" ":N$(LEN(N$)+1)=T$:NEXT I
Here each name is padded to 8 characters (the silent truncation in line 30 stops the padding at exactly 8) and appended to N$. Element P then lives at N$((P-1)*8+1,(P-1)*8+8). Store the real lengths in a numeric array, as here, if you need to trim the padding. For two-dimensional string arrays, Carew pre-computes start and end positions in two numeric arrays, so that A$(X,Y) becomes ARR$(BG(X,Y),EN(X,Y)). Atari BASIC String Handling and Tricks has more on this.
3. PRINT spacing, TAB and SPC
Three differences change how printed output looks:
- Numbers are printed without surrounding spaces.
FOR I=1 TO 2:? I;:NEXT Iprints12in Atari BASIC. Many Microsoft BASICs pad positive numbers with spaces, so tables that relied on it run together. Print" "explicitly. STR$has no leading space:STR$(5)is"5".- The comma jumps to the next "print position", every 10 columns by default. Fred Pinho's "Getting Along Without TAB" (The Creative Atari) pointed out that location 201 sets the spacing.
POKE 201,5gives columns every 5 characters. Restore it withPOKE 201,10.
There is no TAB or SPC. For TAB, set the cursor column directly. Location 85 (COLCRS) is the cursor column for the next PRINT, counted from the screen's left edge. The default left margin is column 2, so TAB(N) relative to the margin becomes POKE 85,N+2 (or POSITION N+2,row if you know the row):
90 ? "PLANET ";P;" IS ";T$;:POKE 85,25:? T$(1,3)
For SPC(N), print a string of spaces: keep a SP$ of spaces and print SP$(1,N). Pinho's own approach was to POKE 201 with the gap he wanted, then print an empty item followed by a comma. Moving the cursor back up a line uses location 84, the cursor row.
Screen-clearing and cursor-addressing commands such as CLS, HOME, HTAB and VTAB become ? CHR$(125) (the clear-screen character) and POSITION column,row.
4. INPUT, GET and the keyboard
- No prompt in INPUT. Atari BASIC's INPUT takes no prompt string. Write
? "NAME";:INPUT N$. INPUT prints its own?. - Single keys. Microsoft
GET A$orINKEY$becomes either a K: channel,OPEN #1,4,0,"K:":GET #1,K, which waits and returns an ATASCII code (CHR$(K)gives the character), or a check of location 764, which does not wait. It holds 255 when no key has been pressed, and otherwise a keyboard code, not ATASCII. Pinho used the 764 method for his car-racing game. Set it back to 255 after reading. Reading the Keyboard, Console Keys and HELP has the code table.
5. Control flow
| Microsoft | Atari BASIC |
|---|---|
IF C THEN X ELSE Y |
IF C THEN X:GOTO next then put Y on the following line |
IF C THEN 100 / IF C GOTO 100 |
IF C THEN 100. IF ... GOTO is a syntax error |
ON ERROR GOTO n |
TRAP n, then PEEK(195) for the error number |
RESUME |
GOTO the right line, and set TRAP again |
DEF FN A(X)=... |
put the formula in a subroutine and GOSUB it |
NEXT (no variable) / NEXT J,I |
NEXT J:NEXT I. Atari BASIC rejects both forms as syntax errors |
| multi-dimensional arrays | at most two dimensions: DIM A(10,10) |
For ELSE, the trick is to make the "then" part jump past the "else" part:
100 IF LN(P)>5 THEN ? "A LONG NAME":GOTO 60
110 ? "A SHORT NAME":GOTO 60
Comparisons give 1 for true and 0 for false in Atari BASIC (? (2>1) prints 1). Code that adds comparisons together, such as X=X+(K=7)-(K=11), works, but check the sign if the original dialect used another value for true.
6. DATA and READ
Atari BASIC reads strings from DATA as plain text up to the next comma, including any quotation marks. DATA "QUOTED" reads as the eight characters "QUOTED", quotes and all, which we confirmed. So remove the quotes from string DATA when converting, unless the item contains a comma. RESTORE line works and is very useful.
7. A worked example
Here is a short program in typical Microsoft style:
10 DIM N$(5)
20 FOR I=1 TO 5: READ N$(I): NEXT
30 DATA "MERCURY","VENUS","EARTH","MARS","JUPITER"
40 HOME
50 INPUT "WHICH PLANET (1-5)";P
60 IF P<1 OR P>5 THEN PRINT "NO SUCH PLANET": GOTO 50
70 PRINT "PLANET ";P;" IS ";N$(P);TAB(25);LEFT$(N$(P),3)
80 IF LEN(N$(P))>5 THEN PRINT "A LONG NAME" ELSE PRINT "A SHORT NAME"
90 GOTO 50
And the Atari BASIC version:
10 REM ATARI BASIC CONVERSION
20 DIM N$(40),T$(8),LN(5)
30 N$="":FOR I=1 TO 5:READ T$:LN(I)=LEN(T$):T$(LEN(T$)+1)=" ":N$(LEN(N$)+1)=T$:NEXT I
40 DATA MERCURY,VENUS,EARTH,MARS,JUPITER
50 ? CHR$(125)
60 ? "WHICH PLANET (1-5)";:INPUT P
70 IF P<1 OR P>5 THEN ? "NO SUCH PLANET":GOTO 60
80 S=(P-1)*8+1:T$=N$(S,S+LN(P)-1)
90 ? "PLANET ";P;" IS ";T$;:POKE 85,25:? T$(1,3)
100 IF LN(P)>5 THEN ? "A LONG NAME":GOTO 60
110 ? "A SHORT NAME":GOTO 60
What changed, line by line: the string array became a padded string plus a length table (20–30). The quotes left the DATA (40). HOME became CHR$(125) (50). The INPUT prompt became a PRINT (60). The array reference became a substring (80). TAB became a POKE to the cursor column, and LEFT$ a subscript (90). ELSE became a GOTO over the following line (100–110). In a test copy that fed in the numbers 0 to 6, it gave the right planet, abbreviation and verdict for 1 to 5, and "NO SUCH PLANET" for 0 and 6.
8. Machine-specific code
Anything that PEEKs, POKEs or CALLs a memory address is specific to the original computer and has to be rewritten from scratch: Apple graphics and sound, PET screen POKEs, TRS-80 SET and RESET. Work out what the code does and use the Atari's own commands: GRAPHICS, PLOT/DRAWTO, SOUND, STICK and so on. Pinho's racing game is a good model. The Atari version gained sound (distortion 2 for the engine) and colour-cycling crash effects that the original never had.
Troubleshooting
- ERROR 9: a string was used before its DIM, or DIMmed twice (common when you leave the Microsoft
DIM N$(5)in place). - ERROR 5: a substring reached past the string's current length. Microsoft functions would have returned a shorter string.
- Numbers run together: add
" "between numeric items in PRINT. - Quotes appear around words: the DATA still has Microsoft-style quotes.
- Columns are off by two: COLCRS counts from the screen edge, not from the left margin, which is normally column 2. Add 2 to the original TAB value.
Further reading
- Paul N. Havey, "Converting Applesoft Basic to Atari Basic", in The Creative Atari (Creative Computing Press, 1983).
- Fred Pinho, "Getting Along Without TAB: An Atari Translation", in the same book.
- David E. Carew, "String Arrays in Atari Basic", in the same book.
- On this site: Atari BASIC String Handling and Tricks, Atari Microsoft BASIC: A Guide for Atari BASIC Programmers, Atari BASIC Error Codes Explained.
Credits
- Picture: The converted planet program, in a test version that feeds it the numbers 0 to 6 instead of waiting for INPUT. — Program authors; screenshot by the site
- Paul N. Havey, "Converting Applesoft Basic to Atari Basic", The Creative Atari — Paul N. Havey; AtariArchives.org
- Fred Pinho, "Getting Along Without TAB -- An Atari Translation", The Creative Atari — Fred Pinho; AtariArchives.org
- David E. Carew, "String Arrays in Atari Basic", The Creative Atari — David E. Carew; AtariArchives.org
- Mapping the Atari — 82 LMARGN, 84 ROWCRS, 85-86 COLCRS, 201 PTABW, 764 CH — AtariArchives.org
