Task: Use Strings to Store Assembly Language Code in Memory for Execution from BASIC
Needed: A BASIC cartridge or Altirra emulator
Time: 10-15 minutes
I previously demonstrated how to POKE assembly language code into page 6 of memory for execution by BASIC using the USR command. I really like this approach but it does have the important limitation that you are limited to 256 bytes on page 6 which could be a challenge with longer assembly programs. A convenient alternative, which was widely used by BASIC programmer in the 1980s, was to store the decimal values for the assembly commands in a string. This is nice because BASIC then automatically allocates memory to store the contents of the string. You can then provide the memory address of the string contents to the USR function using the BASIC ADR function.
Here is a revised version of the BASIC code from the last post with the new string-based method for loading and executing assembly code. The first step is to initialize or dimension the string (ADD$) to hold the assembly commands (line 20). We then loop through reading the assembly commands from the data statement converting them to characters (CHR$) and adding them one at a time to our ADD$ string (lines 30-50). Once the data are in the string all we need to do is call the USR function and provide the memory address of the string contents using the ADR function (line 60). Everything else is the same as the previous example.
10 GRAPHICS 0
20 DIM ADD$(10)
30 FOR I=1 TO 10
40 READ DAT:ADD$(I)=CHR$(DAT)
50 NEXT I
70 POSITION 0,0:PRINT “2+2=”
80 POSITION 4,0:PRINT PEEK(203)
90 GOTO 90
110 DATA 104,216,24,169,2,105,2,133,203,96
Open Altirra and load BASIC. I used Turbo BASIC XL. You can also load the built-in BASIC from Altirra from the File->Attach special cartridge menu.
Open the BASIC code in your PC text editor or web browser and copy the text to your clipboard. Click on the View tab of Altirra and choose the paste text option from the bottom of the list. Altirra will slowly paste the text into the BASIC command line. It is important to keep focus on the Altirra window or the paste will stop and it will lose some characters. Note you can greatly speed up the paste process by choosing the Warp Speed option from the Altirra System menu option. You will want to turn this off after the paste.
Also try booting the ATR file in Altirra or on your Atari from your PC using SIO2PC. You must have a BASIC cartridge loaded. See the STRING.BAS file. The previous ADD.BAS is provided as well.
I am a bit old-fashioned in preferring to POKE assembly code directly into memory. I have always found strings to be a bit clunky, but there really is no good reason not to use them for this purpose. They are in fact necessary when you have lots of assembly code or can’t use page 6.
Here is a nice post on using strings for a slightly different take. Here is a post on Atari Age that discusses the pros and cons of strings and other methods for calling assembly from BASIC.
Note that you can use characters in the string because each character has a corresponding decimal value that gets stored in memory. As long as you know the decimal values you can just list the characters directly in the string. I don’t use this approach because some of the characters can’t be used in text editors on a PC. You would have to enter these programs directly on your Atari computer.