I believe that with some clever assembly support it would indeed be possible to chain together any number of XB programs while completely retaining all the variables and strings from one program to the next. If X is 17 and A$ is "Hello World" in the first program they would stay the same when the second program runs. You have to play a trick on XB so it doesn't find out that a new program has been loaded.
Here's the first program:
1 CALL PEEK(flaglocation,X)::IF X=1 THEN 2::CALL LINK("MOVPRG",-20480)::RUN
..the complete 1st program here with many lines of code, then
2000 PRG$="DSK1.XBPROG2"::GOTO 30000
30000 CALL LINK("LOADXB",PRG$)::GOTO 1
Line 1 looks to see if this program has been relocated. If the value at flaglocation is 1 then it has been relocated and it is OK to go on. Otherwise MOVPRG should move the program so that the line number table starts at >B000 (-20480). This address should be as close to >A000 as possible while still leaving room for all the numberic variables and arrays. Using SIZE and some simple arithmetic should give the right value for this
The line number table goes from high to low, so starting at >B000 it would be:
30000 and a pointer to the contents of line 30000
then all the other line numbers and pointers in descending order.
MOVPRG would put the contents of line 30000 safely into low memory and update the pointer to it. Also, it would move the line number table so it starts at >B000, plus setting the flag at flaglocation. All this would be done automatically by "MOVPRG".
Now line 1 comes to RUN and the program runs again but this time X=1 and the program can proceed.
Now the program executes. When it is time to load the next XB program line 2000 sends the program to line 30000. Remember that the contents of line 30000 are in low memory where they cannot be overwritten. But XB runs fine out of that location.
Line 30000 uses another assembly sub, LOADXB, that loads an IV254 XB program but instead of using RUN we use our own loader so we have control over what happens next. Once the program is loaded LOADXB copies the line number table of the newly loaded program so it is immediately after the entry for line 30000, and of course all the line number table pointers in scratch pad ram are updated. (Probably a few other things would need to happen as well) Then the program goes back to line 1, but this time it is line 1 in the newly loaded program.
One big caveat is that the first program to run would have to have ALL the numeric and string variables used by the various programs. The initial prescan allocates space for them all. If the second program has FOR Z=1 TO 10 and the first program didn't have a Z then XB at best would throw an error and more likely would just crash.
I should add that the XB programs should be IV254; otherwise they will trash the contents of VDP ram when they are loaded.
It's an interesting exercise, but is probably more trouble than it is worth.
(edit) Turns out that MOVPRG is way more complex than necessary. All you have to do is make sure the first program is the longest. That can be done by padding it with long REM statements if necessary. Then the program does not need to be relocated and there is only one prescan needed. Then you just need to use MOVPRG to relocate line 30000 to low memory. Line 1 becomes simply: 1 CALL LINK("MOVPRG")
Edited by senior_falcon, Thu Sep 28, 2017 5:49 AM.