Basic allows you to MERGE a second program with one already existing in memory but the line numbers of the two programs must be different or else the old lines are lost. It might be useful to be able to have two different BASIC programs in memory at the same time but totally independant of each other. For example:
10 REM program #1
20 PRINT "I'm #1"
30 GOTO 20
10 REM program #2
20 PRINT "I try harder!"
30 GOTO 20
This can be accomplished with the aid of the short machine code program in listing 1 below. What it does is set up a second BASIC area starting at a user specified address and then provides a switch between the two areas. This program is in the club library under the name “2programs”. You can load it to any address you like where there is a clear area of 87 bytes. LOAD “2programs” CODE r will load it to address r. To use the program, first POKE 23760,LOW and POKE 23761,HIGH where HIGH and LOW are the high and low bytes of the address where you want your second BASIC area to start. (HIGH=INT(address/256) and LOW=address-256*HIGH) This address must be above STKEND, the end of your present BASIC area. Exactly how much above depends on how much bigger your first BASIC area will grow. To find out STKEND: PRINT PEEK 23653+256*PEEK 23654
To set up the second BASIC area and switch over to it use RAND USR r. You should see the K cursor. You can now enter BASIC program lines, load a new program from tape, define variables etc., all independant of whatever is in your previous BASIC area. To switch back to your original BASIC area, execute RAND USR (r+23). Further executions of RAND USR (r+23) will switch back and forth between the two BASIC areas.
The machine code uses the addresses 23760 through 23767 to store the alternative values of the system variables PROG, VARS, E-LINE and RAMTOP. All other system variables are common to both BASIC areas, although others could be stored and switched over. (eg. UDG, ATTR-P etc.) In listings below I use the hex addresses and the abbreviations of HOT-Z.
The reason why I found it usefull to have two BASIC areas is so that I can include BASIC staements in my machine code programs without interfering with a co-resident BASIC program. In particular, I wanted to use the INPUT command, which seemed dificult to implement otherwise. To do this I just switch to the second BASIC area, type in my numbered BASIC statements and switch back. At the point in my machine code program where I want the INPUT operation I insert the machine code in listing 2. The last statement of my second BASIC program should be a USR call to the address of the continuation of my machine code program.
ASSEMBLY LISTING 1
Mnemonics Comments
SET-UP : LD HL,(rmtp) get RAMTOP
LD (5CD6), HL store it
LD HL, (5CD0) get alt PROG
LD (5CD2), HL setup alt VARS
LD (HL), 80 =128 dec (end
INC HL of file mark)
LD (5CD4), HL set up alt E LINE
DEC HL
DEC HL
LD (rmtp), HL set RAMTOP to
just before the 2nd
BASIC area
SWITCH : AND A reset the carry flag
SYSVARS: LD DE, (5CD6)
LD HL, (rmtp) get RAMTOP an the alt
LD (5CD6), HL RAMTOP and switch them
LD (rmtp), DE
LD DE, (5CD0)
LD HL, (prog) get PROG and the alt
LD (5CD0), HL PROG and switch them
LD (prog), DE
LD DE, (5CD2)
LD HL, (vars) get VARS and the alt
LD (5CD2), HL Vars and switch them
LD (vars), DE
LD DE, (5CD4)
LD HL, (elin) get E LINE and the alt
LD (5CD4), HL E LINE and switch them
LD (elin), DE
this is CLEL (or smin to HOT Z)
which sets WORKSP STKBOT
CALL 133F and STKEND
return if the carry flag is set
RET C (this is so SYSVARS can be
called as a subrtn from
machine code
Jump to the main loop of the
JP 0E28 BASIC interpreter
ASSEMBLY LISTING 2
Mnemonics Comments
SCF set the carry flag so
SYSVARS will return
CALL SYSVARS switch system variables
LD IY, 5C3A only required if m/c
LD (IY+31), 02 program has changed these
(IY+31=DF SZ )
LD A, 01 execution to start at
LD HL, line# the specified line#
INC HL
CALL 16D6 FIND-L(liad to HOT Z)
which finds start and
end addrs of the line
DEC DE
JP 1B27 jump to execution stage
of BASIC interpreter
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.