Skip to content

Instantly share code, notes, and snippets.

@BrendanLeber
Forked from RickCarlino/gforth_cheat_sheet.md
Created August 3, 2020 16:04
Show Gist options
  • Save BrendanLeber/075033a1d2383d79e2feaff58f8e3e0b to your computer and use it in GitHub Desktop.
Save BrendanLeber/075033a1d2383d79e2feaff58f8e3e0b to your computer and use it in GitHub Desktop.

Revisions

  1. @RickCarlino RickCarlino revised this gist May 8, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -134,4 +134,7 @@ Write floats with scientific notation.
    `1.23e` -> 1.23
    `1e0` -> 1.0

    Most Forth implementations create a seperate stack for FP operations. This includes Gforth. Usually, you can just prefix data stack operators with an 'f' to operate in the float stack. Eg: `f.` to pop off the fp stack. Other operators include `f+` `f-` `f*` `f/` `f**` `f@` and `f!`.
    Most Forth implementations create a seperate stack for FP operations. This includes Gforth. Usually, you can just prefix data stack operators with an 'f' to operate in the float stack. Eg: `f.` to pop off the fp stack. Other operators include `f+` `f-` `f*` `f/` `f**` `f@` and `f!`.

    # Working with Files

  2. @RickCarlino RickCarlino revised this gist May 8, 2014. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -127,5 +127,11 @@ Use `recurse` to call the word being defined.
    * `v 2 cells` "Grab the contents of 2 cells of memory, starting at the location of variable v"
    * `create v2 20 cells allot` Allocate 20 cells of memory and store the starting address to variable v2

    (To be continued)
    # Working with Floating Point Numbers

    Write floats with scientific notation.

    `1.23e` -> 1.23
    `1e0` -> 1.0

    Most Forth implementations create a seperate stack for FP operations. This includes Gforth. Usually, you can just prefix data stack operators with an 'f' to operate in the float stack. Eg: `f.` to pop off the fp stack. Other operators include `f+` `f-` `f*` `f/` `f**` `f@` and `f!`.
  3. @RickCarlino RickCarlino revised this gist May 7, 2014. 1 changed file with 7 additions and 10 deletions.
    17 changes: 7 additions & 10 deletions gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -120,15 +120,12 @@ Use `recurse` to call the word being defined.
    * `r@` pushes a copy of the top of the return stack on the data stack

    # Memory and Global Variables
    * `variable v` initialize variable 'v'
    * `v` Push the **address** onto the stack (not the value)
    * `5 v !` Set the **value** of 'v' to 5.
    * `v @` Push the **value** of 'v' to the stack
    * `v 2 cells` "Grab the contents of 2 cells of memory, starting at the location of variable v"
    * `create v2 20 cells allot` Allocate 20 cells of memory and store the starting address to variable v2

    ## Create myVar
    `variable myVar`
    (To be continued)

    ## Store 8 into myVar using '!' (store)
    `8 myVar !`

    ## Retrive value from myVar using '@' (fetch)
    `myVar @`

    ## Inspect the memory that myVar holds
    `myVar 1 cells .s dump`
  4. @RickCarlino RickCarlino revised this gist May 7, 2014. 1 changed file with 16 additions and 2 deletions.
    18 changes: 16 additions & 2 deletions gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -113,8 +113,22 @@ Use `recurse` to call the word being defined.

    # The Return Stack

    Aside from the main stack ('the stack'), there is also a 'return stack'. Typically, the return stack is used to store temporary variables.
    Aside from the main stack ('the stack'), there is also a 'return stack'. Typically, the return stack is used to store temporary variables. **Miscounting items on the return stack usually causes a crash**

    * `>r` Pushes data stack element onto return stack (data -> return)
    * `r>` Move from return stack to data stack (return -> data)
    * `r@` pushes a copy of the top of the return stack on the data stack
    * `r@` pushes a copy of the top of the return stack on the data stack

    # Memory and Global Variables

    ## Create myVar
    `variable myVar`

    ## Store 8 into myVar using '!' (store)
    `8 myVar !`

    ## Retrive value from myVar using '@' (fetch)
    `myVar @`

    ## Inspect the memory that myVar holds
    `myVar 1 cells .s dump`
  5. @RickCarlino RickCarlino revised this gist May 7, 2014. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -113,4 +113,8 @@ Use `recurse` to call the word being defined.

    # The Return Stack

    More to follow.
    Aside from the main stack ('the stack'), there is also a 'return stack'. Typically, the return stack is used to store temporary variables.

    * `>r` Pushes data stack element onto return stack (data -> return)
    * `r>` Move from return stack to data stack (return -> data)
    * `r@` pushes a copy of the top of the return stack on the data stack
  6. @RickCarlino RickCarlino revised this gist May 6, 2014. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -104,4 +104,13 @@ endif;
    dup . 1+
    again ;
    endless
    ```
    ```
    use `leave` to get out of a loop and `exit` to leave the definition.

    # Recursion

    Use `recurse` to call the word being defined.

    # The Return Stack

    More to follow.
  7. @RickCarlino RickCarlino revised this gist Mar 17, 2014. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -90,4 +90,18 @@ Another example:
    endif;
    ```

    #### Stopping at section `3.16` of the gforth tutorial. More to follow.
    # Boolean expressions
    * `true` is often represented by `-1` (a 11111111 byte)
    * `false` is 0
    * In many contexts, non zero values are treated as `true`

    # Loops
    * `begin` does nothing at run-time, `again` jumps back to `begin`.

    ```forth
    : endless ( -- )
    0 begin
    dup . 1+
    again ;
    endless
    ```
  8. @RickCarlino RickCarlino revised this gist Mar 16, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -88,4 +88,6 @@ Another example:
    else
    nip
    endif;
    ```
    ```

    #### Stopping at section `3.16` of the gforth tutorial. More to follow.
  9. @RickCarlino RickCarlino revised this gist Mar 16, 2014. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -48,6 +48,7 @@ By convention the comment after the name of a definition describes the stack eff
    * `see` - 'decompiles' a word to see source.

    # "Types"
    Forth doesn't do any type checking whatsoever. It's not a big deal though, but you should be aware of it.
    * `(none)` - signed integer
    * `u` - unsigned integer
    * `c` - character
    @@ -74,4 +75,17 @@ Conditionals can only be used *within a colon definition*.
    dup 0 < if
    negate
    endif ;
    ```

    **NOTE:** In Forth, `endif` is less commonly used. Typically, the word `then` is used. But that's really confusing for non-forth programmers.

    Another example:

    ```forth
    : min ( n1 n2 -- n )
    2dup < if
    drop
    else
    nip
    endif;
    ```
  10. @RickCarlino RickCarlino revised this gist Mar 16, 2014. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -63,4 +63,15 @@ By convention the comment after the name of a definition describes the stack eff
    ```forth
    : swap { a b -- b a }
    b a ;
    ```

    # Conditionals

    Conditionals can only be used *within a colon definition*.

    ```forth
    : abs ( n1 -- +n2 )
    dup 0 < if
    negate
    endif ;
    ```
  11. @RickCarlino RickCarlino revised this gist Mar 16, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -61,4 +61,6 @@ By convention the comment after the name of a definition describes the stack eff
    Curly braces in a word definition allow you to create local variables in the definition.

    ```forth
    : swap { a b -- b a }
    b a ;
    ```
  12. @RickCarlino RickCarlino revised this gist Mar 16, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -58,4 +58,7 @@ By convention the comment after the name of a definition describes the stack eff
    * `f` - floating-point (note that in stack comments 'f' represents flags, and 'r' represents FP numbers).

    # Locals (Local variable definitions)
    ???? See section 3.15 of the gforth tutorial
    Curly braces in a word definition allow you to create local variables in the definition.

    ```forth
    ```
  13. @RickCarlino RickCarlino revised this gist Mar 16, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -49,11 +49,13 @@ By convention the comment after the name of a definition describes the stack eff

    # "Types"
    * `(none)` - signed integer
    * `u - unsigned integer
    * `u` - unsigned integer
    * `c` - character
    * `d` - signed double-cell integer
    * `ud`, `du` - unsigned double-cell integer
    * `2` - two cells (not-necessarily double-cell numbers)
    * `m`, `um` - mixed single-cell and double-cell operations
    * `f` - floating-point (note that in stack comments 'f' represents flags, and 'r' represents FP numbers).

    # Locals (Local variable definitions)
    ???? See section 3.15 of the gforth tutorial
  14. @RickCarlino RickCarlino revised this gist Mar 16, 2014. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -47,4 +47,13 @@ By convention the comment after the name of a definition describes the stack eff
    ```
    * `see` - 'decompiles' a word to see source.


    # "Types"
    * `(none)` - signed integer
    * `u - unsigned integer
    * `c` - character
    * `d` - signed double-cell integer
    * `ud`, `du` - unsigned double-cell integer
    * `2` - two cells (not-necessarily double-cell numbers)
    * `m`, `um` - mixed single-cell and double-cell operations
    * `f` - floating-point (note that in stack comments 'f' represents flags, and 'r' represents FP numbers).

  15. @RickCarlino RickCarlino revised this gist Mar 16, 2014. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -33,9 +33,18 @@ By convention the comment after the name of a definition describes the stack eff
    * `c` - character
    * `f` - Boolean flags, i.e. false or true.
    * `a-addr`,`a-` Cell-aligned address
    * `c-addr`,`c-` - Char-aligned address (note that a Char may have two bytes in Windows NT)
    * `xt` - Execution token, same size as Cell
    * `w,x` - Cell, can contain an integer or an address. It usually takes 32, 64 or 16 bits (depending on your platform and Forth system). A cell is more commonly known as a machine word, but the term word already means something different in Forth.
    * `d` - signed double-cell integer
    * `ud` - unsigned double-cell integer
    * `r` - Float (on the FP stack)

    # (De)Compiling words
    ```forth
    : squared ( n -- n^2 ) \ The parenthesis are just a convention to explain the function.
    dup * ;
    ```
    * `see` - 'decompiles' a word to see source.
    * `see` - 'decompiles' a word to see source.


  16. @RickCarlino RickCarlino revised this gist Mar 16, 2014. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -28,11 +28,11 @@ By convention the comment after the name of a definition describes the stack eff

    ## Conventions for stack effect comments

    `n` - signed integer
    `u` - unsigned integer
    `c` - character
    `f` - Boolean flags, i.e. false or true.
    `a-addr`,`a-` Cell-aligned address
    * `n` - signed integer
    * `u` - unsigned integer
    * `c` - character
    * `f` - Boolean flags, i.e. false or true.
    * `a-addr`,`a-` Cell-aligned address

    ```forth
    : squared ( n -- n^2 ) \ The parenthesis are just a convention to explain the function.
  17. @RickCarlino RickCarlino revised this gist Mar 16, 2014. 1 changed file with 12 additions and 2 deletions.
    14 changes: 12 additions & 2 deletions gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@
    * `drop` and `2drop` - drop a stack item (once / twice)
    * `dup` - duplicate a stack item
    * `rot` - rotate the stack
    * `nip` - Removes the 2nd to last item on the stack
    * `tuck` - duplicates the 2nd to last item on the stack to the front of the stack

    # Working with Files
    * `s" file.fs" included` - Loads file.fs into memory
    @@ -23,9 +25,17 @@ By convention the comment after the name of a definition describes the stack eff

    You should put a correct stack effect on every definition, even if it is just ( -- ). You should also add some descriptive comment to more complicated words (I usually do
    this in the lines following :). If you don't do this, your code becomes unreadable (because you have to work through every definition before you can understand any).

    ## Conventions for stack effect comments

    `n` - signed integer
    `u` - unsigned integer
    `c` - character
    `f` - Boolean flags, i.e. false or true.
    `a-addr`,`a-` Cell-aligned address

    ```forth
    : squared ( n -- n^2 ) \ The parenthesis are just a convention to explain the function.
    dup * ;
    ```
    * `see` - 'decompiles' a word to see source.

    * `see` - 'decompiles' a word to see source.
  18. @RickCarlino RickCarlino revised this gist Mar 16, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,8 @@
    * `(` and `\` - Comments. Comments are significant in Forth, so keep a space between them.

    # Compiling words
    By convention the comment after the name of a definition describes the stack effect: The part in front of the `--' describes the state of the stack before the execution of
    the definition, i.e., the parameters that are passed into the colon definition; the part behind the `--' is the state of the stack after the execution of the definition,
    By convention the comment after the name of a definition describes the stack effect: The part in front of the '--' describes the state of the stack before the execution of
    the definition, i.e., the parameters that are passed into the colon definition; the part behind the '--' is the state of the stack after the execution of the definition,
    i.e., the results of the definition. The stack comment only shows the top stack items that the definition accesses and/or changes.

    You should put a correct stack effect on every definition, even if it is just ( -- ). You should also add some descriptive comment to more complicated words (I usually do
  19. @RickCarlino RickCarlino revised this gist Mar 16, 2014. 1 changed file with 31 additions and 2 deletions.
    33 changes: 31 additions & 2 deletions gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,31 @@
    `.s` - Show the stack
    `+`, `-`, `*`, `mod` - Math operators
    # Math

    * `.s` - Show the stack
    * `+`, `-`, `*`, `mod` - Math operators
    * `/mod` - performs both / and mod

    # Stack manipulation
    * `drop` and `2drop` - drop a stack item (once / twice)
    * `dup` - duplicate a stack item
    * `rot` - rotate the stack

    # Working with Files
    * `s" file.fs" included` - Loads file.fs into memory
    * `gforth code.fs tests.fs -e bye` - Load a file and exit if something goes wrong (instead of the command line)

    # Comments
    * `(` and `\` - Comments. Comments are significant in Forth, so keep a space between them.

    # Compiling words
    By convention the comment after the name of a definition describes the stack effect: The part in front of the `--' describes the state of the stack before the execution of
    the definition, i.e., the parameters that are passed into the colon definition; the part behind the `--' is the state of the stack after the execution of the definition,
    i.e., the results of the definition. The stack comment only shows the top stack items that the definition accesses and/or changes.

    You should put a correct stack effect on every definition, even if it is just ( -- ). You should also add some descriptive comment to more complicated words (I usually do
    this in the lines following :). If you don't do this, your code becomes unreadable (because you have to work through every definition before you can understand any).
    ```forth
    : squared ( n -- n^2 ) \ The parenthesis are just a convention to explain the function.
    dup * ;
    ```
    * `see` - 'decompiles' a word to see source.

  20. @RickCarlino RickCarlino created this gist Mar 16, 2014.
    2 changes: 2 additions & 0 deletions gforth_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    `.s` - Show the stack
    `+`, `-`, `*`, `mod` - Math operators