Extra memory for BASIC programs
Programs running on the BBC often squeeze as much memory from the system as possible. Programs should adapt themselves to use the amount of memory available by checking how much memory is between LOMEM and HIMEM, for instance:
will define an array of a size that will it into the available variable space, leaving 500 bytes left over for other variables. Note also that you should always compare HIMEM to LOMEM, not to TOP. LOMEM is initially set to TOP, but the variables can easily be put elsewhere completely detached from the BASIC program. TOP is the complement to PAGE, LOMEM is the complement to HIMEM.
Sometimes a program squeezes more memory out of the system. Common methods are to use parts of the operating system or filing system memory by putting the program's data there, or by moving PAGE.
Using operating system memory
Some programs put their data outside the BASIC memory area, for instance the tape/serial buffers at &900-&AFF.
When running on other platforms, such as a second processor, not only are those memory areas not available to the program, the larger amount of memory means the program doesn't need to, and can use memory from BASIC's memory. For example:
IF HIMEM<&8000 THEN mem%=&900 ELSE DIM mem% &FF
this can be shortened to:
mem%=&900:IF HIMEM>&7FFF THEN DIM mem% &FF
This also means that - if there is no 6502-specific code - then the program will work on any other large-memory systems, such as RISC OS, Z80, DOS, Windows, etc.
A common method of squezing out more memory is to lower PAGE. However, you mustn't just force PAGE down to an arbitary address that you think is best, you should move PAGE down only if it needs to be, and only by the amount that is needed for the program to run. The best way of doing this is to start the program with the following:
MODE&87 IF HIMEM<&8000:A%=HIMEM-LOMEM-sizeneeded:IF A%<0:PAGE=PAGE+A%:CHAIN$&600
replacing sizeneeded with an appropriate amount for the program, and ensure you use shadow screen modes (&80+n) in the rest of the program. Then, PAGE will only be forced down if and only if it actually needs to be, and only by an amount that it needs.
This starts by selecting the smallest possible screen mode, and if that results in program space being used it calculates if PAGE needs to be dropped, and if so it drops it by the required amount and reCHAINs the program by using the contents of string buffer that has just been used to load the program.
Some experimentation will be needed to work out the sizeneeded value. One way is to temporarily put HIMEM=&3000 at the start of the program to force it to work as though there is very little memory available. Then use
PRINT ~(&7C00-TOP) AND &FF00
and this should be the value to use for sizeneeded. If this is not enough, then usually just adding another &100 to sizeneeded will do. You will then end up with something like:
MODE&87:IF HIMEM<&8000:A%=HIMEM-LOMEM-&4E00:IF A%<0:PAGE=PAGE+A%:CHAIN$&600
Alternatively, you can start with &4C00, test the program, and progressively add &100 until the program has enough memory to run.
Jgh 23:30, 21 December 2013 (GMT)