|
| Term | Example | Explanation |
|---|---|---|
| Number | 300 | ( -- n ) push value onto stack (separate floating point stack in gforth) |
| Arithmetic operation | + - * / mod | ( n1 n2 -- n ) pop appropriate number of operands; push result |
| SWAP | 1 2 .s swap .s | ( n1 n2 -- n2 n1 ) swap top two stack values |
| DROP | ( n -- ) discard top value | |
| 2DROP | ( n1 n2 -- ) discard top two values | |
| DUP | ( n -- n n ) duplicate top value | |
| 2DUP | ( n1 n2 -- n1 n2 n1 n2 ) duplicate top 2 values | |
| ROT | ( n1 n2 n3 -- n2 n3 n1 ) rotate top 3 values | |
| OVER | ( n1 n2 -- n1 n2 n1 ) copy 2nd value to top | |
| PICK | ( wn .. w1 w0 n -- wn .. w1 w0 wn ) copy wn to top of stack | |
| ROLL | ( wn .. w1 w0 n -- wn-1 .. w1 w0 wn ) rotate wn to top of stack | |
| More on arithmetic operations | ||
| MOD | ( n1 n2 -- remainder ) remainder upon dividing n1 by n2 | |
| / | ( n1 n2 -- quotient ) quotient upon dividing n1 by n2 | |
| Term | Example | Explanation |
|---|---|---|
| WORDS | display all the words in the dictionary | |
| : word definition ; | : add-top-4 + + + ; | Forth programmers, like C programmers, would be averse to using such a descriptively long word |
| CONSTANT | 2 constant coin-sides | define a constant using the top of stack value; it is an r-value; its word represents a value |
| VARIABLE | variable x | declare a variable; it is an l-value; its word represents an address |
| ALLOT | 10 allot | ( n -- ) allocate n address units |
| CELLS | 10 cells allot | ( n1 -- n2 ) number of address units in a cell |
| CHARS | create data 10 chars allot | ( n1 -- n2 ) number of address units in n characters |
| , (comma) | create values 1 , 2 , 3 , | ( x -- ) reserve one cell and store x there |
| CREATE | create data 10 chars allot | ( -- ) define a word, which is bound to an address |
| @ | variable x x @ | ( a-addr -- n ) way to get value stored in variable |
| ! | variable x 1 x ! (set x to 1) | ( n a-addr -- ) way to store a value into a variable |
| HERE | ( -- addr ) contents of data space pointer (an address) |
| Term | Example | Explanation |
|---|---|---|
| char+ | ( c-addr1 -- c-addr2 ) address of next character | |
| c@ | ( c-addr -- char ) get character at address | |
| c! | ( char c-addr -- ) put character at address | |
| cell+ | ( a-addr1 -- a-addr2 ) address of next cell | |
| c@ | ( c-addr -- char ) get character at address | |
| @ | ( a-addr -- x ) get value at address | |
| ! | ( x a-addr -- ) put value at address | |
| sp@ | ( -- a-addr ) get value of stack pointer | |
| sp! | ( a-addr -- ) set value of stack pointer | |
| ALIGNED | sp@ 1 aligned + add one to stack pointer |
( addr -- a-addr ) align the address |
| rp@ | ( -- a-addr ) get value of return stack pointer | |
| rp! | ( a-addr -- ) set value of return stack pointer | |
| >r | ( w -- ) R:( -- w) transfer from data to return stack | |
| r> | ( w -- ) R:( -- w) transfer from return to data stack |
| Term | Example | Explanation |
|---|---|---|
| if | : order2 dup > if swap then ; | Note - if is a compile only word, so can only be used in a subroutine definition |
| relational operators, e.g., < , > , < , u< | 0 1 = leaves false, i.e., 0 on top of stack | compare top two values and replace with 0 (false) or true; special cases, e.g., 0= and 0< |
| logical operators, i.e., 0= , and , or | 0 0= leaves non-zero on top of stack | ( x -- flag) operate on top or top two values and replace with 0 (false) or true |
| Term | Example | Explanation |
|---|---|---|
| do loop | : drop-n-items ( w1 .. wn n -- )
0 ?do drop loop ; |
( limit start -- ) Uses two stack parameters - in this example the loop stop value is already on the stack |
| LEAVE | : drop-at-most-n-items ( w1 .. wn n -- )
1 ?do dup 0= if leave then
drop
loop drop ; |
( R: start limit -- ) remove loop control parameters from return stack and leave the innermost loop |
| while repeat | : drop-until-zero ( 0 w1 .. wn -- 0)
begin
dup 0 <>
while drop repeat ; |
( -- ) if code between begin -- while tests non-zero, execute code between while -- repeat |
| Subroutine actions | ||
| EXIT | : test-for-prime ( n -- flag )
dup 1 = if drop 0 exit then
dup 2 = if drop 1 exit then
dup
2 ?do
\ ( n n )
check-done if unloop exit then
other-stuff
loop ; |
( R: return-addr -- ) return from subroutine (be sure to unloop if inside a loop) |
| UNLOOP | See EXIT | |
| Term | Example | Explanation |
|---|---|---|
| key | key | ( -- n ) push ascii code of key press |
| emit | 67 66 65 emit emit emit | ( n -- ) display character with ascii code n |
| accept | ( c-addr +n1 -- +n2 ) get at most n1 characters of input; n2 is actual number read; store at location c-addr; | |
| type | ( c-addr u -- ) display u characters beginning at address c-addr | |
. (dot) |
.s 3 . .s |
pop and display top of stack |
.r (dot-r) |
.s 11 5 .r |
pop field width then pop and display top of stack ( n1 n2 -- ) |
| .S | 1 2 3 .s |
display stack (without altering it) |
| CR | cr ." hello world" |
display a carriage return |
| ." | cr ." hello world" |
display string (compilation only) |
| .( string) | cr .( hello world) |
display string (immediate effect) |
| SPACE | cr space .( hello) space .( world) |
display a space |
| SPACES | cr 10 spaces .( indented text) |
pop value and display value spaces |