DATA2STR
- Type
- Utility
- Category
- Machine-language-in-a-string generator
- Author
- Jerry White
- Published in
- ANTIC Vol. 2 No. 5 (August 1983), "Help!" (listing omitted from "Conserve RAM", ANTIC Vol. 2 No. 4, July 1983)
- Year
- 1983
- Language
- Atari BASIC
- Requirements
- Any Atari 8-bit, 16K RAM, Atari BASIC
About the program
In "Conserve RAM" (ANTIC, July 1983), Jerry White pointed out that a typical start-up line, GRAPHICS 0 plus margin and colour POKEs, costs over 100 bytes of BASIC. Every numeric constant alone takes seven bytes. The same work done by a short machine-language routine stored in a string takes much less room. The listing that does this was left out of the July issue by mistake, and ANTIC printed it in the next month's "Help!" column.
DATA2STR reads the hand-assembled 6502 code from DATA statements and builds it into a new program line 100: GRAPHICS 0:ASM=ADR("…"):X=USR(ASM):STOP. Then it gets the screen editor to enter that line by itself. White says the new line takes 77 bytes, compared with 103 for the all-POKE version.
What you need
Any Atari 8-bit with Atari BASIC.
Typing it in
Plain ASCII. Line 200 is one long line; type POKE 710 without a break. Every DATA line is followed by REMs that give the assembly instruction, a neat way to document hand-assembly. You can leave those out. White's advice: SAVE before you RUN, because a typo in machine code can lock the computer up.
How to use it
RUN it. The program sets the margins and colours with POKEs, prints the new line 100 on the screen, moves the cursor onto it and "presses" RETURN for you. Line 100 is now part of the program. LIST it to see the string of strange characters that is your machine code. If you RUN again, line 100 runs first: the routine sets the colours and BASIC stops. Type CONT and the line is rebuilt. To reuse the routine in your own program, LIST "D:FILENAME.LST",100 (or "C:") and ENTER it later, as the REMs in lines 4–6 say.
How it works
- Line 300 starts
BASIC$with the text of the new line. Lines 400–700 add a quote mark (CHR$(34)), one character for every DATA byte up to the 999 end marker, and a closing quote. Line 800 adds the USR call. - The routine itself is
PLA(remove BASIC's argument count), thenLDA #/STApairs that put 2 into LMARGN (82), 39 into RMARGN (83), 144 into 710, 55 into 712 and 13 into 709, and finallyRTS. Those colour values differ from the POKEs in line 200, so you can see that the routine has run. - None of the bytes is 34 (a quote) or 155 (end of line), the two values that can't live inside a string literal.
- Line 900 prints the line, puts the cursor back on it with
POSITION 2,0, and POKEs 12 (the RETURN key code) into location 764, the last-key-pressed register. When the program ENDs, the editor sees a RETURN and enters line 100.
Variations to try
- Change the immediate values in the DATA (for example 144 and 55) to choose your own colours.
- Add more
LDA/STApairs for other locations, such as 752 (cursor off) or 710–712. Keep 96 (RTS) last and 999 as the end marker.
Listing
1 REM DATA2STR (DATA TO STRING) 12/23/82 by Jerry White
2 REM THIS PROGRAM CREATES AN ASM
3 REM SUBROUTINE IN LINE NUMBER 100.
4 REM AT THE READY PROMPT
5 REM LIST"C:",100 OR
6 REM LIST"D:FILENAME.LST",100
7 REM
200 GRAPHICS 0:POKE 82,2:POKE 83,39:POKE 710,240:POKE 712,240:POKE 709,13
300 DIM BASIC$(120):BASIC$="100 GRAPHICS 0:ASM=ADR("
400 BASIC$(LEN(BASIC$)+ 1)=CHR$(34)
500 READ NUMBER:IF NUMBER=999 THEN 700
600 BASIC$(LEN(BASIC$)+ 1)=CHR$(NUMBER):GOTO 500
700 BASIC$(LEN(BASIC$)+ 1)=CHR$(34)
800 BASIC$(LEN(BASIC$)+1)="):X=USR(ASM):STOP"
900 ?:?:? BASIC$:POSITION 2,0:POKE 764,12:END
1000 REM DATA FOR ASM SUBROUTINE
1001 REM
1010 DATA 104
1011 REM .KEEP BASIC HAPPY PLA
1012 REM
1020 DATA 169,2
1021 REM .LEFT MARGIN LDA #2
1022 REM
1030 DATA 133,82
1031 REM .POKE 82,2 STA 82
1032 REM
1040 DATA 169,39
1041 REM .RIGHT MARGIN LDA #39
1042 REM
1050 DATA 133,83
1051 REM .POKE 83,39 STA 83
1052 REM
1060 DATA 169,144
1061 REM .BACKGROUND COLOR LDA #144
1062 REM
1070 DATA 141,198,2
1071 REM .POKE 710,144 STA 710
1072 REM
1080 DATA 169,55
1081 REM .BORDER COLOR LDA #55
1082 REM
1090 DATA 141,200,2
1091 REM .POKE 712,55 STA 712
1092 REM
1100 DATA 169,13
1101 REM .FOREGROUND COLOR LDA #13
1102 REM
1110 DATA 141,197,2
1111 REM .POKE 709,13 STA 709
1112 REM
1120 DATA 96
1121 REM .RETURN TO BASIC RTS
1122 REM
1130 DATA 999
Credits
- Program listing: DATA2STR — program listing (ANTIC Vol. 2 No. 5 (August 1983), "Help!" (listing omitted from "Conserve RAM", ANTIC Vol. 2 No. 4, July 1983)) — Jerry White
- Picture: The new line 100 that DATA2STR writes: the machine code appears as a string of graphics characters. — Jerry White (program); screenshot by the site
- ANTIC Vol. 2 No. 5 (August 1983) — Help! (Conserve RAM listing) — Jerry White, 1983
- ANTIC Vol. 2 No. 4 (July 1983) — Conserve RAM, by Jerry White — Jerry White; AtariMagazines.com (Kevin Savetz), 1983
