Memory Protection


Jim Clark

Player/missile graphics, redefined character sets, screen flipping, and machine language subroutines all create a need for protected areas of memory. This article covers several ways of handling the problem and gives you a program that protects low memory.

On the Atari, a problem arises in applications requiring a portion of memory to be protected from BASIC. For example, most machine language subroutines need protection. The problem is that BASIC is likely to use memory anywhere within available RAM, thus writing over the machine language subroutine and destroying it.

In many computers it is possible to protect memory at the "high" end, that is, at the highest RAM address. The Atari uses high memory for the data which is displayed on the screen. If you attempt to protect memory above the screen display by reducing the high memory value that BASIC thinks it has, then you cannot clear the screen or scroll text in any of the split-screen modes because these actions affect memory beyondthe screen display area. These actions cause no problem when the screen display is actually the last thing in memory, because they apply to nonexistent memory. However, if you want to use memory beyond the screen display for your own purposes, then your data will be damaged by any action in your program which clears the screen or scrolls text in a text window.

Another alternative is to protect low memory. The main problem with this approach is that the memory protection must be done before BASIC gets control, since BASIC starts saving any program you enter beginning at the low memory address. The program shown here solves this problem as follows: it takes control of the Atari with a machine language subroutine and resets the system's low memory pointer. It then reinitializes BASIC--just as if you had pressed the SYSTEM RESET key--and BASIC takes control again, blissfully unaware that it now has less RAM to work with than it did before you ran this program.

To find the address of the memory you have protected, type ?PEEK(743) + 256*PEEK(794) beforeyou run this program. The number printed can be used as the origin for a machine language subroutine, or as the destination address for whatever data you want to store in the protected area.

When you run the program, it asks how much memory you want to protect. Type in any positive number which is less than the amount of RAM you have available, as determined by typing ?FRE(0). The program reinitializes BASIC, and if you type ?PEEK(743) + 256*PEEK(744) again, the number printed will be greater than the value shown before running the program: the difference is the amount you requested to be protected. The memory area will remain protected until you turn the computer off, and the area can be used for machine language subroutines, redefined character sets, player/missile graphics objects, or any other use you might wish.

Memory Protection

Download P209L1.BAS (Saved BASIC)
Download / View P209L1.LST (Listed BASIC)

Return to Table of Contents | Previous Section | Next Section