I will not give a full description of Forth but rather a small picture how it works and is used. Forth is also a set of flavours that differ in some parts. The Jupiter ACE has FORTH-79 as its base but lacks some of its words, it has also some additional words for its own purpose, i.e using tapes as storage.
For a better understanding about Forth read a book like Leo Brodie's Starting FORTH or try the web-tutor for 4tH (Jan98) http://www.geocities.com/SiliconValley/Bay/2334/4thtutor.htm
vlistand press enter. This set of words can easily be extended. Example:
: hello ." Hello, world!" cr ;This example show how a word
hellois defined and that it will when executed print
Hello, world!on the screen. The colon means compile word and the word after it, here
hello, will be the name of the new word. The
."is also a Forth word because Forth interprets all clusters of non-space characters as words. The word
."means (will mean - in a strictly forth manner) print to screen all characters up to the first
"and here that will be
Hello, world!. The
"is the end-of-output delimiter and the next word will be
crthat means output a carriage-return character. The last word
;means end of the compiled words actions.
A word can be defined several times. A new definition does not remove the old one but rather means that any new use of the word will use the new definition. Example:
: abc ." old definition" cr ; : test abc ; : abc ." new definition" cr ; testIn this example the word
testuses the word
abcwhich by the order of defining is the word that prints
old definition. When the new
abcis defined it will not alter
testor the old
abcbut rather declare a new
abcfor future usage. Because Jupiter ACE doesn't make use of whats known as the EDITOR, that is used during software development, it has a special word
redefinethat will change old words and their usage to new ones. After
testin the example above print
A word that can't be found in the vocabulary will Forth try to
interpret as a number and place on the stack. If the word can't be
interpreted as a number will Forth print a
? at the word.
.will print what is on top of the stack (and remove it). Example:
3 2 1 . . . will print 1 2 3The basic operators
*uses two values from the stack and places the result on the stack. Example:
1 1 + . will print 2 3 1 - . will print 2 4 2 / . will print 2 2 3 * . will print 6Forth work with integers, here 16 bit. The Jupiter ACE has extra build in words to handle floating-point numbers. These are
ufloat, and to input a floating-point number add a
.at the appropriate place, i.e
1.for 1 or
.1for 0.1. Note for the floating-point values that they occupy two values (items) on the stack.
The stack can be manipulated in different ways. Words for this are;
dup duplicates the top of stack
swap exchanges the two values on top of the stack values.
removes the top of the stack value.
rot rotates the three values on top
of the stack making the value on top of the stack the second value, the
second value the third and the third value on to of the stack.
takes the value on top of the stack and looks up that number of steps
down the stack and gets that value. Examples:
1 dup . . will print 1 1 2 1 swap . . will print 2 1 2 1 drop . will print 2 3 2 1 rot . . . will print 3 1 2 4 3 2 1 3 pick . . . . . will print 3 1 2 3 4Example of value passing through the stack.
: add4 4 + ; : add8 add4 add4 ; 5 add4 . will print 9 5 add8 . will print 13Forth has also a second stack called the return stack. This stack is used to store return addresses during execution of a called word and other misc things. The words
>ris often used to move values back and forth between the stack and return stack.
>rmeans move to the return stack and
r>means move to the stack from the return stack.
: max.x 63 ;which is a word
max.xthat places the value 63 on the stack. Because this a common thing to do Forth has a word just for creating constants and its name is
63 constant max.xThis does the same as the previous example. A variable is similar but the user is able to change its value. When the variables name (word) is executed it rather gives a value that can be used to access the value of the variable, both for reading and writing. Example:
12 variable x x @ . will print 12 13 x ! will set x to 13 x @ . will print 13The word
variabletakes a name and a value from the stack and allocates some memory for the variable. When the new word for the variable is executed it places a value on the stack (the address). The word
@takes a value from the stack and looks up in memory the value stored at that address. The word
!takes two values from the stack, the top of stack is the address where the second value will be stored.
: test 100 0 do ." >" loop ; test will print 100 > charactersThe word
dotakes two values from the stack. The top of stack is the starting value (here 0) and the second (here 100) the value when the repetition should be ended. With the word
ican one access the current value for the repetition. Example:
: test 10 4 do i . loop ; test will print 4 5 6 7 8 9Note that the 10 is not printed in the example above. Note also that the word
loopincreases the value by one all the time. There is a special variation of the word
+loopwhich takes a values from the stack and uses that as the increment. Example:
: test 0 5 do i . -2 +loop ; test will print 5 3 1More complicated repetitions are available with the words
repeat. The word
beginmarks where the repetition begins (returns to). The word
untiltakes a value from the stack, if it is zero (false) it will repeat the loop from the word
: test begin dup . 1- dup 0= until drop ; 5 test will print 5 4 3 2 1The word
1-is an optimized shortcut for the pair
-. The word
0=is an optimized shortcut for the pair
=. The word
=is a test operator that takes two values from the stack and if they are equal it will place a one (true - or minus one depending on the Forth) on the stack and if they are not equal it will place zero (false) on the stack.
The next repetition construction uses the words
repeat. The word
begin work like in the
until case but in this
case will the word
repeat unconditionaly repeat the loop from the
begin. The word
while is placed somewhere inbetween a
repeat and it takes a value from the stack. If the value is
zero (false) it will leave the repetition and continue executing after
repeat. If the value is not zero (true) it will not do
anything which in effect means that it will continue to the word
and there repeat the repetition from the word
: test begin dup while dup . 1- repeat ; 5 test will print 5 4 3 2 1For simple decisions there are the words
then. The word
iftakes one value of the stack. If the value is zero (false) will
ifignore everything until the word
elseis missing) and continue executing after the word
else. If the value is not zero (true) will everything inbetween the word
ifand the word
elseis missing) be executed and everything inbetween the word
elseand the word
thenwill be ignored. Example:
: test 0= if ." in" then ." complete!" ; 0 test will print incomplete! 1 test will print complete! : test if ." True!" else ." False!" then ; 0 test will print False! 1 test will print True!Note that
ifconstructions can be nested. Example:
if -----+ if ---+ | else ---+ | if -+ | | then -+ | | then ---+ | else -----+ if -+ | then -+ | then -----+
save <name>saves the vocabulary as <name>
load <name>loads the vocabulary <name>
s l bsave <name>saves l bytes from the memory starting at address s as <name>
s l bload <name>loades 0 bytes to the memory starting at address s as <name>. If s or l is zero will their value be taken from the file.
list <word>lists the definition of <word>
edit <word>loads the <word> so the user can edit it. When the user presses enter will a new definition be compiled. Use
redefineto update the new definition for older usages of the word.
redefine <word>removes the old definition of <word> and updates the older usages of it.
f+, f-, f*, f/, f., fnegate, int, ufloat
ascii <word>puts the first letter in <word> on the stack as its ascii value
l c atsets the next printing position on the screen, l can be from 0 to 22 and c can be from 0 to 31.
T t beepsounds the beeper with tone T (specially calculated value) for t milliseconds
addr callwill call Z80 machine code at addr, should be terminated with a
jp (iy)Z80 instruction.
x y m plotplots a dot at position (x,y) with mode m. (0,0) is the lower left corner and (63,45) is the upper right corner.
definer <name> ... does>
: <name> CREATE ... DOES>
compiler <name> ... runs>