text
stringlengths
0
99.6k
Will push the address which contains the address of the next Forth word to
be executed to the parameter stack. (This will be the third and fourth bytes
past the address of the current word being interpreted.) Obviously, by
storing new values in the IP, and then jumping to NEXT , you can also force
the execution of a particular definition. This technique is also rarely
used. Typical uses for the IP are mainly accessing inline data.
Accessing Variables from Code definitions.
Since variables leave the address of their parameter fields on the stack,
it is a simple matter to access these variables from CODE level definitions.
Here is an example:
VARIABLE FOO
CODE FOO+1 FOO LDA, CLC, 1 # ADC, FOO STA, CS IF, FOO 1+ INC, THEN
END-CODE
This is a common way to increment the value at a memory location. The
assembler also provides a way to access USER variables, using the UP (user
pointer constant). User variables must be accessed using an offset, so the
)Y addressing mode is recommended. You will need to know the offset of the
user variable you need to access in advance, of course.
MISC.
Since the Assembler is co-resident with the FORTH system, all the power of
FORTH is available to you when using the Assembler. As one example:
FOO @ 2 3 */ # LDA,
will initialize the accumulator to two thirds of the value stored in the
variable FOO. (Of course, this must be 255 or less.) Occasional conflicts
may arise, however. In particular, a common error is to confuse the
assemblers 0= and 0< words with Forths - they are not the same. If you wish
to use the Forth versions while assembling, you must explicitly enter the
FORTH vocabulary, do your Forth thing, and then re-enter the assembler
vocabulary. Such conflicts are rare, and usually easily recognized.
The Forth assembler uses the standard MOS mnemonics for the 6502 op-codes,
but each mnemonic has a ',' attached. Thus, in forth, we write LDA, BRK, or
JMP, not LDA BRK and JMP. Also, the Assembler conditionals use the same
convention - IF, ELSE, THEN, and not IF ELSE THEN . A common error is to
omit the comma from one or more of these conditionals. I am personally not
wild about this convention, but the first Forth assemblers used them, and
now we are stuck with it.
You may have noticed that I have not used the JSR, RTS, instructions. This
is because typically, CODE definitions are called from higher level words,
and must end with a JMP, to NEXT or the equivalent routine. It is possible
to write programs which are structured in this way. Typically, the
subroutines are given names with CREATE, and then called from a higher level
code definition. Here is an example:
CREATE BLETCH ASSEMBLER NOP, NOP, RTS,
Note that when we do this, we must invoke the Assembler vocabulary
explicitly. BLETCH would be typically called from a higher level CODE
definition:
CODE THE-GREAT-RTS-HACK BLETCH JSR, NEXT JMP, END-CODE
This is only occasionally necessary however - usually when writing code that
is extremely time critical, such as graphics code.
Finally, it is possible to exit a code definition by jumping to yet another
code routine. You must remember to restore the X register (if you have
altered it) and it is also wise to reset the Y register to 0, since many
code level definitions will assume that this is the case. As a simple
example, here is a word that simply simply does a JMP, to Forth's KEY
routine:
CODE CALL-KEY ' KEY @ JMP, END-CODE
The tick (') gets the address of KEY's code field, and the @ fetches the
address of the routine which is stored in KEY's code field. This is the
recommended technique. It is more portable, and also safer than others which
one sees. Note that you cannot call hi-level Forth words using this
technique - only code level definitions may be called in this way. If you
think you want to exit a CODE definition with a call to a higher level Forth
word, think again. If you still need to do it, then start tinkering with the
IP and W registers.
I think that about covers it. I have tried to cover all of the basics, and
many of the more advanced techniques in combining assembly language and
Forth. If some of it seems obscure, it is probably my explanation, since
hacking in CODE is really no more difficult than hacking in any language -
it just runs faster.
Good Luck
And
Happy Hacking!
SDB
This is in answer to the numerous users of Blazin' Forth who want a full screen editor, but who have had some trouble getting one up on their systems.
The approach used here requires altering LIST (screen #5), and adding a few lines of code to screen #41. Please note: You must have the source code do make these changes.
The changes to LIST are indicated below. The only changes are the addition of DECIMAL in line 9 , and the addition of 2 .R ." > " in line 11. This will cause the screens to list in the format shown below, with line numbers as 0> instead of just a number.
scr #5