Starting Forth Words -*-org-*-
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| 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