Catalog / Forth Programming Language Cheatsheet
Forth Programming Language Cheatsheet
A concise reference for the Forth programming language, covering its core concepts, syntax, and common words for stack manipulation, arithmetic, control flow, and memory access.
Core Concepts and Stack Manipulation
Fundamental Principles
Stack-Based: Forth is a stack-based language where data is manipulated on a stack. Most operations involve pushing data onto the stack, performing operations on the top elements, and pushing the result back onto the stack. Reverse Polish Notation (RPN): Forth uses RPN, also known as postfix notation, where operators follow their operands (e.g., |
Words: Forth programs are built from ‘words’, which are essentially functions or commands. Words are executed in the order they appear. |
Stack Manipulation Words
|
Duplicates the top item on the stack. Example: |
|
Removes the top item from the stack. Example: |
|
Exchanges the top two items on the stack. Example: |
|
Duplicates the second item on the stack and places it on top. Example: |
|
Rotates the top three items on the stack, bringing the third item to the top. Example: |
|
Duplicates the top two items on the stack. Example: |
Arithmetic and Logical Operations
Arithmetic Words
|
Adds the top two numbers on the stack. Example: |
|
Subtracts the top number from the second number on the stack. Example: |
|
Multiplies the top two numbers on the stack. Example: |
|
Divides the second number on the stack by the top number, returning the quotient. Example: |
|
Divides the second number on the stack by the top number, returning the remainder. Example: |
|
Divides the second number by the top number, returning both the quotient and the remainder (remainder on top). Example: |
Logical and Comparison Words
|
Compares the top two numbers on the stack for equality. Returns Example: |
|
Compares the top two numbers for inequality. Returns Example: |
|
Compares if the second number on the stack is less than the top number. Returns Example: |
|
Compares if the second number on the stack is greater than the top number. Returns Example: |
|
Performs a bitwise AND operation on the top two numbers. Example: |
|
Performs a bitwise OR operation on the top two numbers. Example: |
|
Performs a bitwise NOT operation on the top number. Example: |
Control Flow and Definitions
Control Flow Structures
Example:
|
Example:
|
Example:
|
Example:
|
Defining New Words
Example:
This defines a new word |
Defining constants and variables:
Example:
Example:
|
Memory Access and I/O
Memory Access Words
|
Stores a value at a specified memory address. The address is on top of the stack, followed by the value to store. Example: |
|
Fetches the value from a specified memory address and places it on the stack. The address is on top of the stack. Example: |
|
Adds a value to the content of a specified memory address. The address is on top of the stack, followed by the increment value. Example: |
|
Stores a byte at a specified memory address. Example: |
|
Fetches a byte from a specified memory address. Example: |
Input/Output Words
|
Prints the top number on the stack to the console, followed by a space. Example: |
|
Prints the contents of the data stack without modifying it (stack snapshot). Example: |
|
Prints the character corresponding to the ASCII value on top of the stack. Example: |
|
Prints a string literal to the console. The string is enclosed in double quotes. Example: |
|
Reads a character from the input and places its ASCII value on the stack. Example: |