ColorForth mnemonics/syntax are sometimes different from T18. My opinion is that they're better.
There are some tricky things you can do, particularly with or and -.
Registers are:
Jump Instructions
Jump to address in register P, I or R
00 ;
Jump thru R (destructive)
The word ; is Forth syntax to end a definition. Here it doesn't necessarily mean end, but return
;
push ;
Useful for a vectored jump into a table
01 ;:
Jump thru R, save P in R
Sometimes called co-routine jump, it returns to caller while saving the current address. This can be repeated to
02 jump
Jump thru I
The opcode is not used explicitly. The compiler generates it
The address is in the instruction, either 10, 8 or 3 bits depending on slot
The compiler will turn a call followed by ; into a jump to save time and space
if . . else . . . then
Often more efficient than conditional jumps
03 call
Jump thru I, push current address to R
The opcode is not used explicitly. Referencing any defined word generates a call
word . . . ;
word
04 unext
Jump to slot 0
Discards the address left by for. Very efficient loop since no instruction fetchs required.
for unext
for +* unext
for 2* unext
for 2/ unext
for @+ !b unext
for @b !+ unext
for @p+ !+ unext
for @+ !p+ unext
05 next
If R is non-zero, jump thru I and decrement R. Otherwise pop R
For expects loop-count (-1) on stack and is defined as:
push begin
Begin fills the current instruction word with . so that the loop begins in the next word. Leaves address on compiler stack for next to use
for . . . . next
Prefer unext if only 3 instructions in loop
for @+ over or if drop swap next no match then match
Swap is executed at compile time to access the address for next
Divide operation: trial subtract and shift in either 0 or 1
06 if
Jump thru I if T is zero
If leaves its address on compiler stack for then to complete. Then may abort if address won't fit in slot
if . . . then
or if . . . then
07 -if
Jump thru I if T is positive
abs -if - 1 . + then ;
max - over . + - -if drop ; then + ;
min - over . + - -if + ; then drop ;
Memory Instructions
Fetch or store thru registers A, B, P.
@ and ! are Forth abbreviations for 'fetch' and 'store'. Fetch pushes the data stack; store pops it
08 @p+
Fetch thru P, increment P
Increment suppressed when executing from port
123
Uses 1 slot plus 1 word
100 200 300 400
Uses 4 slots plus 4 words
pi 314 ;
Uses 2 slots plus 1 word
1+ 1 . + ;
Uses 4 slots plus 1 word
09 @+
Fetch thru A, increment A
Increment suppressed when referencing port
0 63 for @+ . + unext
0a @b
Fetch thru B
io b! @b
0b @
Fetch thru A
io a! @
0c !p+
Store thru P, increment P
@b !p+
x! @p+ drop !p+ ;
x 100 ;
Avoids using an address register
0d !+
Store thru A, increment A
63 for @b !+ unext
63 for @p+ !+ unext
0e !b
Store thru B
Be careful to distinguish !b from b!
30003 !b
2000a !b
0f !
Store thru A
30000 !
ALU Instructions
Unary and binary operations:
10 +*
Multiply step: add S to T if A0=1 then shift T and A right
. +* . +* . +* . +*
If the multiplicand (in T) was shifted left 4 places, the result is aligned
push . begin +* unext
This construction is required to provide a . for the first +*; unext provides delay for the others
* a! 17 push . begin +* unext drop a ;
18-bit result
11 2*
Shift T left
dup 2* . +
dup 2* 2* . +
2* dup 2* . +
dup 2* dup 2* . + . +
@b 2* -if
2* 2* -if
12 2/
Shift T right; sign fill
2/ 2/
13 -
One's complement T
- -if
- 1 . +
- . +
This result is 1 too small. It can be corrected during additional arithmetic
- . + -
push - pop . + -
dup or -
dup dup - . +
dup or - 2*
dup or - 2* -
Same number of slots as literal 1
14 +
Add S to T (discard S)
. +
Dot required if stack has just changed. Otherwise carry can propagate only 9 bits
14 +
Add S to T with carry
Requires bit 9 of P be set
200 org arithmetic . . . ;
arithmetic
P9 reset upon return from arithmetic
. +
dup or . +
dup dup - . +
dup or - dup . +
15 and
Bit-wise and of S and T
ff and
20000 and
dup -1 . + and
16 or
Bit-wise exclusive-or of S and T
3ffff or
Not useful since it uses 5 slots instead of 1 ( - )
20000 or
over - and or
over or or
Uses a stack location
17 drop
Discard T
pop drop
push drop pop
Uses a return stack location
Stack Instructions
18 dup
Create a working copy of T
dup or
dup dup or
dup *
dup dup * *
19 pop
Fetch R (destructive)
pop dup push
pop drop ;
pop drop ;
1a over
Fetch S (non-destructive)
over . +
over over
1b a
Fetch A (non-destructive)
a over . +
1c .
Do nothing
b! . b!
. +
for . unext
for . . unext
for . . . unext
1d push
Push T into R
push over
push drop pop
Uses a return stack location
push here 1 + next pop
push max pop min
1e b!
Store into B
Be careful to distinguish b! and !b
1f a!
Store into A
push a! pop a