Forth is a stack-based language created by Charles Moore around 1970. Gforth is the GNU implementation: gforth hello.fs -e bye runs a file and exits, and gforth alone opens an interactive prompt that answers ok after each line.
Forth · Lesson 1 · On the house
Hello, Forth: the data stack and words
Run Forth with gforth, push numbers onto the data stack, print with . and read stack effects with .s. Lesson 1 is free — on the house.
Lessons · Forth · Lesson 1 of 10 · On the house
Hello, Forth: the data stack and words
Run Forth with gforth, push numbers onto the data stack, print with . and read stack effects with .s.

Source is a stream of words separated by whitespace. The interpreter reads one word at a time: if it names a defined word, Forth runs it; otherwise Forth tries to read it as a number and pushes it onto the data stack.
The data stack is last-in, first-out. 1 2 3 pushes three numbers, leaving 3 on top. . (dot) pops the top number and prints it followed by a space.
.s prints the whole stack without changing it, as <depth> bottom ... top. It is the main debugging tool: run it whenever you are unsure what the stack holds.
cr prints a newline. ." text" compiles text to print inside a definition, and .( text) prints immediately at the top level. The space after ." or .( is required because each is itself a word.
Comments: \ comments to the end of the line, and ( ... ) is an inline comment. By convention a stack comment such as ( a b -- sum ) documents what a word takes and leaves.
hello.fs
\ hello.fs
.( Hello, Forth!) cr
1 2 3 .s cr
. . . cr
42 . cr
.s cr
Quiz
Quiz
Quiz
Quiz
Check
Match the still for: Hello, Forth: the data stack and words.
Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.