Atari BASIC String Handling and Tricks
If you learned BASIC on almost any other 1980s micro, Atari BASIC's strings will feel strange at first. There is no LEFT$, MID$ or RIGHT$, no + for joining strings, and no true string arrays. What you get instead is a different model: every string is a fixed-size block of memory that you declare in advance and then slice with subscripts. Once it clicks, it is fast and surprisingly powerful — strings were the standard way for BASIC programmers to move memory around at machine-code speed.
You will learn: declaring strings, substrings, joining, converting and comparing; searching; fast fills; simulated string arrays; and how to aim a string at an arbitrary address.
You need: Atari BASIC (any revision) on a real machine or emulator.
1. DIM first, always
Every string variable must be dimensioned before use, or you get error 9:
10 DIM A$(20),NAME$(40),BUF$(1000)
The number is the maximum length (up to 32767 bytes). The string's current length starts at zero and changes as you assign to it. LEN(A$) returns the current length, not the DIM size. Assigning more characters than the DIM allows simply truncates — Antic's New Owners Column demonstrated that typing a 30-letter name into a 20-character string keeps only the first 20.
Internally, BASIC keeps an eight-byte entry for each variable in the variable value table (pointer VVTP, locations 134–135). For a string, the entry holds an offset into the string/array area (pointer STARP, locations 140–141), plus the current length and the DIM size. The characters themselves sit in the string/array area, one byte each. That is why a string of 1,000 characters costs about 1,000 bytes, while a numeric array of 1,000 elements costs 6,000 — every number is a six-byte BCD float.
2. Substrings replace LEFT$, MID$ and RIGHT$
A subscripted string gives you a slice:
| Other BASICs | Atari BASIC |
|---|---|
LEFT$(A$,5) |
A$(1,5) |
MID$(A$,6,3) |
A$(6,8) |
RIGHT$(A$,4) |
A$(LEN(A$)-3) |
With two subscripts you get characters from the first to the second. With one subscript you get everything from that position to the end. A subscript of 0, or one past the current length, raises error 5.
Slices can also be the target of an assignment, which lets you overwrite part of a string in place:
10 DIM A$(20),B$(20)
20 A$="HELLO THERE READER"
30 B$="XXXXXXXXXX"
40 B$(3,5)=A$(6,8)
50 PRINT B$
When the two sides differ in size, the smaller one decides how many characters are copied.
3. Joining strings
With no + operator, you append by assigning to the position just past the current end:
10 DIM F$(16),N$(12)
20 PRINT "FILE NAME";:INPUT N$
30 F$="D:"
40 F$(LEN(F$)+1)=N$
50 PRINT F$
This is the idiom you will see in thousands of magazine listings. Make sure the DIM is big enough for the combined result. To empty a string, assign the null string: F$="".
4. Converting and comparing
STR$(N)turns a number into a string;VAL(A$)turns the leading digits of a string back into a number.VALof a string that starts with a letter raises error 18.ASC(A$)gives the ATASCII code of the first character;CHR$(N)makes a one-character string.- Comparisons (
=,<,>,<>,<=,>=) work character by character using ATASCII codes, so"BCD" < "CDE"is true.
A string can also be read from DATA statements. A comma separates items, so DATA DAVID PLOTKIN is one item while DATA DAVID,PLOTKIN is two.
5. The self-copy fill trick
This is the most famous Atari BASIC string idiom, described in De Re Atari. BASIC copies strings one byte at a time, from the first byte upwards. If the destination is the source shifted by one position, each byte copied becomes the source for the next one — so a single character spreads through the whole string at machine speed:
10 DIM A$(1000)
20 A$(1)="*":A$(1000)="*"
30 A$(2)=A$
40 PRINT A$
Line 20 sets the first byte and, by writing the last byte, also makes the string's length 1000. Line 30 does the fill. Replace "*" with " " to blank a buffer before use — Carl Evans's Atari BASIC Faster and Better recommends exactly this before building formatted output.
The same forward-copy behaviour means an overlapping copy upwards inside one string smears rather than shifts. Try it to see:
10 DIM S$(15):S$="123456789"
20 S$(7,15)=S$(1,9):PRINT S$
6. Searching for a substring
There is no INSTR in Atari BASIC (Turbo-BASIC XL and FastBasic add one), but a subroutine is easy:
1000 REM FIND T$ IN S$, RESULT IN P (0=NOT FOUND)
1010 P=0:LT=LEN(T$)
1020 IF LT=0 OR LT>LEN(S$) THEN RETURN
1030 FOR I=1 TO LEN(S$)-LT+1
1040 IF S$(I,I+LT-1)=T$ THEN P=I:POP:RETURN
1050 NEXT I:RETURN
Note the POP in line 1040: leaving a FOR loop early would otherwise leave its entry on BASIC's runtime stack. For long strings, a BASIC loop like this is slow; Atari BASIC Faster and Better includes a machine-language search routine (SEEKER) called through USR for that reason.
7. Upper-casing and other character surgery
Walk the string, adjust codes, and write each character back with a one-character slice:
2000 REM CONVERT W$ TO UPPER CASE
2010 FOR I=1 TO LEN(W$)
2020 C=ASC(W$(I,I))
2030 IF C>96 AND C<123 THEN W$(I,I)=CHR$(C-32)
2040 NEXT I:RETURN
Inverse video is simply the high bit: adding 128 to a character's ATASCII code (or subtracting 128) flips it between normal and inverse.
Stripping trailing blanks — handy after reading fixed-width records — works backwards from the end:
3000 FOR I=LEN(W$) TO 2 STEP -1
3010 IF W$(I,I)<>" " THEN W$=W$(1,I):POP:RETURN
3020 NEXT I:RETURN
8. Simulating string arrays
Atari BASIC cannot DIM N$(10,8). The standard workaround is one long string divided into fixed-size fields. Element E of size SZ lives at positions SZ*(E-1)+1 to SZ*E:
10 DIM N$(80),T$(10)
20 SZ=10:N$(1)=" ":N$(80)=" ":N$(2)=N$
30 FOR E=1 TO 8:READ T$
40 N$(SZ*(E-1)+1,SZ*E)=T$:NEXT E
50 DATA JOHN,PAUL,ERIC,CARL,GREG,MARK,MIKE,ANNE
60 PRINT "WHICH NUMBER (1-8)";:INPUT E
70 T$=N$(SZ*(E-1)+1,SZ*E):PRINT T$
80 GOTO 60
Line 20 blank-fills the whole string first so every field has a defined length, and short names are padded with spaces. A two-dimensional version just extends the formula: element (X,Y) with XMAX columns starts at (X-1)*SZ+1+(Y-1)*SZ*XMAX. Evans's book packages these as LOOKUP1D and LOOKUP2D subroutines.
If your data values never exceed 255, you can also use a string instead of a numeric array: CHR$ to store, ASC to read. It's a classic space-saving trick, since each element takes one byte instead of six.
9. Pointing a string at any memory
Because a string is just an offset from STARP plus a length, you can edit its variable-table entry so that the "string" overlays screen memory, a character set or player/missile memory. Assignments to that string then move data at machine speed. De Re Atari uses this to animate a player:
100 REM PLAYER/MISSILE EXAMPLE
110 DIM A$(512),B$(20)
120 X=X+1:READ A:IF A<>-1 THEN B$(X,X)=CHR$(A):GOTO 120
130 DATA 0,255,129,129,129,129,129,129,129,129,255,0,-1
2000 POKE 559,62:POKE 704,88
2020 I=PEEK(106)-16:POKE 54279,I
2030 POKE 53277,3:POKE 710,224
2040 VTAB=PEEK(134)+PEEK(135)*256
2050 ATAB=PEEK(140)+PEEK(141)*256
2060 OFFS=I*256+1024-ATAB
2070 HI=INT(OFFS/256):LO=OFFS-HI*256
2090 POKE VTAB+2,LO:POKE VTAB+3,HI
3000 Y=60:Z=100:V=1:H=1
4000 A$(Y,Y+11)=B$:POKE 53248,Z
4010 Y=Y+V:Z=Z+H
4020 IF Y>213 OR Y<33 THEN V=-V
4030 IF Z>206 OR Z<49 THEN H=-H
4420 GOTO 4000
How it works: A$ must be variable number zero — the first variable name BASIC ever saw for this program, because names are numbered in the order they are entered — so its entry starts at the very beginning of the variable value table (VTAB). Bytes 2 and 3 of that entry hold the offset from STARP; lines 2060–2090 rewrite it so the string begins 1,024 bytes into player/missile memory, which line 2020 places 16 pages below RAMTOP. Line 2000's POKE 559,62 selects single-line player resolution, in which player 0 starts 1,024 bytes above the P/M base. Line 4000 then copies the 12-byte shape into the player in one statement — and because the shape has a blank byte at each end, moving it one line at a time erases its own trail.
This is powerful and unforgiving: get the variable order or the arithmetic wrong and you will overwrite something important. Save before you run.
Troubleshooting
- ERROR 9: you forgot to DIM, or DIMmed the same string twice.
- ERROR 5: a subscript is 0 or beyond the current length — common when slicing an empty string.
- Garbage at the end of a string: writing to a far position (like
A$(1000)="*") extends the length, and the bytes in between are whatever was in memory. Fill first. - The string-pointer trick corrupts the program: the target variable was not the one you thought. Type that line first in a fresh program (after NEW), and note that after LIST/NEW/ENTER the variable table is rebuilt in the order variables appear.
Further reading
- Atari BASIC Faster and Better — Carl M. Evans (IJG, Inc., 1983), Chapter 7.
- Atari BASIC Tutorial — Robert A. Peck (Howard W. Sams, 1983), Chapter 3 "Stringing Along".
- The Atari BASIC Source Book — Bill Wilkinson, Kathleen O'Brien and Paul Laughton (Small System Services, 1983).
Credits
- Antic Vol. 5 No. 9 (Jan 1987) — New Owners Column, Lesson 10: Strings (David Plotkin) — David Plotkin, 1987
- De Re Atari, Chapter 10 — Atari BASIC — Lane Winner (Section 10 of De Re Atari)
- Atari BASIC Faster and Better (Carl M. Evans, IJG 1983) — Chapter 7: Using Strings — Atari8bit.net, 1983
- Mapping the Atari — locations 134-135 (VVTP) and 140-141 (STARP) — AtariArchives.org
