text
stringlengths
0
99.6k
What this file is:
A lot of the mail I have been getting about Blazin' Forth concerns the
use of CODE and ;CODE definitions, and combining assembly language with
Forth. It recently occurred to me that a lot of people don't know how to
use the Forth assembler - and are therefore missing out on a considerable
amount of the power of the Forth language. So this is an attempt at a
tutorial in using Assembler in Forth, and in using it in the BFC in
particular. Much of what I say here should be applicable to other 6502
Forth implementations - but not necessarily all. Consider yourself warned.
What this file is not:
This is *not* a course in machine language programming. If you need
information on the basics of using assembly language, then consult a good
text - my personal favorites are the ones by Lance Leventhal, but there are
many good ones around.
General Considerations:
Using CODE and ;CODE words are frowned upon in programs which attempt to
be portable. In fact, the quickest way to insure that your programs are not
83 Standard is to use the word CODE in them.
Having said this however, there is much to be said for incorporating a
few code definitions in Forth programs. Typically, a program will spend
most of its time in one or two words - by recoding these words in assembler,
the overall speed of the program can be increased manyfold - 50% increases
or more are not unusual, and for very little work.
Also, the incompatibility is not as great as it would appear at first.
It is probably safe to say that the most popular 8-bit personal computers
use the 6502 CPU (Apple II, C64 C128, Atari line etc.). As long as your CODE
definitions are not accessing special hardware features (such as the SID
chip in the C64) most code definitions will work on all computers using the
same CPU. I have shared CODE definitions with friends who have Apples and
Ataris running Forth, and I have never yet had to modify one.
Finally, Forth is the easiest language there is to combine with machine
language. Many of the hassles and problems ordinarily associated with
combining high level languages and assembler simply do not occur in Forth.
In other languages, the typical process is this:
1: Write the high level program, and debug.
2: Figure out where you need to speed things up.
3: Fire up an editor, and write some assembly code.
4: Assemble the file and - usually - run a loader program on that output.
5: Load the Editor, and modify your higher level source.
6: Compile your higher level source.
7: Link the compiled program and the loaded assembly language program.
8: When it doesn't work (which it won't, at first). Go back to step 3.
There are also inevitable problems in passing parameters to and from the
higher level code, and the inevitable problem of where to put the machine
language. If you have ever done much of this sort of thing, you know what a
headache these problems can be.
These last two problems are usually the most difficult, and, you will be
surprised, and possibly relieved to hear that they don't occur in Forth. At
all. Ever.
Combine that with a resident assembler and a resident editor and a
resident compiler, writing Assembly language in Forth becomes almost
ridiculously easy.
A Quick Example:
Since I have taken up so much of your time with the above advertisement,
you would probably like me to put my money where my mouth is. Here is a
quick example of what I was talking about above. I hope it's not too
trivial, but the idea I want to get across here is the ease of combining
Assembler with Forth. First, a high level Forth Program:
: SHIFTLEFT ( -- N2 N1 ) // shift N2 left N1 times
0 ?DO 2* LOOP ;
: SHIFT-4 ( Just show what a left shift is )
100 0 DO I 4 SHIFTLEFT . LOOP ;
Multiple left shifts are fairly common, and so SHIFTLEFT is likely to be a
handy word in certain applications. As it is, it will run pretty quickly.
But perhaps, for certain speed demons, not quickly enough, so you decide to
recode the SHIFTLEFT primitive in Assembler:
CODE SHIFTLEFT ( -- N2 N1 ) // shift N2 left N1 times
BOT LDA, TAY, BEGIN, 0 # CPY, 0= NOT WHILE,
SEC ASL, SEC 1+ ROL, DEY, REPEAT, POP JMP, END-CODE
If you are used to conventional assemblers, this probably looks pretty
weird. The important thing to notice here is that *only* SHIFTLEFT has
changed - SHIFT-4 (or any other word which uses SHIFTLEFT) will work just as
it did before, with the only change being the overall increase of speed
which machine language naturally brings to any situation. Notice also that
we didn't have to worry about where to put the code - it goes in the same
spot our higher level SHIFTLEFT went. You will also discover, if you type
this example in, that you don't have to call the assembler. This is all
taken care of for you. As far as you, another user, or other procedures
which use SHIFTLEFT are concerned, there is no difference between using the
hi-level SHIFTLEFT and the CODE SHIFTLEFT.
The Structure of a CODE definition:
It's pretty straightforward. They all look the same:
CODE [name] [assembler mnemonics] END-CODE
Note the similarity to a colon definition:
: [name] [forth words] ;
How to Exit a Code Definition: