text
stringlengths
0
99.6k
First we fetch the low byte of the top of the stack with BOT LDA, . This is
pushed onto the return stack, as required by the exit routine PUSH . Next we
get the high byte with BOT 1+ LDA, . That's all there is to it. As another
illustration, here is an implementation of OVER:
CODE OVER ( A B -- A B A ) SEC LDA, PHA, SEC 1+ LDA, PUSH JMP, END-CODE
The actual addressing mode being used here is "zero-page x". In
conventional assembler, BOT LDA, would be written LDA 0,x while BOT 1+ LDA,
would be written LDA 1,X . To access deeper stack elements, you can keep
adding values to BOT or SEC , or you can use the addressing mode explicitly:
BOT 4 + LDA
or
4 ,X LDA
While not used often, it is also possible to address directly into the
return stack. Typically, you would access the return stack using the PLA,
instruction. This has the side effect of altering the stack pointer, and you
can also only access the top of the return stack. To access arbitrary
bytes, you can use RP) . To do this, you must first save the X-Register in
XSAVE and then execute TSX, which will move the stack pointer into the
X-Register. You can then do RP) LDA, which will fetch the current top of the
return stack. To get deeper into the return stack, offset RP). Below is an
example which non-destructively moves the address on the return stack to the
top of the parameter stack:
CODE GET-RETURN-ADDRESS XSAVE STX, TSX, RP) LDA, PHA, RP) 1+ LDA, XSAVE
LDX, PUSH JMP, END-CODE
One of the easiest and quickest ways to crash any system is to garbage the
return stack. If you need to access the stack, go for it, but use care.
SETUP and N
It is often useful to be able to access absolute memory locations. To
this end, Forth provides an 8 byte temporary data area which is referred to
as the N area. You may initialize this area yourself, or you may call SETUP
to move stack elements to the N area. To use SETUP, you must load the
accumulator with the number of stack elements you want to move (NOTE: the
number of stack elements, NOT the number of bytes) and then do a JSR to
SETUP. SETUP will pop the elements off of the stack, and move them to the N
area. Since there are only 8 bytes in the N area, you have room for at most
4 stack elements. As a simple example, the following will pop the top two
elements off of the stack, and move them to the N area:
CODE MOVE2 2 # LDA, SETUP JSR, END-CODE
The previous top of stack (BOT and BOT 1+) will be stored at N and N 1+ ,
while the second element (SEC and SEC 1+) will be at N 2+ and N 3 +. Once
they have been moved there, you can carry out operations on them, or use
them in the indexed indirect Y addressing mode. ( LDA (N), Y in conventional
assembler, or N )Y LDA, in Forth assembler.) By far the most common error
in using SETUP is to forget that it also pops the elements of the stack as
it moves them.
;CODE
If CODE is the Assemblers equivalent to the hi-level colon, ;CODE is the
assemblers equivalent to DOES>. As an example of the use of ;CODE, we will
write our own versions of CONSTANT - one in high level forth, using DOES>,
and one in low level, using ;CODE. First the hi-level definition:
: CONSTANT CREATE , DOES> @ ;
As a quick review, remember that CREATE (or a word that uses CREATE) creates
a dictionary entry for the next word in the input stream. Words created in
this way all have the same run-time behaviour - they leave the address of
their parameter field on the stack. Note that CREATE by itself does not
allocate any parameter field space in the dictionary, you must do this
yourself by using C, , or ALLOT . The DOES> word allows you to manipulate
the values in the parameter field, essentially allowing you to define a
special set of related words which share the same run-time behaviour. Lets
walk through the process:
10 CONSTANT TEN
When Forth executes the above line, the number 10 will be left on the stack,
and CONSTANT will be executed. The first word in CONSTANT is CREATE, which
will take the first word it finds in the input stream (TEN in this case) and
create a header for it in the dictionary, with its associated link field,
name field and code field. The code field written by CREATE will cause the
address of the first byte of the parameter field to be left on the stack
when TEN (the word defined by CONSTANT) is executed. Next , is executed,
which takes the top of the stack, and compiles this number in the
dictionary. DOES> does something mysterious (more on this later) which
causes the words following DOES> to be executed at run-time. This is the
compile time behaviour of CONSTANT. Now, when TEN is executed, the run-time
behaviour of CONSTANT will occur. The parameter fields address is pushed
onto the stack, and then the words after DOES> are executed. In this case,
the single word @ will be executed, which replaces the address on the top of
the stack with the value at that address.
TEN . 10 OK
Now for the ;CODE version:
: CONSTANT CREATE , ;CODE 2 # LDY, W )Y LDA, PHA, INY, W )Y LDA, PUSH JMP,
END-CODE
Notice, by the way, that no concluding semi-colon is required when you use