Before giving an example of defining a TI Forth defining word with
;CODE , I should explain why you might want to use it in the first place.
The defining words that are part of the TI Forth kernel are
: (paired with
; ),
VARIABLE ,
CONSTANT ,
USER ,
VOCABULARY ,
<BUILDS (paired with
DOES> or
;CODE [not in kernel]) and
CREATE . The defining words
CODE and
;CODE are defined on the system disk and must be loaded with
-CODE to be used. Most words you would ever need to define can be created with the first three (
: ,
VARIABLE and
CONSTANT ). However, you too can use
<BUILDS and
CREATE , the same words used for defining most of the above, for the eventuality that these do not suffice.
In fig-Forth and TI Forth, it is not useful to use
CREATE on the command line unless you really know what you are doing because it creates a dictionary header in which the smudge bit is set and the code field points at the parameter field with no storage allotted for it. This means that the parameter field must be allotted with executable code (or the code field changed to point to some) and the smudge bit must be reset so a dictionary search can find the word. The same discussion obtains for
<BUILDS except for the smudge bit because
<BUILDS is defined as
: <BUILDS CREATE SMUDGE ; ( SMUDGE toggles the smudge bit.)
This situation is made easier by using
<BUILDS ,
DOES> and
;CODE within colon definitions as
: NEW_DEFINING_WORD <BUILDS ... DOES> ... ;
or
: NEW_DEFINING_WORD <BUILDS ... ;CODE ...
You simply replace the first "
..." with words you want to execute when
NEW_DEFINING_WORD is compiling a new word,
e.g., to reserve space for and store a value in the first cell of the parameter field using
, . You then replace the second "
..." with code to be executed when the new word actually executes. It will be this code to which the code field of the new word will point.
Here is an example of the use of
;CODE in the definition of a defining word,
i.e., a word that creates new words:
CONSTANT is a TI Forth word that defines a word, the value of which is pushed to the stack when the word is executed.
9 CONSTANT XXX
defines the word
XXX with 9 in its parameter field and the address of the execution code of
CONSTANT in its code field. TI Forth defines
CONSTANT in high-level Forth essentially as
: CONSTANT <BUILDS , DOES> @ ;
Using
;CODE and after loading the TI Forth Assembler with
-ASSEMBLER , it could also be defined with Assembler code as
: CONSTANT <BUILDS , ;CODE SP DECT, *W *SP MOV, NEXT,
which, once you know the machine code, can be coded without the Assembler loaded as
HEX
: CONSTANT <BUILDS , ;CODE 0649 , C65A , 045F ,
For
CONSTANT , the first definition is easier to understand. They are both the same length. They both create words of the same length. However, there may come a time when only Assembler will do your bidding and
;CODE offers that facility.
...lee