Forth does not rely as heavily on the use of variables as other compiled
languages. This is because values normally reside on the stack.
A D V E R T I S E M E N T
situations, of course, where variables are required. To create a variable, use
the word VARIABLE as follows:
This created a variable named MY-VAR . A space in memory is now reserved to hold
its 32-bit value. The word VARIABLE is what's known as a "defining word" since
it creates new words in the dictionary. Now enter:
The number you see is the address, or location, of the memory that was reserved
for MY-VAR. To store data into memory you use the word ! , pronounced
"store". It looks like an exclamation point, but to a Forth programmer it is the
way to write 32-bit data to memory. To read the value contained in memory at a
given address, use the Forth word @ , pronounced "fetch". Try entering
513 MY-VAR !
MY-VAR @ .
This sets the variable MY-VAR to 513 , then reads the value back and prints it.
The stack diagrams for these words follows:
@ ( address -- value , FETCH value FROM address in memory )
! ( value address -- , STORE value TO address in memory )
VARIABLE ( <name> -- , define a 4 byte memory storage location)
A handy word for checking the value of a variable is ? , pronounced
"question". Try entering:
If ? wasn't defined, we could define it as:
: ? ( address -- , look at variable )
Imagine you are writing a game and you want to keep track of the highest score.
You could keep the highest score in a variable. When you reported a new score,
you could check it aginst the highest score. Try entering this code in a file as
described in the previous section:
: REPORT.SCORE ( score -- , print out score )
DUP CR ." Your Score = " . CR
HIGH-SCORE @ MAX ( calculate new high )
DUP ." Highest Score = " . CR
HIGH-SCORE ! ( update variable )
Save the file to disk, then compile this code using the INCLUDE word. Test your
word as follows:
The Forth words @ and ! work on 32-bit quantities. Some Forths are "16-bit"
Forths. They fetch and store 16-bit quantities. Forth has some words that will
work on 8 and 16-bit values. C@ and C! work characters which are usually for
8-bit bytes. The 'C' stands for "Character" since ASCII characters are 8-bit
numbers. Use W@ and W! for 16-bit "Words."
Another useful word is +! , pronounced "plus store." It adds a value
to a 32-bit value in memory. Try:
20 MY-VAR !
5 MY-VAR +!
MY-VAR @ .
Forth also provides some other words that are similar to VARIABLE. Look in the
glossary for VALUE and ARRAY. Also look at the section on "local
variables " which are variables which only exist on the stack while a Forth
word is executing.
A word of warning about fetching and storing to memory: You have now
learned enough about Forth to be dangerous. The operation of a computer is based
on having the right numbers in the right place in memory. You now know how to
write new numbers to any place in memory. Since an address is just a number, you
could, but shouldn't, enter:
73 253000 ! ( Do NOT do this. )
The 253000 would be treated as an address and you would set that memory location
to 73. I have no idea what will happen after that, maybe nothing. This would be
like firing a rifle through the walls of your apartment building. You don't know
who or what you are going to hit. Since you share memory with other programs
including the operating system, you could easily cause the computer to behave
strangely, even crash. Don't let this bother you too much, however. Crashing a
computer, unlike crashing a car, does not hurt the computer. You just have to
reboot. The worst that could happen is that if you crash while the computer is
writing to a disk, you could lose a file. That's why we make backups. This same
potential problem exists in any powerful language, not just Forth. This might be
less likely in BASIC, however, because BASIC protects you from a lot of things,
including the danger of writing powerful programs.
Another way to get into trouble is to do what's called an "odd address memory
access." The 68000 processor arranges words and longwords, 16 and 32 bit
numbers, on even addresses. If you do a @ or ! , or W@ or
W! , to an odd address, the 68000 processor will take exception to this
and try to abort.
Forth gives you some protection from this by trapping this exception and
returning you to the OK prompt. If you really need to access data on an odd
address, check out the words ODD@ and ODD! in the glossary. C@
and C! work fine on both odd and even addresses.
If you have a number that is appearing often in your program, we recommend that
you define it as a "constant." Enter:
128 CONSTANT MAX_CHARS
We just defined a word called MAX_CHARS that returns the value on the stack when
it was defined. It cannot be changed unless you edit the program and recompile.
Using CONSTANT can improve the readability of your programs and reduce
some bugs. Imagine if you refer to the number 128 very often in your program,
say 8 times. Then you decide to change this number to 256. If you globally
change 128 to 256 you might change something you didn't intend to. If you change
it by hand you might miss one, especially if your program occupies more than one
file. Using CONSTANT will make it easy to change. The code that results is
equally as fast and small as putting the numbers in directly. I recommend
defining a constant for almost any number.