Using kForth

2.4 Variables and Constants

An integer variable may be declared as follows:

variable name

Values may be stored and retrieved from the variable using the "store" (!) and "fetch" (@) operators. For example, if we want to define a variable called counter and initialize its value to 20, we enter the following:

variable counter
20 counter !

When you define a variable, memory is reserved at some address to hold an integer value, and the name of the variable becomes part of the dictionary. Typing the name counter at the Forth prompt and pressing enter will cause the memory address of counter to be placed onto the stack. Try the following:


You will see a memory address on top of the stack.

To examine the value stored in the variable counter, we place the address of counter on the stack, then use the fetch operator to retrieve the value from that address onto the stack:

counter @

The number 20 will be on top of the stack. Of course to see the value, we must print it using the word "dot" (.), so entering

counter @ .

will print the value 20. Forth also has a built-in word, ?, that performs the sequence '@ . '.

Now, let's say we want to increment the value of counter by ten. First we fetch the value stored in counter onto the stack, then add ten, and finally store the new value into the variable. This is accomplished by the sequence:

counter @ 10 + counter !

Actually, Forth provides another shorter way of doing the same thing:

10 counter +!

Floating point variables are defined in a similar way:

fvariable name

The correspoding operators for storing and retrieving floating point numbers into the variable are f! and f@. Let's define a floating point variable called velocity and initialize it to zero.

fvariable velocity
0e velocity f!

Note that a floating point value of zero is entered as 0e and we used the operator f! to store the value into velocity. If we now want to increment the value of velocity by 9.8, we can enter

velocity f@ 9.8e f+ velocity f!

(kForth does not have a word called f+!, but you may define such a word!). To print a floating point value on the stack, use the word f. as explained previously. For example,

velocity f@ f.

will print the value 9.8.

Integer constants are defined as follows

value constant name

To define a constant called megabyte, for example, enter

1048576 constant megabyte

I often can't remember how many bytes there are in a megabyte, so I would have written instead

1024 1024 * constant megabyte

Now, type the name of the constant and print the top item on the stack

megabyte .

and you will see printed the value 1048576. Typing the name of the constant retrieves its value (not an address) onto the stack.

Floating point constants are defined in a similar fashion

fvalue fconstant name

To define a constant containing the acceleration due to gravity, 9.8 meters per second squared, type

9.8e fconstant g

The name of the constant is g. Typing

g f.

will print 9.8. Now, let's add the value of g to the value of velocity and print the result to illustrate the use of floating point variables and constants

velocity f@ g f+ f.