How to merge two BASIC programs together (C64)

Sometimes it may be useful to merge two BASIC programs together. For instance, we may need on the program we are currently working on a routine from an older program. So, how to merge two BASIC programs together?

We will have a look at how to merge two BASIC programs together on the Commodore 64, but it’s fairly easy to adapt the following procedure to other Commodore computers.

Please notice that this procedure will work only if the two programs to merge don’t have common line numbers.

 

What we need to know

Not much to be honest. We don’t have to worry about the exact way a BASIC program looks like in RAM memory. We just need to know that two null bytes are used to signal the end of the BASIC program, and that a null byte is used to signal the end of each BASIC line. So, we have a byte zero at the end of each line, and we have two bytes zero at the end of the BASIC program.

Locations 43 and 44 decimal hold the low byte and the high byte of BASIC start address. This is the memory address a BASIC program starts from.

It’s very important to remember that with the command LOAD”PROGRAM NAME”,8 a program will be always loaded starting from the address on locations 43 and 44. The command LOAD”PROGRAM NAME”,8,1 will load the program from the address that can be found in the program file instead. So, as we will see in a moment, we must omit the secondary address (1).

One last thing. Locations 45 and 46 hold the start of variables address (SOV). Since variables are right after the BASIC program in memory, SOV-1 and SOV-2 are the last two bytes of the BASIC program. Both SOV-1 and SOV-2 address hold a zero to signal the end of the BASIC program. The address SOV-3 contains a zero byte signaling the end of the last BASIC program line. So, this must be left intact.

Since we need to merge PROGRAM 1 (currently in memory) and PROGRAM 2 (to be loaded) together, we must load PROGRAM 2 starting from address SOV-2. This way, we will overwrite the two bytes signaling the end of PROGRAM 1.

How to merge two BASIC programs together

 

The procedure

Load PROGRAM 1 as usual. Default loading address will be used (2049 decimal).

Now, let’s evaluate the Start of Variables address:

sv = peek(45)+256*peek(46)

Starting from low byte (location 45) and high byte (from location 46), we now have the actual Start of Variables address on variable SV.

As in the previous picture, we need to load PROGRAM 2 from SOV-2. So, we must use the loading address:

la=sv-2

Since using LOAD”PROGRAM NAME”,8 will load the program on memory starting from the address on locations 43 and 44, we now need the commands:

poke 44,int(la/256)

poke 43,la-peek(44)*256

At this point, we can load PROGRAM 2:

LOAD"PROGRAM 2 NAME",8

We are nearly done. But since we need a big program made up of PROGRAM 1 and PROGRAM 2, we must tell the BASIC interpreter that the whole program starts from the standard address 2049 decimal, which is where PROGRAM 1 starts from:

POKE 43,1: POKE 44,8

We are done. Now we have in memory a big program made up of PROGRAM 1 and PROGRAM 2 merged together. It can be saved as usual.

Previous Entries Commodore 64 keyboard buffer tricks: deleting and creating BASIC lines from BASIC Next Entries Commodore 64 SID music programming with BASIC – playing a simple three voices tune

Leave a Reply

*