Sunteți pe pagina 1din 2

FORTH FOR THE COMPLETE IDIOT

FORTH-79 HANDY REFERENCE


on right. See operand key at bottom.
Stack Inputs and outputs are shown; top of stack

STACK MANIPULATION
DUP n—nn) Duplicate top of stack.
n—) Discard tap of stack.
DROP Exchange top two stack items.
SWAP nln2—n2nl)
nln2—nln2nl) Make copy of second Item on top.
OVER
ROT nln2n3—n2n3nl) Rotate third item to top. “rote” 1 OVER)
ni—n2) Copy ni-tb item to top. (Thus PICK DUP .2 PICK
PICK
ROLL n—) Rotate n-th Item to top. (Thus 2 ROLL SWAP .3 ROLL ROT)
?DUP n—n(n)) Duplicate only It non-zero. “query-dup caution). “to-f”
>R n—) Move top item to ‘return stack” for temporary storage (use
Retrieve item from return stack. “r-from
( -.n)
—n) Copy top of return stack onto stack. “r-fetch
DEPTH — n) Count number of items on stack.

COMPARISON
(nin2—flag) True If ni less than n2. less-then
— (nln2—fiag) True If top two numbers are equal equals
> (nln2—flag) True if ni greater than n2. “greater-ttien
0< (n—flag) True if top number negative. “zero-less” s”
(n—flag) True if top number zero. (Equivalent to NOT) “Zero-eQUal
0> (n—flag) True if top number greater then zero. ‘zero-greater
(dl—fIag) True If dl less than . d-less-than”
0< Compare top Iwo Items as unsigned Integers. “u-less
-than”
(unlun2—flag)
NOT (flag—-iflag) Reverse truth value. (Equivalent to 0)

ARITHMETIC AND LOGICAL


+ (nin2—-euni) Add. “plus”
0+ (dld2-.sum) Add double-precision numbers. d-plu$
— (nln2—dlff) Subtract (ni —n2). ‘minus”
1+ (n—n+1) Add 1 to top number. one-plus”
1— (n—n—) Subtract 1 from top number. one-mInuW
2+ (n—n+2) Add 2 to top number. ‘two-plus”
2— (n—n—2) Subtract 2 from top number. “two-minus”
• (nlri2—prod) Multiply. “times”
I (nln2—quot) Divide (nh/n2). (Quotient rounded toward zero) ‘dMde”
MOO (nln2—rern) Modulo (La., remainder from dMSIOn nl/n2). Remainder has same sign as ft. “mod”
IMOD (nin2—,emquot) Divide, giving remainder and quotient “divide-mod”
‘/MOD (nln2nS—remquot) Multiply, then divide (nI’n2/n3), with double-precision intermediate. “times-divide-mod”
“I (nln2n3—quot) Like /MOD • but give quotient only, rounded toward zero. “times-divide”
(unlun2-ud) Multiply unsigned numbers, leaving unsigned double-precision result “u-times”
WMOD (udun—uremuquot) Divide double number by single, giving remainder and quotient all unsigned. “u-divide-mod”
MAX (nln2—max) Leave greater of two numbers. ‘max”
MIN (nln2—min) Leave lesser of two numbers. “min”
ABS (n—mi) Absolute value, “absolute”
NEGATE (n— —n) Leave two’s complement
DNEGATE (d——d) Leave twos compliment of double-precision number. “d-negate”
AND (nin2—and) Bltwlse logical AND.
OR (nln2—oq) Bitwise logical OR
XOR (nln2—xor) Bltwlse logical exclusive-OR. “x-or’

MEMORY
( addr — n) Replace address by number at address. “IetcW
(naddr— ) Storenataddr.”store
( addr — byte) Fetch least significant byte only. “c-tetcW
CI ( n addr —) Store least signflcant byte only. “c-store”
( addr — ) Display number at address. “puestlon-maIIc
+1 ( n addr — ) Add n to number at addr. ‘plus-store” at addf2, if n>0.
MOVE ( addrl sddf2 n — ) Move n numbers starting at addrl to memory starting addi2. It n)O. “c-move”
MOVE ( addrl addr2 n —) Move n bytes starting at addrl to memory starting at n>0,
( addr n byte —) Pill n bytes in memory with byte beginning at ad&, If
FILL
CONTROL STRUCTURES
DO...LOOP do: ( end+1 start —) Set up loop, given index range.
on data stack.
( — index) Place current loop index
( — Index) Return Index of next outer loop in same definItion. limit equal to Index.
.3 Terminate loop at next LOOP or +LOOP by setting
,

LEAVE ( — ) Like DO... LOOP, but adds stack value (instead


of always 1) to Index- Loop terminates when
DO...+LOOP do: ( limit start — ) Index Is less than limit (n<0). “plus-loop”
-Hoop: ( n —) Index Is greater than or equal to limit (n>0), or when
IF,. .(true). - .THEN lt(flag—) lttopofstacktnje,execute.
IF.. . (true) ELSE. . . Ii: ( flag —) Same, but it false, execute ELSE clause.
- (false) . .THEN
Loop back to BEGIN until true at UNTiL. unconditionally to BEGIN. When false, continue after
-

BEGIN - - UNTIL imfl(ftag-)


LOOP while true at WHILE; REPEAT loops
-

BEGIN -. WHILE. while: (flag —


-. REPEAT
REPEAT. be used within DO.., LOOP)
(_) Terminate execution of colon definition. (May noton stack (a.p., address returned by FIND).
(addi’—) Execute dictionary entry at compilation address

addr, addrl,... addresses char 7-bit ascii character value


-w d, di.... 32-bit signed numbers B-bit byte flag boolean flag
u unsigned byte
,. ni, . . - 10-bit signed numbers
FORTI-; FOR THE COMPLETE IDICT

TERMINAL INPUT-OUTPUT
CR Do a carriage return and hr,e teed c-r
EMrr Type ascii value from stack.
SPACE Type one apace.
SPACES n— ) Type n spaces, If n>O.
TYPE adOrn— Type string of n characters begirmirig at addr. If n>0.
COUNT addr — addr+l n) Change address of string (prefixed by length byte at addr) to TYPE form.
—TRAILING addr ni — adOr n2) Reduce character count of string at addr to omit trailing blanks. dash-traiIing’
KEY -char) Read key end leave ascn value on stack.
EXPECT adn-) Read n cfleracters (or until carnage return) from terminal to address, with null(s) at red.
QUERY Read line of up to 60 characters from terminal to input butter.
WORD cbs — addr) Read next word from input stream using char as delimiter, ortmtd mill. Leave addief length byte.

NUMERIC CONVERSION
BASE (-addr) System variable containing radix for numeric conversion.
DECIMAL (—) Set decimal number base.
(n— Print number with one trailing blank and sign if negative. dor
U. (wi-) Print top of stack as unsigned number with one trailing blank. ‘u-dor
CONVERT (dl addrl — addr2 ) Convert String ataddrl +1 todoublenurnber. Add to dl leaving awn 02 and add tnon-digit.
(— ) Start numeric output string conversion. i.ss-sharp
(udi -ud2) Convert next digit of unsigned double number and add character to output string. ‘sherp
.5 (ud —00) Convert all significant digits of unsigned do*.lole number to output string. stiwp-s
HOLD (c-) Add ascii char to output string
SIGN (n— ) Add minus sign to output string If ricO.
(0 — adOrn) top d and terminate numeric output string, leaving addr and count for TYPE. ‘shwp-grester

MASS STORAGE INPUT/OUTPUT


UST (n—) List screen n and set SCR to contain it.
LOAD (it— interpret screen n, then resume interpretation of the current input stream.
SCR ( —addr) System variable containing screen number most recently listed.
BLOCK (n—addx) Leave memory address of block, reading from mass storage If necessary.
UPDATE ( — Mark last block referenced as modified.
BUFFER (n—sddr) add of a free buffer, as ad toblock n witteprevious c 4.nts tomassetoragelt UPDATEd.
SAVE-BUFFERS ( — Write alt UPDATEd blocks to mass storage.
EMPTY-BUFFERS (-) Mark all block buffers as empty, without writing UPDATEd blocks to mass storage.

DEFINING WORDS
:xxx C-’) Begin colon definition of xxx . colon
C—) d colon dflnificn, semcolon
VARIABLE xxx ( — Cleats a two-byte variable named xxx mturns address when executed.
xx( -addr)
CONSTANT xxx (n— Create a constant named xxx with value n returns value when executed.
xxx( — it)
VOCABULARY xxx ( — Create a vocabulary named xxx; bec;omes CONT vocabulary when executed.
CREATE... DOES> does: ( — addr) Used to create a new defining word, with execution-time routine In high-level FOR1K ‘does

VOCABULARIES
CONTEXT (-addr) System variable pointIng to vocabulary where word names we searched for.
CURRENT ( —addr) System variable pointing to vocabulary where new definitions are put
FORTH (—) Mainvocabulary, contained in all other vocabularlee. Execution of FORTh sets contextvocabulaiy.
DEFINITIONS (—) Sets CURRENT vocabulary to CONTEXT.
( —addr) Find address of xxx in dictlonary If used in definition, compile address. tick’
FIND ( —addr) Leave con 3aliri address of next wordinirElut stream. itnolloundin CONTEXT orFORTH, leaveD.
FORGET xxx (—) Forget all definitions back to and including xxx • which must be in CURRENT or FORTH.

COMPILER
(n— ) Compile a number Into the dictionary. ‘cornma
word.
ALLOT (n— ) Add two bytes to the parameter field of the most recently-definedexecuted. “dot-ouote
C—) Print message (terminated by ‘). If used in definition, print when
definition, rather than compiled.
w4MEDIATE (—) Mark lastdeflned word to be executed when encountered ins definition is executed.
UTERAL (n— ) if compiling, save n in dictionary, to be returned to stack when
STATE C-—) System variable whose value Is non-zero when compilation is aceraring.
(—) Stop compiling input text and begin executing. ‘Ieft”bracket
(—) Slop executing input text and begin compiling. “flght-bracker dictionary.
hOUFILE (—) Compile the address of the next non-IMMEDIATE word Into the
Compile the Iottng word, even If IMMEDIATE. braeket-ccaip
lle
(COMPtLE C—)
MISCELLANEOUS after ( . paren, do.e-parn’
(—) Begin comment, terminated by) on same line or screen; apace
Leave address of next available dictionary location.
tERE (-addr)
(-.addr) Leave address of a scratch area of at least 64 bytes.
PAD System variable containing character offset into
input buffec used. e.g., by WORD. “ton
>lN (-sOOt) interpreted, cr0 if tromtwminaL “b-Hi
C-—) S stem variable containing block number currentty being
return control to terminal.
(—) Clear data and return stacks, set execution mode,
ABORT Clear data staci or print any message.
ourr C—) u ABORT , xcept does not
C—) VerIfy that system conforms toFORTH-79.Staridd.
79-STANDARD

S-ar putea să vă placă și