Variables
This subtle and remarkable code was discovered by Peter. It provides for variables in RAM, retrieving them without involving an address register. Consider this self-modifying code
The words must be adjacent in this order. They occupy 3 words of RAM.
n!
Store value into variable n
- Fetch the next word from RAM. That is the first instruction word of the variable's code. It must contain a @p to fetch the parameter word
- Discard the instruction word
- Since @p increments the p register, it now points to the parameter word
- Store the new value
- End
n
Calling n will return its value, initially 0.
n may execute additional code, so long as the parameter is fetched in the first instruction word. For example
will add the variable instead of just fetching it.
n! may also execute additional code, so long as it preceeds the 4-word sequence @p drop !p ; aligned on a word boundary. I just used
- pg! if drop 4000 then @p drop !p ;
- pg 0 or ;