text
stringlengths
0
99.6k
CODE TRUE ( -- -1 ) DEY, ( Y REG now holds $ff)
TYA, PHA, PUSH JMP, END-CODE
Here is an even shorter definition which will leave a 0 on the parameter
stack:
CODE FALSE ( -- 0 ) TYA, ( set A register to 0 )
PHA, ( set up for PUSH )
PUSH JMP, END-CODE
Life being what it is, you will often wish you could use the X register.
There is a way. You can use the system storage location XSAVE, to
temporarily save the value of the X register while you are doing other
things. You must remember to restore the X register before exiting, however.
A typical sequence is:
XSAVE STX, ( stuff that changes x ) XSAVE LDX, NEXT JMP, END-CODE
Why it looks so strange....
The main reason the Forth Assembler looks so strange is that it is
reverse polish, like all of Forth. Operands *preceed* the operators. Here
are some examples that should make it clear:
Conventional Assembler Forth's Assembler
===================== ================
LDA # 0 0 # LDA,
ROL A .A ROL,
STA ADDRESS,X ADDRESS ,X STA,
STA (ADDRESS,X) ADDRESS X) STA,
LDA (ADDRESS),Y ADDRESS )Y LDA,
JMP (INDIRECT) INDIRECT ) JMP,
JMP ADDRESS ADDRESS JMP,
LDA ADDRESS ADDRESS LDA,
While admittedly unusual, it does make the best use of the stack at assembly
time.
The other major difference is that the Forth Assembler does not use labels.
There are no branch instructions - the Forth Assembler uses a structured
code approach to control flow. It does this by using analogues of the
hi-level IF THEN ELSE etc. to control the flow of your CODE definition. To
take advantage of this, you must specify the condition code you want tested.
You specify this condition code by using any of the following words:
CS test if carry set
0< test if negative flag set
0= test if zero flag set
VS test if overflow flag set
You can follow these condition code specifiers with not, to test for the
opposite condition:
CS NOT test if carry clear
0< NOT test if negative clear
0= NOT test if zero flag clear
VS NOT test if overflow flag clear
Below is an example of a possible definition of 0= , which leaves true if
the top of the stack is 0, and false (0) if it is anything else:
CODE 0= ( N -- FLAG ) BOT LDA, BOT 1+ ORA, 0= IF, 255 # LDA, ELSE, 0 # LDA,
THEN, PHA, PUT JMP, END-CODE
In the above code, we first test for 0 by ORing the two bytes which make up
the top of the stack together. The result will be zero only if both are
zero. We then test the zero flag (with 0=). If the byte is 0, we LDA with
255, otherwise, we LDA with 0, and replace the top of the stack with the
flag by jumping to the PUT exit routine. Note that we could make this
definition much shorter by taking advantage of the fact that the Y register
is zero at entry:
CODE 0= ( N -- FLAG ) BOT LDA, BOT 1+ ORA, 0= IF, DEY, THEN, TYA, PHA, PUT
JMP, END-CODE
You can use the same type of tests to do conditional loops. Here is a do
nothing example that simply wastes some time in a loop:
CODE WAIT ( -- ) BEGIN, DEY, 0= UNTIL, NEXT JMP, END-CODE
This simply decrements the Y register until it becomes zero. In the
original Forth assembler for 6502 machines, the BEGIN, UNTIL, structure was
the only one available. The BFC has extended this to include BEGIN, WHILE,
REPEAT, and BEGIN, AGAIN, . The BEGIN, AGAIN, loop is infinite - you must
JUMP out of it in the middle somewhere.
Accessing the Stacks.
Typically, most routines only need to access the top two elements of the
parameter stack. Since this is so common, special words have been provided
to make life easier here. BOT references the top of the stack (which is
lower in memory, and so the BOTtom of the stack). SEC references the SECond
element of the stack. It's important to remember that a Forth stack entry is
16 bits, or two bytes, so to obtain the whole stack element, you need to do
two fetches or stores. Here is a sample implementation of DUP:
CODE DUP ( N -- N N ) BOT LDA, PHA, BOT 1+ LDA, PUSH JMP, END-CODE