Classic Computer Magazine Archive COMPUTE! ISSUE 146 / NOVEMBER 1992 / PAGE G36

BASIC Move and Save. (computer program for accessing memory)
by Daniel Lightner

Have you ever been in the middle of a great BASIC programming session when all of a sudden an OUT OF MEMORY ERROR message appears on the screen? Perhaps you've had a large program to halt in the middle of execution with a similar error message?

As a programmer, you may know that there's a 4K block of free RAM hidden under BASIC's ROM and RAM from 49152 to 53247. Wouldn't it be great if you could store some of your BASIC code there?

Well, you can with BAMOV and BASAV. These two utility programs for the 64 let you use this block of RAM that's usually reserved for machine language programs. They are particularly useful when you're using programs that require a lot of sprite or character data.

Getting Started

BASAV and BAMOV are written in machine language. To enter them, use MLX, our machine language entry program. See "Typing Aids" elsewhere in this section. When MLX prompts for starting and ending addresses for BASAV, respond with the following.

Starting address: C000 Ending address: C0C7

When entering BAMOV, respond with these addresses.

Starting address: CF62 Ending address: D001

Be sure to save each program before leaving MLX.

A Few Rules

Before these programs can be used, certain techniques must be employed and certain rules followed. Your large BASIC program must be divided into two parts. The second part of the program will be called by the first part during execution.

It's important to note that program 2 must be at least 42 bytes shorter than program 1. In most cases you won't have any problems determining this size differential, but here's a way to check. Load program 1 and type this line of code in direct mode.

PRINT INT(PEEK(46)*256)+PEEK(45)-2049

The value returned is the length of the program in bytes. Load the second program and enter the line again. To determine the difference, subtract the value given for program 2 from the value given for program 1. The number returned must be 42 or greater.

Special Coding

Program 1 must contain these or similar lines of code at the end of the program. Just be sure the line numbers are high enough to place the code at the end of the listing.

50000 GOSUB 50005 50001 SYS 53090: RETURN 50005 SYS 53090: GOTO10

When you want program 1 to call program 2, read its data, or whatever, have it GOSUB to line 50000. When program 2 has finished executing, the program will return normally to the next statement following the GOSUB 50000. However, it's not mandatory that control return to program 1.

Program 2 must also begin with whatever line the GOTO in line 50005 of program 1 dictates. In the above example, it's line 10. Remember to keep this number below 50000.

To pass control back to program 1, program 2 must end with a RETURN that is not part of any GOSUB routine in program 2.

A Demonstration

Two short demo programs labeled Prg 1 and Prg2 are included to demonstrate how BASAV and BAMOV work. These programs are written entirely in BASIC. To help avoid typing errors, enter them with The Automatic Proofreader. See "Typing Aids" again.

Running the Demos

Note that when Prg1 executes, it loads BAMOV and a file called Program2 into memory. Having the program load these two is not mandatory. You could load these two programs in direct mode before loading and running Prg1. If you decide to load them in immediate mode, delete lines 25 and 30 of Prg1. This will be better understood as we continue.

Load BASAV with the ,8,1 extension. Then type NEW and press Return. Now load Prg2 as you would any BASIC program. Before you go further, be sure there's a formatted disk in drive 8 in order to receive a relocated version of Prg2. Then type SYS 49152 and press Return. The program will run, and the file will be saved as BAS-TMP. After the file has been saved, enter the following line of code in direct mode.

OPEN1,8,15, "RO:PROGRAM2=BAS-TMP": CLOSE1

It should be clear now that PROGRAM2 as listed in Prg1 is Prg2 relocated. Place a copy of BAMOV on the same disk as Program2. Reset the computer by either typing NEW or turning it off and on again. Load Prg1 and place the disk containing Program2 and BAMOV in drive 8. When you run the program, notice that control alternates between the two programs.

As its name implies, BAMOV is the BASIC mover. It pulls program 2 from beneath BASIC's ROM and places part of program 1 there. When activated again, it does the reverse.

When control is passed to line 50000 in program 1, it does a GOSUB to line 50005 so that when a RETURN is encountered, it will return to the next set of commands. At line 50005, BAMOV is activated, pulling program 2 into BASIC's memory while removing program 1. After it returns from the SYS call, the program encounters the GOTO10 command, and BASIC passes control to line 10 of program 2.

Program flow continues from there until it encounters a RETURN. At that point, control returns to line 50001 following the GOSUB in line 50000 of program 1.

Note that this line must remain at the same location in memory. This is the reason for making sure that program 2 is at least 42 bytes shorter than program 1. Next, BAMOV is called again, and program 1 is put back in place. The RETURN in line 50001 returns control to the line that originally called the GOSUB50000, in this case line 65. All the switching back and forth may sound confusing, but it should become clear when you run the programs.

BASIC programs that require sprite and character data can read the data into memory and then pass control to the second program. But remember that this can only work as long as the second program is shorter than the first program.