BASIC stamp manual v2.2

5: BASIC Stamp Command Reference – RUN
BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com Page 383
When the Slot 1 program runs you may be surprised to see that cats and
dogs are now zero and fleas are up to 259! – even though we didn't
explicitly define them. What happened? The key to remember is that
variable names are simply pointers to RAM addresses, and the PBASIC
compiler assigns variable names to RAM in descending order by size.
This means that in the Slot 1 program, fleas was assigned to RAM
locations 0 and 1 which are holding the values 3 and 1 respectively. Since
words are stored low-byte first, the value 259 for fleas makes sense
(3 + (1 * 256)).
There may be occasions when you need to preserve the RAM space in a
program slot before calling on another slot that has different variable
requirements. You can use the following subroutines to save your RAM
space to the SPRAM and restore it on returning from the other program
slot.
Save_RAM:
PUT 0, B0 ' move RAM 0 value to SP
FOR B0 = 1 TO 25 ' loop through other RAM bytes
PUT B0, B0(B0) ' move RAM value to SP location
NEXT
RETURN
Restore_RAM:
FOR B0 = 1 TO 25 ' loop through RAM
GET B0, B0(B0) ' retrieve RAM value from SP
NEXT
GET 0, B0 ' retrieve RAM 0 value from SP
RETURN
While the use of internal variable names is usually discouraged, these
subroutines demonstrate a valid opportunity for their use, as well as the
ability to take advantage of the BASIC Stamp's unique memory
architecture.
The Save_RAM routine starts by saving the first byte of RAM (internal
name: B0) to location 0 in the SPRAM. This is done so that B0 can be used
as a loop index for the other locations. The FOR...NEXT loop provides
control of that index. The following line is probably the most difficult to
comprehend, but works due to the nature of the BASIC Stamp module's
RAM organization
PUT B0, B0(B0) ' move RAM value to SP location