text
stringlengths 0
99.6k
|
|---|
;CODE.
|
When ;CODE is executed (for example, by typing 10 CONSTANT TEN), it will
|
REWRITE THE CODE FIELD OF THE WORD BEING DEFINED! This is extremely
|
important to remember. All Forth words have code fields that define their
|
run time behaviour, with colon definitions all sharing the same code field,
|
variables sharing the same code field (different, of course, from that of
|
colon definitions and constants) and so on. When CREATE executes in our
|
CONSTANT definition, it creates a code field that contains the address of a
|
routine which will push the address of the word being defined to the stack.
|
When ;CODE executes in our CONSTANT definition, it will search out this code
|
field, and re-write it with the address of the machine language routine
|
which immediately follows ;CODE. This means that the only run time behaviour
|
of a word which contains a ;CODE termination is determined by you. Even
|
though there is a CREATE in the lo-level definition of CONSTANT, it will not
|
deposit the address of the parameter field on the stack, because ;CODE has
|
changed the code field to point to our machine language routine. An
|
illustration may make this clearer:
|
Header created by CREATE:
|
**************
|
* Link Field *
|
**************
|
* TEN * Name field - stores ASCII characters of defined name.
|
**************
|
* $0900 * $0900 = address of routine to push parameter field on stack
|
**************
|
* 10 * Parameter field - stores value of constant
|
**************
|
Note that $0900 is just an example address - don't try jumping to it in your
|
own code.
|
When a word defined by CREATE is executed, the machine language routine at
|
$0900 (the address in the code field of the word) will be executed. This
|
routine is what causes the run-time behaviour of CREATE defined words - it
|
pushes the address of the parameter field to the stack. The above
|
illustration shows the state of the dictionary after the CREATE and , part
|
of CONSTANT have been executed. Now, suppose that the address of the first
|
instruction following ;CODE in CONSTANT ( the 2 # LDY, ) is at $8000 . When
|
;CODE executes in the course of defining a new constant, the header will
|
look like this:
|
Header after ;CODE has been executed.
|
**************
|
* Link Field *
|
**************
|
* TEN * Name field - stores ASCII characters of defined name.
|
**************
|
* $8000 * $8000 = address of your machine language routine
|
**************
|
* 10 * Parameter field - stores value of constant
|
**************
|
So, as always, when Forth executes TEN, the first thing to be executed is
|
the address of the routine in the code field, which no longer points to the
|
CREATE run time code, but to your machine language code.
|
Hope that is all clear enough. Now, to the particulars of the ;CODE part of
|
our low level CONSTANT definition (note that the following is implementation
|
dependent. Most present day Forths use an implementation along these lines,
|
but not all.)
|
Forth depends on two registers for its performance, the IP and the W
|
register. When Forth is implemented on processors with more registers,
|
typically a processor register will be used for these Forth system
|
registers. However, there are not enough on the 6502, so zero page locations
|
have been used instead. W will contain the address of the code field of the
|
word currently being executed. For example, if the code field of TEN is
|
located at $9000, the W register will contain $9000, or the address of the
|
address of the routine to be executed. The ;CODE part of CONSTANT uses this
|
fact to access the parameter field of the current word. Since the code field
|
of an address, is always two bytes long, the first byte of the parameter
|
field is located at $9002. Given this information, it becomes a simple
|
matter of indirectly indexing from W. Loading the Y register with 2, and
|
then performing W )Y LDA, (LDA (W),Y in conventional assembler) will get the
|
first byte of the constants value. The next byte is at $9003, so we simply
|
increment Y, and indirectly index again to get the next byte. The rest of
|
the definition consists of setting up for PUSH, which was covered earlier.
|
Phew! Hope that was clear enough. I think you can see that indexing from W
|
can get you any byte in a definitions parameter field. W 1- contains an
|
indirect JMP instruction, so you can also vector control to other routines
|
by storing the address of the address of the routine in W, and then doing a
|
W 1- JMP, . This technique is rarely used, however.
|
There is an intimate relationship between the IP register and the W
|
register, and an effective use of these registers depends on a clear
|
understanding of how they work in the Forth system as a whole. Both of them
|
are changed by NEXT. When NEXT executes, the IP will point to an address
|
which contains the code field address of the next word to be executed by
|
Forth. NEXT fetches this address, and stores it in W. It then bumps the IP
|
(which stands for Interpretive Pointer) by two, so the next time around it
|
will be pointing to the next word to be executed. Finally, NEXT does a JMP
|
to W 1-, which causes an indirect jump to the address pointed to by W.
|
The IP is most useful for accessing things like inline data structures,
|
(character strings, and the like.) Since the IP is incremented by NEXT, the
|
code fragment:
|
IP )Y LDA, PHA, INY, IP )Y LDA, PUSH JMP,
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.