How to merge BASIC programmes on your Commodore C64, C128 and Plus/4

- by

Commodore LogoYou can merge / combine / concatenate BASIC programmes on a Commodore computer. It’s often useful to develop shorter chunks of code and put them together for a larger app.

Sadly there’s no built-in command that lets you do this, even though the C128 user guide hints that the CONCAT command can do this (if you’ve ever tried you know that this is not the case and only works for sequential data files).

With a few peeks and pokes we can accomplish what we need. Here’s how:

  • after loading the fist programme, we’ll set the start of BASIC to the end of programme 1
  • then we’ll load the second programme
  • and finally return the start of BASIC to the first one

We’re left with both programmes in memory – without any changes in line numbers. Depending on how you write the code a quick RENUMBER command may be necessary here. Even though you can run a programme when the line numbers are out of sequence, GOSUB and GOTO routines may not work as expected.

The commands are slightly different for each machine.

On the C64

The start of BASIC is stored in 43/44, and ends just before the variables begin – which is stored in 45/46. Load your first programme, then type this:

n=(peek(45)+256*peek(46))-2
poke 44,n/256
poke 43,n-256*peek(46)

Load the second part, then reset the BASIC pointers to where the first part resides:

poke 43,1
poke 44,8
clr

On the Plus/4

The Plus/4 works almost identical to the C64 in regards to storing the BASIC pointers, however the start of BASIC is at a different address. Load your first programme, then type this (just like on the C64):

n=(peek(45)+256*peek(46))-2
poke 44,n/256
poke 43,n-256*peek(46)

Load the second part, then reset the BASIC pointers:

poke 43,1
poke 44,16
clr

On the C128

The C128 stores variables in a different memory bank. Therefore it has a dedicated pointer to the end of BASIC which is missing on the other computers. The memory map is slightly different, and the start of BASIC is stored in 45/46. Load your first programme, then type this:

n=peek(174)+256*peek(175)-2
poke 46,n/256
poke 45,n-256*peek(46)

Load the second part, then reset the BASIC pointers:

poke 45,1
poke 46,28
clr

And there you have it! Run any of these routines right after a reset or restart.

Thanks to Rick Kephart for this tip. You may also be interested in another (more complex) solution to this puzzle by Jim Butterfield in which he reads each byte and merges them back on disk.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.