of memory. Other words are provided to deal with byte length values and 32-bit. Movement of longer strings of data about the memory is achieved by using the stack in a different way. Instead of piling data up on the stack and then disposing of it the words MOVE and CMOVE expect to find 3 parameters on the stack, a source address, a destination address and a count for the number of data items to be moved. For instance the phrase:
HERE PAD 10 CMOVE
would move 10 bytes from HERE (the top of the dictionary) to PAD, STARTING with the 1st byte in the string. Some Versions of Forths provide the word <CMOVE which moves the last byte first to avoid overwriting data with short range moves towards high memory. If your particular Forth doesn't have one then you'll probably need to write your own.
While only two named data structures come with the Forth, others may quite readily be added due to the extendibility inherent in it. This is achieved by creating new defining words. VARIABLE and CONSTANT are known as defining words in Forth, they define a class of words which behave in the same manner when compiled and also when they execute ie all CONSTANTS remove a value from the stack during compilation, and all execute so as to place that value on the stack. The principle tools in creating new defining words are CREATE (<BUILDS in Fig-Forth) to define the way in which words will compile, and DOES> which defines the way in which they will execute.
CREATE may be used as follows:
CREATE STORE 20 ALLOT
as a compiler which creates a dictionary heading called, in this case, STORE. The ALLOT allocates 20 bytes of storage following the header. On execution STORE acts to place the address of the first byte on the stack. To use this, for example to hold an ASCII string entered from the keyboard, the phrase:
STORE 20 EXPECT
might be used, and to output to screen or printer:
STORE 20 TYPE
CREATE may also be used within a colon definition to create a class of words. For instance, if VARIABLE did not exist one way to create it would be:
:VARIABLE CREATE 2 ALLOT;defines
VARIABLE
VARIABLE COUNTER defines COUNTER
0 COUNTER ! initialize to zero
COUNTER on execution performs the runtime code for CREATE, placing the address immediately following the dictionary header on the stack, and there by doing everything expected of it. Another example would be:
:BYTES CREATE ALLOT ; (define bytes)
20 BYTES STORE (define store)
STORE 20 ERASE (initialize store)
BYTES expects a number on the stack which it uses to ALLOT dictionary space. Here a 20 byte string named STORE is created. When executed STORE also uses CREATE's behaviour pattern and leaves the start address ready for ERASE.
FORTH COMPARISONS
WORD
STACK
DESCRIPTION
=
(n, n _ _ f)
COMPARES THE TOP TWO ITEMS ON THE STACK. LEAVES A TRUE FLAG IF THEY ARE EQUAL ELSE FALSE.
<
(nf, n2 _ _ f)
COMPARES THE TOP TWO ITEMS ON THE
STACK. LEAVES A TRUE FLAG IF n I
IS LESS THAN n2 ELSE FALSE.
>
(n1, n2 _ _ f)
COMPARES THE TOP TWO ITEMS ON THE
STACK. LEAVES A TRUE FLAG IF n I IS
GREATER THAN n2 ELSE FALSE.
0=
(n _ _ f)
LEAVES A TRUE FLAG IF THE TOP
ITEM ON THE STACK IS EQUAL TO
ZERO, ELSE FALSE.
0<
(n _ _ f)
LEAVES A TRUE FLAG IF THE TOP
ITEM ON THE STACK IS LESS THAN
ZERO, ELSE FALSE.
0>
(n _ _ f)
LEAVES A TRUE FLAG IF THE TOP
ITEM ON THE STACK IS GREATER THAN
ZERO, ELSE FALSE.
FORTH LOGICAL OPERATIONS
WORD
STACK
DESCRIPTION
OR
(f, f _ _ f)
LEAVES A TRUE FLAG IF EITHER OF THE
TOP ITEMS ON THE STACK ARE TRUE, FALSE
IF THEY ARE BOTH ZERO.
AND
(f, f _ _ f)
LEAVES A TRUE FLAG IF BOTH OF THE TOP
ITEMS ON THE STACK ARE TRUE, FALSE IF
EITHER OF THEM ARE ZERO.
NOT
(f _ _ f)
REVERSE THE LOGICAL VALUE OF THE TOP STACK ITEM.
XOR
(f, f _ _ f)
LEAVES A TRUE FLAG IF ONLY ONE OF THE TOP
TWO ITEMS ON THE STACK IS TRUE, BUT NOT IF
THEY ARE BOTH TRUE (Exclusive OR)
ERASE expects a start address and byte count on the stack and clears the appropriate memory area to zero.
DOES is one of the most remarkably useful tools in the Forth system. It may only be used inside a colon definition and performs two.
functions. Consider a would-be definition for CONSTANT:
:CONSTANT CREATE , DOES> 2 ;
The phrase between CONSTANT and DOES is what CONSTANT executes. The , serves to compile a 16-bit value from the stack into the dictionary. The code for CONSTANT is terminated by DOES>. This also marks the start of the code for the word defined by CONSTANT. CREATE still leaves that address on the stack and this is used by a to push the compiled value of the constant onto the stack. The code following the DOES may be of any length and is terminated
The box labelled 'Forth Logical Operators' shows the logical operators that can be used to act on information stored in the stack, while 'Forth Comparisons' shows how various words in the Forth vocabulary compare bits of information on the stack and how they relate their findings about those bits of information.