forth/starting-words.txt

196 lines
12 KiB
Org Mode
Executable File

Starting Forth Words -*-org-*-
<pre>
Forth words introduced by chapter in "Starting Forth"[1].
* 1. Fundamental Forth
|-------------+--------------+----------------------------------------|
| : xxx yyy ; | -- | Create word xxx with definition yyy |
| CR | -- | Carriage return on terminal |
| SPACES | n -- | Print n spaces on terminal |
| SPACE | -- | Print one space on terminal |
| EMIT | c -- | Print character ASCII c on terminal |
| ." xxx" | -- | Print character string xxx on terminal |
| + | n1 n2 -- sum | Addition |
| . | n -- | Pop n from stack and print |
|-------------+--------------+----------------------------------------|
* 2. How to Get Results
|-------+----------------------+----------------------------------|
| + | n1 n2 -- sum | Adds |
| - | n1 n2 -- diff | Subtracts n1 - n2 |
| * | n1 n2 -- prod | Multiplies |
| / | n1 n2 -- quot | Divides n1 / n2 (int. quotient) |
| /MOD | n1 n2 -- rem quot | n2 / n2 remainder & quotient |
| MOD | n1 n2 -- rem | n1 / n2 remainder |
| SWAP | n1 n2 -- n2 n1 | Reverse top two stack items |
| DUP | n -- n n | Duplicate top stack item |
| OVER | n1 n2 -- n1 n2 n1 | Copy second stack item to top |
| ROT | n1 n2 n3 -- n2 n3 n1 | Rotate third stack item |
| DROP | n -- | Discard top stack item |
| .S | -- | Stack print (non-destructive) |
| 2SWAP | d1 d2 -- d2 d1 | Reverse top two pairs of numbers |
| 2DUP | d -- d d | Duplicate top pair of numbers |
| 2OVER | d1 d2 -- d1 d2 d1 | Copy second pair to top |
| 2DROP | d -- | Discard top pair |
|-------+----------------------+----------------------------------|
* 3. The Editor (and Staff)
|---------------+-----------+----------------------------------------|
| USE xxx | -- | Use file xxx as Forth "disk" |
| LIST | n -- | List disk block n |
| LOAD | n -- | Compile disk block n |
| ( xxx) | -- | Comment |
| UPDATE | -- | Mark current block modified |
| EMPTY-BUFFERS | -- | Mark all blocks unmodified |
| BLOCK | u -- addr | Swap-in block u from mass storage |
| INCLUDE xxx | -- | Compile text file xxx |
| FORGET xxx | -- | Remove definitions from xxx onward |
| MARKER xxx | -- | Set dictionary restore point |
| | | (Executing xxx will remove later defs. |
|---------------+-----------+----------------------------------------|
* 4. Decisions, Decisions ...
|----------------------+----------+-----------------------------------------|
| IF xxx THEN | IF: f -- | Execute xxx if f true (non-zero) |
| IF xxx ELSE yyy THEN | IF: f -- | Execute xxx if f true, yyy if false (0) |
|----------------------+----------+-----------------------------------------|
|-------------+---------------+-----------------------------------|
| = | n1 n2 -- f | Test n1 = n2 |
| - | n1 n2 -- diff | (Equiv. to test n1 != n2) |
| < | n1 n2 -- f | Test n1 < n2 |
| > | n1 n2 -- f | Test n1 > n2 |
| 0= | n -- f | Test n = 0 |
| 0< | n -- f | Test n < 0 |
| 0> | n --f | Test n > 0 |
| AND | n1 n2 -- and | Logical and |
| OR | n1 n2 -- or | Logical or |
| ?DUP | 0 -- 0 | Duplicate if n non-zero |
| | n -- n n | |
| ABORT" xxx" | f -- | If f true, abort with message xxx |
| ?STACK | -- f | True if stack underflow |
| INVERT | f -- f | Logical not |
|-------------+---------------+-----------------------------------|
* 5. The Philosophy of Fixed Point
|--------+----------------------+-------------------------------------------|
| 1+ | n -- n+1 | Add one |
| 1- | n -- n11 | Subtract one |
| 2+ | n -- n+2 | Add two |
| 2- | n -- n-2 | Subtract two |
| 2* | n -- n*2 | Mult. by two/Bit shift left |
| 2/ | n -- n/1 | Div. by two/Bit shift right |
| ABS | n -- n-abs | Absolute value |
| NEGATE | n -- -n | Reverse sign |
| MIN | n1 n2 -- min | Minimum |
| MAX | n1 n2 -- max | Maximum |
| >R | n -- | Pop to return stack |
| R> | -- n | Push from return stack |
| I | -- n | Push copy of return stack top |
| R@ | -- n | Push copy of return stack top |
| J | n -- n+1 | Push copy of return stack 3rd item |
| */ | n1 n2 n3 -- quot | n1*n2/n3 (intermed. result double-length) |
| */MOD | n1 n2 n3 -- rem quot | n1*n2/n3 remainder, quotient |
|--------+----------------------+-------------------------------------------|
* 6. Throw It For a Loop
|----------------------------+--------------------+--------------------------------|
| DO xxx LOOP | DO: limit index -- | Execute xxx limit-index times |
| DO xxx +LOOP | DO: limit index -- | Execute xxx incrementing by n |
| | +LOOP: n -- | from index to limit |
| BEGIN xxx UNTIL | UNTIL: f -- | Repeat xxx until f true |
| BEGIN xxx WHILE yyy REPEAT | WHILE: f -- | Repeat xxx, then yyy if f true |
|----------------------------+--------------------+--------------------------------|
|-------+------------+----------------------------------------------------------|
| LEAVE | -- | Terminate loop immediately |
| U.R | u width -- | Print u right-justified in field width |
| PAGE | -- | Clear terminal and move cursor to upper left-hand corner |
| QUIT | -- | Terminate task (supress "ok") |
| XX | -- | Clear stacks (undefined word) |
|-------+------------+----------------------------------------------------------|
* 7. A Number of Kinds of Numbers
|-----------+---------------------+------------------------------------------------|
| U. | u -- | Print unsigned number |
| UM* | u1 u2 -- ud | Return product of u1, u2 (single -> double) |
| UM/MOD | ud u1 -- u2 u3 | Divide double by single, return single-length |
| | | quotient, remainder |
| U< | u1 u2 -- f | Return u1<u2 |
| HEX | -- | Hexadecimal mode |
| OCTAL | -- | Octal mode |
| DECIMAL | -- | Decimal mode |
| <# xxx #> | d -- addr u | Format unsigned double to string |
| # | -- | Insert low digit in number format |
| #S | -- | Insert rest of high digits in format |
| c HOLD | -- | Insert ASCII code c in format |
| [CHAR] a | -- c | Return ASCII code for character a |
| SIGN | n -- n | Insert minus sign in format if n<0 |
| D+ | d1 d2 -- d-sum | Add (double-length) |
| D- | d1 d2 -- d-diff | Subtract (double-length) |
| DABS | d -- d-abs | Absolute value (double-length) |
| DNEGATE | d -- -d | Reverse sign (double-length) |
| DMAX | d1 d2 -- d-max | Maximum (double-length) |
| DMIN | d1 d2 -- d-min | Minimum (double-length) |
| D= | d1 d2 -- f | Test d1=d2 (double-length) |
| D0= | d -- f | Test d=0 (double-length) |
| D< | d1 d2 -- f | Test d1<d2 (double-length) |
| DU< | ud1 ud2 -- f | Test ud1<ud2 (double-length unsigned) |
| D.R | d width -- | Print d right-justified in field width |
| M+ | d n -- d-sum | Add d+n (mixed precision) |
| SM/REM | d n1 -- n2 n3 | Divide d/n1 giving symmetric quot. n3, rem. n2 |
| FM/MOD | d n1 -- n2 n3 | Divide d/n1 giving floored quot. n3, rem. n2 |
| M* | n1 n2 -- d-prod | Multiply n1*n2 giving double-length |
| M*/ | d n1 n2 -- d-result | d*n1/n2 giving double-length |
|-----------+---------------------+------------------------------------------------|
* 8. Variables, Constants, and Arrays
|---------------+--------------+------------------------------------------------|
| CONSTANT xxx | n -- | Create constant xxx with value n |
| | xxx: -- n | xxx returns n |
| VARIABLE xxx | -- | Create variable xxx |
| | xxx: -- addr | xxx returns var. address |
| CREATE xxx | -- | Create dictionary entry xxx |
| | xxx: -- addr | xxx returns entry address |
| ! | n addr -- | Store n at addr |
| @ | addr -- n | Return contents of addr |
| ? | addr -- | Print contents of addr |
| +! | n addr -- | Add n to contents of addr |
| ALLOT | n -- | Extend prev. dict. entry by n bytes |
| CELL | -- n | Returns number of bytes per cell |
| CELLS | n -- bytes | Returns bytes in n cells |
| , | n -- | Compile n into next cell in dict. |
| C! | b addr -- | Store 8-bit value b in addr |
| C@ | addr -- b | Return 8-bit val. b from addr |
| FILL | addr n b -- | Fill n bytes of mem. with b starting from addr |
| BASE | -- addr | Variable containing current numeric base |
| 2CONSTANT xxx | d -- | Create double-length constant xxx |
| | xxx: -- d | |
| 2VARIABLE xxx | -- | Create double-length variable xxx |
| | xxx: -- addr | |
| 2! | d addr -- | Store double-length at addr |
| 2@ | addr -- d | Fetch double-length fro addr |
| C, | b -- | Compile b into next byte in dict. |
| DUMP | addr u -- | Display u bytes of mem. starting from addr |
| ERASE | addr n -- | Fill n bytes of mem. with 0 starting from addr |
|---------------+--------------+------------------------------------------------|
* Notes
[1] Leo Brodie. Starting Forth. Forth, Inc. 2003.
http://www.forth.com/starting-forth/index.html
accessed Nov. 1, 2011.
</pre>