The Data Section

In addition to a code section, every word also has an optional data section. You can think of a word's data section as a list of slots, in which you can stuff various items: integers, reals, text, XTs, etc.

The list of slots is created right after the word is defined using the word

,
known as COMMA. COMMA pops the top item on the stack and inserts it into the last defined word's data section. This of course extends the length of the data section by one item. As an example, the program:
: hello "how are you?" . ;  41 , 34.0 , "Yo" ,
creates the word
HELLO
with 3 slots in its data section. These slots contain the items 41 (integer), 34.0 (real) and the text "Yo" on the last slot.

This process of creating data section slots using COMMA is known as initialization.

Fetch and Store

After initialization, you will often want to store and fetch data from a Word's data section:
WordAction
@
(Called FETCH).

Fetches the data from a given Word. There are two forms:

@ ( xt -- * )
and
@ ( xt n -- * )
In the first form, the item in the first slot is fetched and pushed onto the stack. In the second form, n is the slot index from which to retrieve the item. The first slot has index 0, the second slot has index 1 and so on. As an example, to retrieve the second item from the word
HELLO
(defined above):
' hello 1 @ .
Recall the index 1 represents the second item on
HELLO
's data section.

?

Print the first item (index 0):

? ( xt -- )
For example,
' hello @ . 
is the same as
' hello ?
.

!
(Called STORE).

STORE replaces data into an existing slot. This has also two forms:

! ( * xt -- )
and
! ( * xt n -- )
In the first form, the item in the first slot is replaced by the item on the stack. In the second form, the item at the given index n is replaced by what's on the stack.

Carrying on the previous example,

"gibraltar" ' hello !
will place the text "gibraltar" on the first slot on the word HELLO.

+!

Add to the first item (index 0):

+! ( * xt -- )
For example,
' hello @ 3 + ' hello ! 
is the same as
3 ' hello +!
.

Quiz

Question 1

Test out the programs above. Verify that you can retrieve the first item on 
HELLO
after it has been replaced with the text "gibraltar".



Question 2

Write a program with two words:

  1. A word called
    REF
    that has no action but has one data section slot. Initialize this slot to zero.
  2. A word called
    COUNTER
    that displays the number of times it has been executed.
Hint:
COUNTER
needs to use
REF
.

Remember to test your answer.



Question 3

Building on your answer in Question 2, create a new word 
INC
that increments the slot in
REF
. How can your word
COUNTER
be amended to use
INC
?

As always, remember to test your answer!



Question 4*

Define a word 
::
that can be used to simultaneously define new words and create their data section like so:
		32 :: xx "hello!" . ;
		
would create the word
XX
and initialize its data section to be a 32 element array.

As always, remember to test your answer!



Bonus Question*

What does this program do?

: : : 0 , ;

Build a program to test your answer.



Next: The Dictionary