Read Assembly Language from Floppy Disk for Execution in BASIC (15-30 mins)

Task: Read Assembly Language from Floppy Disk for Execution in BASIC

Needed: Altirra emulator or Atari 8-bit computer, BASIC

Time: 15-30 mins

Introduction

I have previously written posts about calling assembly language commands from BASIC. The general idea is to convert assembly language mnemonics (e.g. ADC, STA) to their decimal equivalents and put these in DATA statements in BASIC programs. The DATA statements can then be read directly into memory or stored in strings in memory and then executed using the USR function. The end result is executing assembly code from BASIC which can greatly speed up a program (e.g. player-missile graphics).

Here, I demonstrate how to store the assembly language programs on floppy disk for reading and execution by BASIC. This can be beneficial if you are running up against the storage limit of an 8K ROM, for example.

Instructions

Here is an ATR file (ASMDISK.ATR) with DOS 2.0S and the two BASIC programs (WRITE.BAS & READ.BAS) we will use for the demonstration. The assembly code we will be writing, reading, and executing simply adds 2+2 and stores the result in memory. The details can be found in this previous post.

The first step is to get the assembly code onto floppy disk. An easy way to do this is to write a simple BASIC program to create and OPEN a file and then PUT the decimal values of the assembly code in the file. Here is the BASIC code in the WRITE.BAS file on the ATR image listed above. Line 10 OPENs device #1 (i.e. the floppy drive) for writing (indicated by the 8) with filename ADD.DAT where we will store the assembly decimal values. Lines 20 and 40 are the FOR loop for writing each of the 10 decimal values in the DATA statement in line 50. Line 30 reads the data and PUTs the data in the file one at a time each time through the loop.

10  OPEN #1,8,0,”D:ADD.DAT”
20  FOR I=0 TO 9
30  READ DAT:PUT #1,DAT
40  NEXT I
50  DATA 104,216,24,169,2,105,2,133,203,96

You can copy and paste this code into Altirra with BASIC loaded or use LOAD “D:WRITE.BAS” from the ATR file in Altirra or a floppy disk on original hardware. RUNing this BASIC program will write the data to disk.

In the second step we can now try reading the assembly language data and executing it in the next BASIC program (READ.BAS). Line 10 initializes the string we will use to store the assembly code in memory. Line 20 OPENs the file from the floppy disk for reading (indicated by the 4). Lines 30 and 60 are the FOR loop for the 10 assembly commands. Line 40 GETs each decimal value one at a time each time through the loop. Line 50 stores the decimal value in a string which automatically gets stored in memory by BASIC. Line 100 initializes GRAPHICS mode 0 for printing the result on the screen. Line 110 is the magic USR command which executes the assembly code at the memory location where the string ADD$ is stored (determined using the ADR function). Lines 120 and 130 print the 2+2 result stored in memory location 203 (page 0). Line 140 is an endless loop.

10  DIM ADD$(10)
20  OPEN #1,4,0,”D:ADD.DAT”
30  FOR I=1 TO 10
40  GET #1,DAT
50  ADD$(I)=CHR$(DAT)
60  NEXT I
100  GRAPHICS 0
110  ADD=USR(ADR(ADD$))
120  POSITION 0,0:PRINT “2+2=”
130  POSITION 4,0:PRINT PEEK(203)
140  GOTO 140

You can copy and paste this code into Altirra with BASIC loaded or use LOAD “D:READ.BAS” from the ATR file in Altirra or a floppy disk on original hardware. If all goes well you should see 2+2=4 on the screen.

Comments

This is nice to know how to do if you plan on writing a large program which bumps up against memory or ROM limits. Of course, it takes time to load code from disk so this would need to be done during the program initialization. This demo is also a nice introduction to some of the input-output control commands in BASIC.