Forth operates on a data stack. Numbers push values; words such as +, DUP and EXECUTE consume or produce them. Order is part of the program.
Forth operates on a data stack. Numbers push values; words such as +, DUP and EXECUTE consume or produce them. Order is part of the program. Open full-size diagram

Forth: examples explained

Maturity and boundaries: experimental

Implemented scope: Stack operations, integer arithmetic, words, loops, byte I/O, strings and numeric PB embedding are shown.

Boundary: This is not full Forth 2012; BASE/HEX and other words are absent, and stack misuse is not fully guarded.

Back to the PBXB64 V9 overview Deutsch

Forth supported words and scope

Browse the language-specific supported-command and scope reference, with searchable categories and explicit boundaries.

Open scope reference

Forth operates on a data stack. Numbers push values; words such as +, DUP and EXECUTE consume or produce them. Order is part of the program.

Twelve short examples and one larger program. Choose a topic or work through the examples in order.

1. 1 + 2 + 3 + 4 + 5 = 15

The numbers 1 through 5 are added to 15. The program prints 15 and ends with exit code 0.

5 4 + 3 + 2 + 1 +
.
\ 15

Expected exit code: 0; output verification pattern (regex): '15'

PBXB64.exe sum15-forth.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Executed natively with the V22 candidate from 2026-09-30; final-compiler verification pending.

2. Rearrange the stack: DUP, SWAP, OVER, ROT

DUP copies the top value; SWAP exchanges the top two; OVER copies the second; ROT rotates the top three. . pops and prints the top. 0 BYE ends with exit code 0.

1 2 DUP . . . CR
1 2 3 SWAP . . . CR
1 2 3 OVER . . . . CR
1 2 3 ROT . . . CR
1 2 2DUP . . . . CR
1 2 2DROP 99 . CR
0 BYE

Expected exit code: 0

PBXB64.exe test_forth_stack.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Canonical test case with an expected result; no new native V22 check here. Final execution pending.

3. Invoke a user word through EXECUTE

The definition : add+ + ; creates a word. 5 and 6 are pushed; ' add+ produces its execution token. EXECUTE invokes the word; . prints the sum 11.

: add+ + ;
5 6 ' add+ EXECUTE . CR
0 BYE

Expected exit code: 0

PBXB64.exe test_forth_execute_user.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Canonical test case with an expected result; no new native V22 check here. Final execution pending.

4. The maximum of two stack values

Step-by-step flow

  1. 2dup duplicates the top two values while retaining the originals.
  2. > compares the second stack value with the top; IF then removes the smaller original.
  3. 3 7 max2 leaves 7 on the stack. BYE uses that value as the process exit code.
: max2 2dup > if drop else swap drop then ;
3 7 max2
BYE

Expected exit code: 7

PBXB64.exe forth-test_forth_if.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.

5. A loop with step two

Step-by-step flow

  1. 0 initializes the sum; 10 0 DO supplies limit and start index.
  2. I yields 0, 2, 4, 6 and 8 because 2 +LOOP sets the step.
  3. Their sum is 20. . prints it; 0 BYE exits with code 0.
: step-sum
  0 10 0 DO I + 2 +LOOP .
;
step-sum
0 BYE

Expected exit code: 0

Canonical output verification pattern (regex)
20
PBXB64.exe forth-test_forth_plus_loop.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.

6. BEGIN/UNTIL tests after the body

Step-by-step flow

  1. DUP . prints the current counter while retaining its remaining value.
  2. 1+ increments it; DUP 5 > then checks the exit condition.
  3. Displayed values are 0 through 5. Value 6 satisfies the exit check and is not printed.
: count5
  0 BEGIN DUP . 1+ DUP 5 > UNTIL
;
count5
0 BYE

Expected exit code: 0

Canonical output verification pattern (regex)
012345
PBXB64.exe forth-test_forth_begin_until.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.

7. ?DO can skip an empty loop

Step-by-step flow

  1. 5 0 ?DO runs indices 0 through 4.
  2. 5 5 ?DO begins at the limit and never executes its body.
  3. CR emits a newline in both cases; the second case prints only an empty line.
: test1 5 0 ?DO I . LOOP CR ;
: test2 5 5 ?DO I . LOOP CR ;
test1
test2
0 BYE

Expected exit code: 0

Canonical output verification pattern (regex)
01234

PBXB64.exe forth-test_forth_question_do.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.

8. Compute a sum recursively

Step-by-step flow

  1. DUP 0> checks whether the input is positive.
  2. RECURSE computes sumto for n - 1; + adds the returned value to n.
  3. At 0, DROP removes the input and supplies 0. Outputs are 15 for 5 and 0 for 0.
: sumto
  DUP 0> IF DUP 1- RECURSE + ELSE DROP 0 THEN
;
5 sumto . CR
0 sumto . CR
0 BYE

Expected exit code: 0

Canonical output verification pattern (regex)
15
0
PBXB64.exe forth-test_forth_recursion_sum.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.

9. I and J in nested loops

Step-by-step flow

  1. The outer loop uses 0, 1 and 2; the inner loop uses 0 and 1 each time.
  2. I denotes the inner index and J the outer index.
  3. Six I + J terms are 0, 1, 1, 2, 2 and 3, summing to 9.
: nested-sum
  0 3 0 DO
    2 0 DO
      I J + +
    LOOP
  LOOP
  .
;
nested-sum
0 BYE

Expected exit code: 0

Canonical output verification pattern (regex)
9
PBXB64.exe forth-test_forth_nested_loops.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.

10. Temporarily store values on the return stack

Step-by-step flow

  1. 1 2 3 pushes three data values; >R >R moves 3 and then 2 to the return stack.
  2. R@ reads 2 without removing it. Two R> calls then retrieve 2 and 3.
  3. The original 1 remains on the data stack. Outputs are 2, 2, 3 and 1.
: rs-demo
  1 2 3
  >R >R
  R@ . CR
  R> . CR
  R> . CR
  . CR
;
rs-demo
0 BYE

Expected exit code: 0

Canonical output verification pattern (regex)
2
2
3
1
PBXB64.exe forth-test_forth_return_stack.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.

11. Inspect data-stack depth

Step-by-step flow

  1. 1 2 3 creates three entries.
  2. DEPTH reports the count; . removes only that newly generated count value.
  3. After three DROP operations the stack is empty and the second DEPTH output is 0.
: test-depth
  1 2 3 DEPTH . CR
  DROP DROP DROP
  DEPTH . CR
;
test-depth
0 BYE

Expected exit code: 0

Canonical output verification pattern (regex)
3
0
PBXB64.exe forth-test_forth_stack_introspection.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.

12. Leave a word early with EXIT

Step-by-step flow

  1. DUP supplies a copy of the input for IF.
  2. A nonzero value takes EXIT while retaining the original value.
  3. For input 0, DROP removes the original and pushes 99. The calls print 5 and 99.
: guarded DUP IF EXIT THEN DROP 99 ;
5 guarded . CR
0 guarded . CR
0 BYE

Expected exit code: 0

Canonical output verification pattern (regex)
5
99
PBXB64.exe forth-test_forth_exit.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.

Larger example: Several case selections as separate words

Step-by-step flow

  1. Four words combine CASE, OF, ENDOF and ENDCASE; each receives a selector from the data stack.
  2. Calls exercise matching cases and default paths. For example, cases 1, 2 and 3 produce 10, 20 and 30.
  3. . prints each result and CR separates lines. The full canonical output contract also specifies default paths for this PBXB64 Forth dialect.
: case-value
  CASE
    1 OF 10 ENDOF
    2 OF 20 ENDOF
    3 OF 30 ENDOF
    99
  ENDCASE
;

: case-with-base
  CASE
    10 OF 1 ENDOF
    16 OF 2 ENDOF
    0
  ENDCASE
;

: case-default-only
  CASE
    1 OF 100 ENDOF
    42
  ENDCASE
;

: case-multi-of
  CASE
    0 OF 1000 ENDOF
    1 OF 1001 ENDOF
    2 OF 1002 ENDOF
    3 OF 1003 ENDOF
    -1
  ENDCASE
;

1 case-value . CR
2 case-value . CR
3 case-value . CR
4 case-value . CR
10 case-with-base . CR
16 case-with-base . CR
7 case-with-base . CR
0 case-default-only . CR
1 case-default-only . CR
0 case-multi-of . CR
1 case-multi-of . CR
2 case-multi-of . CR
3 case-multi-of . CR
4 case-multi-of . CR
0 BYE

Expected exit code: 0

Canonical output verification pattern (regex)
10
20
30
99
1
2
0
42
100
1000
1001
1002
1003
-1
PBXB64.exe forth-test_forth_case.forth -O2 -o example.exe
.\example.exe
$LASTEXITCODE

Download source

Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.

All sources for this tutorial

Each example has its own folder; the README lists compiler commands. Included support files retain matching paths.

Download all 13 examples as ZIP

Bridging

A Forth word's stack convention is not automatically a C or BASIC call contract. Shared native modules require explicitly matching symbol and type contracts.

Explore complete source pairs