text
stringlengths 0
99.6k
|
|---|
One thing you must remember is that you have to explicitly leave a CODE
|
definition by doing a JMP, to another code level routine. This is probably
|
the single most common error made by newcomers. Possibly it is caused by
|
making a false analogy between the higher level ; and the code level
|
END-CODE. While the Forth word ; does in fact get you back to where you came
|
from, END-CODE does not. In fact, END-CODE does nothing at all at run-time.
|
You can exit a CODE definition by doing a JMP, to any of the following:
|
NEXT POP POPTWO PUSH or PUT . These are described below:
|
NEXT
|
NEXT is commonly called the address interpreter. It is the word that is
|
responsible for the execution of all Forth words. ALL words in Forth
|
ultimately end up here. Doing a NEXT JMP, will cause the current code
|
definition to stop and return to the word that called it. All of the
|
following exit points end with a jump to NEXT . In what follows, remember
|
that a "stack element" refers to a 16 bit quantity -- i.e. two bytes.
|
POP
|
POP first drops the first element of the stack, and then jumps to NEXT.
|
Same as DROP in hi-level forth.
|
POPTWO
|
POPTWO drops the top two elements from the stack, and then jumps to NEXT.
|
Same as 2DROP in hi-level.
|
PUSH
|
PUSH lets you leave a result on the top of the stack. PUSH expects the
|
low byte of the new top of stack to be on the return stack, and the high
|
in the accumulator. PUSH will leave these as the new top of the parameter
|
stack (the former top will then be the second element). PUSH then calls
|
NEXT. Since this routine is somewhat more complicated than the others here
|
is a typical sequence:
|
PHA, ( push low byte to return stack )
|
TYA, ( assume high byte is saved in Y register, move it to the A reg)
|
PUSH JMP, END-CODE ( parameters set, so jump to PUSH)
|
PUT
|
PUT replaces the top of the stack with a new value. You setup PUT in
|
the same way as you setup for PUSH - the new lobyte is pushed to the return
|
stack, and the new hibyte is in the accumulator before the call to PUT. The
|
only difference between the two is that PUT replaces the present top of the
|
stack, while PUSH creates a new top of stack.
|
Register Usage:
|
Since Forth uses two stacks, and the 6502 CPU only implements one
|
hardware stack, the parameter stack must be "artificially" maintained. In
|
this implementation (as in most) it is located in the zero page, and the X
|
register is used as the parameter stack pointer. The machine stack is
|
Forth's return stack. Therefore instructions which affect the X register or
|
the machine stack should be used with care. At entry to a CODE defintion,
|
the X register points to the top byte of the parameter stack. This stack
|
grows downward in memory, so decrementing the X register will make room
|
for another element on the stack, and incrementing the stack pointer will
|
remove an element from the stack. Here is a diagram which shows the
|
situation when two elements are on the stack:
|
Hi Memory
|
**************
|
* hibyte2 *
|
* lobyte2 *
|
**************
|
* hibyte1 *
|
X --> * lobyte1 * Top of Stack
|
**************
|
Notice that the two bytes which make up one stack entry are stored in the
|
usual 6502 order, with the lobyte lower in memory. To remove the top element
|
of the stack, we can define the code word DROP:
|
CODE DROP ( N -- ) INX, INX, NEXT JMP, END-CODE
|
Which in fact, is exactly the the way DROP is defined in the BFC. After DROP
|
has been executed, the stack looks like this:
|
Hi Memory
|
*************
|
* hibyte2 *
|
X --> * lobyte2 * New top of Stack
|
*************
|
We will return to this topic later, when we talk about accessing the
|
parameter stack in more detail. For now, the main point is to remember that
|
when Forth starts executing your code definition, the X register will
|
contain a pointer to the top of the stack. You can use the X register to
|
access the stack, or to remove elements from the stack, but when your code
|
definition is finished, other Forth words, and the Forth system itself is
|
going to expect the X register to contain a valid stack pointer, so don't
|
change it wantonly. You should also remember that since each stack entry is
|
two bytes, only even multiples of the INX, or DEX, instruction make sense.
|
(I.E. INX, INX, INX, INX, not INX, INX, INX, - the first will drop two stack
|
elements, while the second will drop 1 and 1/2 stack elements - and cause
|
the Forth system to behave oddly.)
|
Both the Accumulator (A reg) and the Y register may be freely used. The A
|
register will contain garbage, and must be initialized, but the Y register
|
is guaranteed to be 0 on entry to your code, and you may take advantage of
|
this fact or not, as you wish. Here is a short code definition that will
|
leave the value of -1 (Forth's canonical TRUE flag) on the stack. It uses
|
the PUSH routine described earlier.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.