Merge two (saved) Apple II BASIC programs in memory
The ZX Spectrum had the ability to MERGE two BASIC programs from storage media, so that two separate BASIC listings saved to tape, could be first LOADed and then MERGEd, thereby combining the two BASIC programs.
For example
HELLO.BAS
and WORLD.BAS
If loaded using
results in
How can I do this on an Apple II? In other words, is there a mechanism for effectively patching a BASIC program (already in memory) with BASIC code held in a seperate file (on disk or tape)?
There doesn't seem to be a MERGE command1, just LOAD, SAVE, CATALOOG, UNLOCK, etc.. Running two sequential LOAD commands, overwrites the first program, as if a NEW had been executed - which is as expected.
Some searching showed that it would seem that you can not easily merge BASIC code from tape/disk. The usual BASIC commands CHAIN and to a maybe slightly lesser extent, MERGE, are not present in AppleSoft BASIC2.
Linked from that second thread is an interesting workaround3 that, with a little work, might be a solution but, as it stands, it deals with a different issue (and subsequent "loads" of chained BASIC code overwrite the previous BASIC code). I want to actually merge code rather than, chaining two BASIC programs together and passing control between them.
FWIW, I'm using an Apple IIe ROM on the Virtual II emulator (and AppleSoft BASIC/DOS).
1 Chapter 2, DOS Control Commands, Applesoft BASIC Toolbox, 1984
2 Prodos and Applesoft Basic not friends with each other :(
3 Chaining Programs In Applesoft BASIC
The Applesoft Toolkit provided a number of extensions to Applesoft Basic via the ampersand hook.
The renumbering function was probably the most frequently used, but it also provided "hold" and "merge" commands to move an existing Basic program to a hidden safe area, so you could load a new program, and then merge the program back from the hold. It was adviced to do renumbering first before merging to avoid making a mess.
A disk image for the toolkit is available e.g. on Asimov.
There is no built-in facility for merging BASIC programs. They're stored in tokenized form and can't simply be stuffed together. @scruss identified a simple approach in a comment: save one of the parts to a text file, load the second part, and then use the EXEC command to "type" the lines in from the file.
The POKE command adjusts the width of the text window, and is required to prevent Applesoft from adding line breaks mid-line. The TEXT command resets it. (POKE 33,73 works in 80-column mode.)
If you're curious about why the POKE works, the relevant section of the Apple ][+ ROM is: