Tuesday April 18, 2023

Towards A Simple Forth System

The following language is the result of my attempt to create a Forth interpreter. Its current state is a stepping stone between basic postfix arithmetic and a full Forth system. It is a stack based computer architecture.

The language contains 7 symbols, which may also be thought of as opcodes.
1) (ADD) + Addition
2) (MUL) * Multiplication
3) (CMP) > Comparison
4) (BRN) ? Branch
5) (FLP) & Flip
6) (DRP) # Drop
7) (DUP) $ Dup

Their operations may be divided into Arithmetic (+,*), Control Flow (>,?), and Data Manipulation (&,#,$). Examples are given below.

Arithmetic

The -- represents the division between the data stack and the input stack. The symbol directly to the right of the -- defines the current operation. When the symbol is a number it is pushed directly to the left of the -- onto the data stack.

Data Stack -- Input Stack

The examples below show the system loading 2 numbers and carrying out an arithmetic operation.

Addition:
                         -- 3 4 +                      
(NUM)                    -- (3) 4 +                      
(NUM)                  3 -- (4) +                        
(ADD)                3 4 -- (+)                          
                       7 --

Multiplication:
                         -- 4 2 *                      
(NUM)                    -- (4) 2 *                      
(NUM)                  4 -- (2) *                        
(MUL)                4 2 -- (*)                          
                       8 --


Control Flow

Control flow is achieved with comparison and branch instructions. The comparison instruction (>) checks if the second number on the data stack is greater than the top of the data stack. If true 1 is pushed back onto the data stack otherwise 0.
Comparison:
                         -- 3 4 >                      
(NUM)                    -- (3) 4 >                      
(NUM)                  3 -- (4) >                        
(CMP)                3 4 -- (>)                          
                       0 --

The branch operation checks if the top of the data stack is a 0. If so the next word on the input stack is dropped. The two examples below show the operation of both possible cases. In example 0 the 1 is dropped because it is the next symbol on the stack after the ? operation. In example 1 the 1 is not dropped so it ends up on the data stack.
Branch example 0:
                         -- 0 ? 1                      
(NUM)                    -- (0) ? 1                      
(BRN)                  0 -- (?) 1                        
                         --

Branch example 1:
                         -- 1 ? 1                      
(NUM)                    -- (1) ? 1                      
(BRN)                  1 -- (?) 1                        
(NUM)                    -- (1)                          
                       1 --


Data Manipulation

Data on the stack may be manipulated with the operations Flip, Drop, Dup. The Flip operation switches the order of the top two elements on the stack. Drop drops the top element from the stack. Dup duplicates the top element on the stack.
Flip:
                         -- 1 2 &                      
(NUM)                    -- (1) 2 &                      
(NUM)                  1 -- (2) &                        
(FLP)                1 2 -- (&)                          
                     2 1 --

Drop:
                         -- 3 #                        
(NUM)                    -- (3) #                        
(DRP)                  3 -- (#)                          
                         --

Dup:
                         -- 4 $                        
(NUM)                    -- (4) $                        
(DUP)                  4 -- ($)                          
                     4 4 --


Defining Functions

Functions are defined using Forth syntax.
: name opcodes ;
In the example given below the function "a" represents the opcodes ($ -1 + $ ? a). When the symbol "a" is encountered the opcodes ($ -1 + $ ? a) are copied onto the input stack. A function can reference itself recursively by copying its own name onto the input stack. This is an extremely inefficient way of defining recursion but it is the simplest to implement.
: a $ -1 + $ ? a ;


A Simple Loop

The following example shows how a simple loop may be created using the opcodes ($, +, ?) and a recursive function call. The complete operation of the program is shown below. Given some starting number which is pushed to the left of the -- onto the data stack the $ opcode duplicates the input then the + opcode adds -1 to the top of the data stack, finally the top of the stack is duplicated and checked with ? to see if 0, otherwise the insert opcode is called when "a" is encountered to copy the body of the function back onto the input stack.

The way in which a return stack could be used to speed up the execution of this kind of architecture becomes obvious when considering ways to avoid repeatedly copying the same definition onto the input stack. For the initial design a 2 stack architecture is more convenient since it may be represented by a single line on a page. The ability to describe the entire state of the system with a single string which is operated on at a single point -- represents an abstract design of a turing machine with a central write head and two spools of tape, one on the left and one on the right of the Input and Data stacks.
Dictionary:
: a $ -1 + $ ? a ;
                         -- 5 a                        
(NUM)                    -- (5) a                        
(INS)                  5 -- (a)                          
(DUP)                  5 -- ($) -1 + $ ? a               
(NUM)                5 5 -- (-1) + $ ? a                 
(ADD)             5 5 -1 -- (+) $ ? a                    
(DUP)                5 4 -- ($) ? a                      
(BRN)              5 4 4 -- (?) a                        
(INS)                5 4 -- (a)                          
(DUP)                5 4 -- ($) -1 + $ ? a               
(NUM)              5 4 4 -- (-1) + $ ? a                 
(ADD)           5 4 4 -1 -- (+) $ ? a                    
(DUP)              5 4 3 -- ($) ? a                      
(BRN)            5 4 3 3 -- (?) a                        
(INS)              5 4 3 -- (a)                          
(DUP)              5 4 3 -- ($) -1 + $ ? a               
(NUM)            5 4 3 3 -- (-1) + $ ? a                 
(ADD)         5 4 3 3 -1 -- (+) $ ? a                    
(DUP)            5 4 3 2 -- ($) ? a                      
(BRN)          5 4 3 2 2 -- (?) a                        
(INS)            5 4 3 2 -- (a)                          
(DUP)            5 4 3 2 -- ($) -1 + $ ? a               
(NUM)          5 4 3 2 2 -- (-1) + $ ? a                 
(ADD)       5 4 3 2 2 -1 -- (+) $ ? a                    
(DUP)          5 4 3 2 1 -- ($) ? a                      
(BRN)        5 4 3 2 1 1 -- (?) a                        
(INS)          5 4 3 2 1 -- (a)                          
(DUP)          5 4 3 2 1 -- ($) -1 + $ ? a               
(NUM)        5 4 3 2 1 1 -- (-1) + $ ? a                 
(ADD)     5 4 3 2 1 1 -1 -- (+) $ ? a                    
(DUP)        5 4 3 2 1 0 -- ($) ? a                      
(BRN)      5 4 3 2 1 0 0 -- (?) a                        
             5 4 3 2 1 0 --


Factorial

A factorial program in this language can be described by a set of functions. The first function "a" is a modified loop which counts down from the input number and stops at 1. The operation of this part of the program is shown below.
Dictionary:
: a $ -1 + $ 1 > ? a ;
                         -- 5 a                        
(NUM)                    -- (5) a                        
(INS)                  5 -- (a)                          
(DUP)                  5 -- ($) -1 + $ 1 > ? a           
(NUM)                5 5 -- (-1) + $ 1 > ? a             
(ADD)             5 5 -1 -- (+) $ 1 > ? a                
(DUP)                5 4 -- ($) 1 > ? a                  
(NUM)              5 4 4 -- (1) > ? a                    
(CMP)            5 4 4 1 -- (>) ? a                      
(BRN)              5 4 1 -- (?) a                        
(INS)                5 4 -- (a)                          
(DUP)                5 4 -- ($) -1 + $ 1 > ? a           
(NUM)              5 4 4 -- (-1) + $ 1 > ? a             
(ADD)           5 4 4 -1 -- (+) $ 1 > ? a                
(DUP)              5 4 3 -- ($) 1 > ? a                  
(NUM)            5 4 3 3 -- (1) > ? a                    
(CMP)          5 4 3 3 1 -- (>) ? a                      
(BRN)            5 4 3 1 -- (?) a                        
(INS)              5 4 3 -- (a)                          
(DUP)              5 4 3 -- ($) -1 + $ 1 > ? a           
(NUM)            5 4 3 3 -- (-1) + $ 1 > ? a             
(ADD)         5 4 3 3 -1 -- (+) $ 1 > ? a                
(DUP)            5 4 3 2 -- ($) 1 > ? a                  
(NUM)          5 4 3 2 2 -- (1) > ? a                    
(CMP)        5 4 3 2 2 1 -- (>) ? a                      
(BRN)          5 4 3 2 1 -- (?) a                        
(INS)            5 4 3 2 -- (a)                          
(DUP)            5 4 3 2 -- ($) -1 + $ 1 > ? a           
(NUM)          5 4 3 2 2 -- (-1) + $ 1 > ? a             
(ADD)       5 4 3 2 2 -1 -- (+) $ 1 > ? a                
(DUP)          5 4 3 2 1 -- ($) 1 > ? a                  
(NUM)        5 4 3 2 1 1 -- (1) > ? a                    
(CMP)      5 4 3 2 1 1 1 -- (>) ? a                      
(BRN)        5 4 3 2 1 0 -- (?) a                        
               5 4 3 2 1 --
It becomes obvious at this point that all the elements of the factorial are on the stack, before multiplying them together however a new function "b" is defined to add 0 to the stack and place it under the input number with &.
Dictionary:
: a $ -1 + $ 1 > ? a ;
: b 0 & a ;
                         -- 5 b                        
(NUM)                    -- (5) b                        
(INS)                  5 -- (b)                          
(NUM)                  5 -- (0) & a                      
(FLP)                5 0 -- (&) a                        
(INS)                0 5 -- (a)                          
(DUP)                0 5 -- ($) -1 + $ 1 > ? a           
(NUM)              0 5 5 -- (-1) + $ 1 > ? a             
(ADD)           0 5 5 -1 -- (+) $ 1 > ? a                
(DUP)              0 5 4 -- ($) 1 > ? a                  
(NUM)            0 5 4 4 -- (1) > ? a                    
(CMP)          0 5 4 4 1 -- (>) ? a                      
(BRN)            0 5 4 1 -- (?) a                        
(INS)              0 5 4 -- (a)                          
(DUP)              0 5 4 -- ($) -1 + $ 1 > ? a           
(NUM)            0 5 4 4 -- (-1) + $ 1 > ? a             
(ADD)         0 5 4 4 -1 -- (+) $ 1 > ? a                
(DUP)            0 5 4 3 -- ($) 1 > ? a                  
(NUM)          0 5 4 3 3 -- (1) > ? a                    
(CMP)        0 5 4 3 3 1 -- (>) ? a                      
(BRN)          0 5 4 3 1 -- (?) a                        
(INS)            0 5 4 3 -- (a)                          
(DUP)            0 5 4 3 -- ($) -1 + $ 1 > ? a           
(NUM)          0 5 4 3 3 -- (-1) + $ 1 > ? a             
(ADD)       0 5 4 3 3 -1 -- (+) $ 1 > ? a                
(DUP)          0 5 4 3 2 -- ($) 1 > ? a                  
(NUM)        0 5 4 3 2 2 -- (1) > ? a                    
(CMP)      0 5 4 3 2 2 1 -- (>) ? a                      
(BRN)        0 5 4 3 2 1 -- (?) a                        
(INS)          0 5 4 3 2 -- (a)                          
(DUP)          0 5 4 3 2 -- ($) -1 + $ 1 > ? a           
(NUM)        0 5 4 3 2 2 -- (-1) + $ 1 > ? a             
(ADD)     0 5 4 3 2 2 -1 -- (+) $ 1 > ? a                
(DUP)        0 5 4 3 2 1 -- ($) 1 > ? a                  
(NUM)      0 5 4 3 2 1 1 -- (1) > ? a                    
(CMP)    0 5 4 3 2 1 1 1 -- (>) ? a                      
(BRN)      0 5 4 3 2 1 0 -- (?) a                        
             0 5 4 3 2 1 --
Now all that is remaining is to multiply together numbers while checking to see if the zero is encountered. A function "c" is added to the end of b which flips the top two numbers on the stack and checks if 0 is encountered with ?. If so the program is done, otherwise "d" is called which is simply * and a return to c. So all the elements are multiplied and only an extra 0 is left on the stack.
Dictionary:
: a $ -1 + $ 1 > ? a ;
: b 0 & a c ;
: c & $ ? d ;
: d * c ;
                         -- 5 b                        
(NUM)                    -- (5) b                        
(INS)                  5 -- (b)                          
(NUM)                  5 -- (0) & a c                    
(FLP)                5 0 -- (&) a c                      
(INS)                0 5 -- (a) c                        
(DUP)                0 5 -- ($) -1 + $ 1 > ? a c         
(NUM)              0 5 5 -- (-1) + $ 1 > ? a c           
(ADD)           0 5 5 -1 -- (+) $ 1 > ? a c              
(DUP)              0 5 4 -- ($) 1 > ? a c                
(NUM)            0 5 4 4 -- (1) > ? a c                  
(CMP)          0 5 4 4 1 -- (>) ? a c                    
(BRN)            0 5 4 1 -- (?) a c                      
(INS)              0 5 4 -- (a) c                        
(DUP)              0 5 4 -- ($) -1 + $ 1 > ? a c         
(NUM)            0 5 4 4 -- (-1) + $ 1 > ? a c           
(ADD)         0 5 4 4 -1 -- (+) $ 1 > ? a c              
(DUP)            0 5 4 3 -- ($) 1 > ? a c                
(NUM)          0 5 4 3 3 -- (1) > ? a c                  
(CMP)        0 5 4 3 3 1 -- (>) ? a c                    
(BRN)          0 5 4 3 1 -- (?) a c                      
(INS)            0 5 4 3 -- (a) c                        
(DUP)            0 5 4 3 -- ($) -1 + $ 1 > ? a c         
(NUM)          0 5 4 3 3 -- (-1) + $ 1 > ? a c           
(ADD)       0 5 4 3 3 -1 -- (+) $ 1 > ? a c              
(DUP)          0 5 4 3 2 -- ($) 1 > ? a c                
(NUM)        0 5 4 3 2 2 -- (1) > ? a c                  
(CMP)      0 5 4 3 2 2 1 -- (>) ? a c                    
(BRN)        0 5 4 3 2 1 -- (?) a c                      
(INS)          0 5 4 3 2 -- (a) c                        
(DUP)          0 5 4 3 2 -- ($) -1 + $ 1 > ? a c         
(NUM)        0 5 4 3 2 2 -- (-1) + $ 1 > ? a c           
(ADD)     0 5 4 3 2 2 -1 -- (+) $ 1 > ? a c              
(DUP)        0 5 4 3 2 1 -- ($) 1 > ? a c                
(NUM)      0 5 4 3 2 1 1 -- (1) > ? a c                  
(CMP)    0 5 4 3 2 1 1 1 -- (>) ? a c                    
(BRN)      0 5 4 3 2 1 0 -- (?) a c                      
(INS)        0 5 4 3 2 1 -- (c)                          
(FLP)        0 5 4 3 2 1 -- (&) $ ? d                    
(DUP)        0 5 4 3 1 2 -- ($) ? d                      
(BRN)      0 5 4 3 1 2 2 -- (?) d                        
(INS)        0 5 4 3 1 2 -- (d)                          
(MUL)        0 5 4 3 1 2 -- (*) c                        
(INS)          0 5 4 3 2 -- (c)                          
(FLP)          0 5 4 3 2 -- (&) $ ? d                    
(DUP)          0 5 4 2 3 -- ($) ? d                      
(BRN)        0 5 4 2 3 3 -- (?) d                        
(INS)          0 5 4 2 3 -- (d)                          
(MUL)          0 5 4 2 3 -- (*) c                        
(INS)            0 5 4 6 -- (c)                          
(FLP)            0 5 4 6 -- (&) $ ? d                    
(DUP)            0 5 6 4 -- ($) ? d                      
(BRN)          0 5 6 4 4 -- (?) d                        
(INS)            0 5 6 4 -- (d)                          
(MUL)            0 5 6 4 -- (*) c                        
(INS)             0 5 24 -- (c)                          
(FLP)             0 5 24 -- (&) $ ? d                    
(DUP)             0 24 5 -- ($) ? d                      
(BRN)           0 24 5 5 -- (?) d                        
(INS)             0 24 5 -- (d)                          
(MUL)             0 24 5 -- (*) c                        
(INS)              0 120 -- (c)                          
(FLP)              0 120 -- (&) $ ? d                    
(DUP)              120 0 -- ($) ? d                      
(BRN)            120 0 0 -- (?) d                        
                   120 0 --
The last thing to do is to write a function to remove the 0 from the stack. This function simply calls "b" then the # opcode which drops the last value on the stack. In this case 0. The final factorial program is given below.
Dictionary:
: a $ -1 + $ 1 > ? a ;
: b 0 & a c ;
: c & $ ? d ;
: d * c ;
: e b # ;
                         -- 5 e                        
(NUM)                    -- (5) e                        
(INS)                  5 -- (e)                          
(INS)                  5 -- (b) #                        
(NUM)                  5 -- (0) & a c #                  
(FLP)                5 0 -- (&) a c #                    
(INS)                0 5 -- (a) c #                      
(DUP)                0 5 -- ($) -1 + $ 1 > ? a c #       
(NUM)              0 5 5 -- (-1) + $ 1 > ? a c #         
(ADD)           0 5 5 -1 -- (+) $ 1 > ? a c #            
(DUP)              0 5 4 -- ($) 1 > ? a c #              
(NUM)            0 5 4 4 -- (1) > ? a c #                
(CMP)          0 5 4 4 1 -- (>) ? a c #                  
(BRN)            0 5 4 1 -- (?) a c #                    
(INS)              0 5 4 -- (a) c #                      
(DUP)              0 5 4 -- ($) -1 + $ 1 > ? a c #       
(NUM)            0 5 4 4 -- (-1) + $ 1 > ? a c #         
(ADD)         0 5 4 4 -1 -- (+) $ 1 > ? a c #            
(DUP)            0 5 4 3 -- ($) 1 > ? a c #              
(NUM)          0 5 4 3 3 -- (1) > ? a c #                
(CMP)        0 5 4 3 3 1 -- (>) ? a c #                  
(BRN)          0 5 4 3 1 -- (?) a c #                    
(INS)            0 5 4 3 -- (a) c #                      
(DUP)            0 5 4 3 -- ($) -1 + $ 1 > ? a c #       
(NUM)          0 5 4 3 3 -- (-1) + $ 1 > ? a c #         
(ADD)       0 5 4 3 3 -1 -- (+) $ 1 > ? a c #            
(DUP)          0 5 4 3 2 -- ($) 1 > ? a c #              
(NUM)        0 5 4 3 2 2 -- (1) > ? a c #                
(CMP)      0 5 4 3 2 2 1 -- (>) ? a c #                  
(BRN)        0 5 4 3 2 1 -- (?) a c #                    
(INS)          0 5 4 3 2 -- (a) c #                      
(DUP)          0 5 4 3 2 -- ($) -1 + $ 1 > ? a c #       
(NUM)        0 5 4 3 2 2 -- (-1) + $ 1 > ? a c #         
(ADD)     0 5 4 3 2 2 -1 -- (+) $ 1 > ? a c #            
(DUP)        0 5 4 3 2 1 -- ($) 1 > ? a c #              
(NUM)      0 5 4 3 2 1 1 -- (1) > ? a c #                
(CMP)    0 5 4 3 2 1 1 1 -- (>) ? a c #                  
(BRN)      0 5 4 3 2 1 0 -- (?) a c #                    
(INS)        0 5 4 3 2 1 -- (c) #                        
(FLP)        0 5 4 3 2 1 -- (&) $ ? d #                  
(DUP)        0 5 4 3 1 2 -- ($) ? d #                    
(BRN)      0 5 4 3 1 2 2 -- (?) d #                      
(INS)        0 5 4 3 1 2 -- (d) #                        
(MUL)        0 5 4 3 1 2 -- (*) c #                      
(INS)          0 5 4 3 2 -- (c) #                        
(FLP)          0 5 4 3 2 -- (&) $ ? d #                  
(DUP)          0 5 4 2 3 -- ($) ? d #                    
(BRN)        0 5 4 2 3 3 -- (?) d #                      
(INS)          0 5 4 2 3 -- (d) #                        
(MUL)          0 5 4 2 3 -- (*) c #                      
(INS)            0 5 4 6 -- (c) #                        
(FLP)            0 5 4 6 -- (&) $ ? d #                  
(DUP)            0 5 6 4 -- ($) ? d #                    
(BRN)          0 5 6 4 4 -- (?) d #                      
(INS)            0 5 6 4 -- (d) #                        
(MUL)            0 5 6 4 -- (*) c #                      
(INS)             0 5 24 -- (c) #                        
(FLP)             0 5 24 -- (&) $ ? d #                  
(DUP)             0 24 5 -- ($) ? d #                    
(BRN)           0 24 5 5 -- (?) d #                      
(INS)             0 24 5 -- (d) #                        
(MUL)             0 24 5 -- (*) c #                      
(INS)              0 120 -- (c) #                        
(FLP)              0 120 -- (&) $ ? d #                  
(DUP)              120 0 -- ($) ? d #                    
(BRN)            120 0 0 -- (?) d #                      
(DRP)              120 0 -- (#)                          
                     120 --


Fibonacci

A fibonacci number generating program is a bit simpler than the factorial. The function "a" simply checks the base case and calls "b". In the case where the input to a is not greater than 1 the comparison (>) returns a 0 and ? ends at a base case. The function "b" decrements by 1 and calls "a", then decrements by 2 and calls "a", then adds the two numbers on the stack together. So the recursive definition of the fibonacci sequence has been implemented. In the test below the 6th fibonacci number is generated, leaving the output 8 on the stack. The sequence is 1,1,2,3,5,8...

Dictionary:
: a $ 1 > ? b ;
: b $ -1 + a & -2 + a + ;
                         -- 6 a                        
(NUM)                    -- (6) a                        
(INS)                  6 -- (a)                          
(DUP)                  6 -- ($) 1 > ? b                  
(NUM)                6 6 -- (1) > ? b                    
(CMP)              6 6 1 -- (>) ? b                      
(BRN)                6 1 -- (?) b                        
(INS)                  6 -- (b)                          
(DUP)                  6 -- ($) -1 + a & -2 + a +        
(NUM)                6 6 -- (-1) + a & -2 + a +          
(ADD)             6 6 -1 -- (+) a & -2 + a +             
(INS)                6 5 -- (a) & -2 + a +               
(DUP)                6 5 -- ($) 1 > ? b & -2 + a +       
(NUM)              6 5 5 -- (1) > ? b & -2 + a +         
(CMP)            6 5 5 1 -- (>) ? b & -2 + a +           
(BRN)              6 5 1 -- (?) b & -2 + a +             
(INS)                6 5 -- (b) & -2 + a +               
(DUP)                6 5 -- ($) -1 + a & -2 + a + & -2 + a +
(NUM)              6 5 5 -- (-1) + a & -2 + a + & -2 + a +
(ADD)           6 5 5 -1 -- (+) a & -2 + a + & -2 + a +  
(INS)              6 5 4 -- (a) & -2 + a + & -2 + a +    
(DUP)              6 5 4 -- ($) 1 > ? b & -2 + a + & -2 + a +
(NUM)            6 5 4 4 -- (1) > ? b & -2 + a + & -2 + a +
(CMP)          6 5 4 4 1 -- (>) ? b & -2 + a + & -2 + a +
(BRN)            6 5 4 1 -- (?) b & -2 + a + & -2 + a +  
(INS)              6 5 4 -- (b) & -2 + a + & -2 + a +    
(DUP)              6 5 4 -- ($) -1 + a & -2 + a + & -2 + a + & -2 + a +
(NUM)            6 5 4 4 -- (-1) + a & -2 + a + & -2 + a + & -2 + a +
(ADD)         6 5 4 4 -1 -- (+) a & -2 + a + & -2 + a + & -2 + a +
(INS)            6 5 4 3 -- (a) & -2 + a + & -2 + a + & -2 + a +
(DUP)            6 5 4 3 -- ($) 1 > ? b & -2 + a + & -2 + a + & -2 + a +
(NUM)          6 5 4 3 3 -- (1) > ? b & -2 + a + & -2 + a + & -2 + a +
(CMP)        6 5 4 3 3 1 -- (>) ? b & -2 + a + & -2 + a + & -2 + a +
(BRN)          6 5 4 3 1 -- (?) b & -2 + a + & -2 + a + & -2 + a +
(INS)            6 5 4 3 -- (b) & -2 + a + & -2 + a + & -2 + a +
(DUP)            6 5 4 3 -- ($) -1 + a & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(NUM)          6 5 4 3 3 -- (-1) + a & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(ADD)       6 5 4 3 3 -1 -- (+) a & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(INS)          6 5 4 3 2 -- (a) & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(DUP)          6 5 4 3 2 -- ($) 1 > ? b & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(NUM)        6 5 4 3 2 2 -- (1) > ? b & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(CMP)      6 5 4 3 2 2 1 -- (>) ? b & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(BRN)        6 5 4 3 2 1 -- (?) b & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(INS)          6 5 4 3 2 -- (b) & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(DUP)          6 5 4 3 2 -- ($) -1 + a & -2 + a + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(NUM)        6 5 4 3 2 2 -- (-1) + a & -2 + a + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(ADD)     6 5 4 3 2 2 -1 -- (+) a & -2 + a + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(INS)        6 5 4 3 2 1 -- (a) & -2 + a + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(DUP)        6 5 4 3 2 1 -- ($) 1 > ? b & -2 + a + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(NUM)      6 5 4 3 2 1 1 -- (1) > ? b & -2 + a + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(CMP)    6 5 4 3 2 1 1 1 -- (>) ? b & -2 + a + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(BRN)      6 5 4 3 2 1 0 -- (?) b & -2 + a + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(FLP)        6 5 4 3 2 1 -- (&) -2 + a + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(NUM)        6 5 4 3 1 2 -- (-2) + a + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(ADD)     6 5 4 3 1 2 -2 -- (+) a + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(INS)        6 5 4 3 1 0 -- (a) + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(DUP)        6 5 4 3 1 0 -- ($) 1 > ? b + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(NUM)      6 5 4 3 1 0 0 -- (1) > ? b + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(CMP)    6 5 4 3 1 0 0 1 -- (>) ? b + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(BRN)      6 5 4 3 1 0 0 -- (?) b + & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(ADD)        6 5 4 3 1 0 -- (+) & -2 + a + & -2 + a + & -2 + a + & -2 + a +
(FLP)          6 5 4 3 1 -- (&) -2 + a + & -2 + a + & -2 + a + & -2 + a +
(NUM)          6 5 4 1 3 -- (-2) + a + & -2 + a + & -2 + a + & -2 + a +
(ADD)       6 5 4 1 3 -2 -- (+) a + & -2 + a + & -2 + a + & -2 + a +
(INS)          6 5 4 1 1 -- (a) + & -2 + a + & -2 + a + & -2 + a +
(DUP)          6 5 4 1 1 -- ($) 1 > ? b + & -2 + a + & -2 + a + & -2 + a +
(NUM)        6 5 4 1 1 1 -- (1) > ? b + & -2 + a + & -2 + a + & -2 + a +
(CMP)      6 5 4 1 1 1 1 -- (>) ? b + & -2 + a + & -2 + a + & -2 + a +
(BRN)        6 5 4 1 1 0 -- (?) b + & -2 + a + & -2 + a + & -2 + a +
(ADD)          6 5 4 1 1 -- (+) & -2 + a + & -2 + a + & -2 + a +
(FLP)            6 5 4 2 -- (&) -2 + a + & -2 + a + & -2 + a +
(NUM)            6 5 2 4 -- (-2) + a + & -2 + a + & -2 + a +
(ADD)         6 5 2 4 -2 -- (+) a + & -2 + a + & -2 + a +
(INS)            6 5 2 2 -- (a) + & -2 + a + & -2 + a +  
(DUP)            6 5 2 2 -- ($) 1 > ? b + & -2 + a + & -2 + a +
(NUM)          6 5 2 2 2 -- (1) > ? b + & -2 + a + & -2 + a +
(CMP)        6 5 2 2 2 1 -- (>) ? b + & -2 + a + & -2 + a +
(BRN)          6 5 2 2 1 -- (?) b + & -2 + a + & -2 + a +
(INS)            6 5 2 2 -- (b) + & -2 + a + & -2 + a +  
(DUP)            6 5 2 2 -- ($) -1 + a & -2 + a + + & -2 + a + & -2 + a +
(NUM)          6 5 2 2 2 -- (-1) + a & -2 + a + + & -2 + a + & -2 + a +
(ADD)       6 5 2 2 2 -1 -- (+) a & -2 + a + + & -2 + a + & -2 + a +
(INS)          6 5 2 2 1 -- (a) & -2 + a + + & -2 + a + & -2 + a +
(DUP)          6 5 2 2 1 -- ($) 1 > ? b & -2 + a + + & -2 + a + & -2 + a +
(NUM)        6 5 2 2 1 1 -- (1) > ? b & -2 + a + + & -2 + a + & -2 + a +
(CMP)      6 5 2 2 1 1 1 -- (>) ? b & -2 + a + + & -2 + a + & -2 + a +
(BRN)        6 5 2 2 1 0 -- (?) b & -2 + a + + & -2 + a + & -2 + a +
(FLP)          6 5 2 2 1 -- (&) -2 + a + + & -2 + a + & -2 + a +
(NUM)          6 5 2 1 2 -- (-2) + a + + & -2 + a + & -2 + a +
(ADD)       6 5 2 1 2 -2 -- (+) a + + & -2 + a + & -2 + a +
(INS)          6 5 2 1 0 -- (a) + + & -2 + a + & -2 + a +
(DUP)          6 5 2 1 0 -- ($) 1 > ? b + + & -2 + a + & -2 + a +
(NUM)        6 5 2 1 0 0 -- (1) > ? b + + & -2 + a + & -2 + a +
(CMP)      6 5 2 1 0 0 1 -- (>) ? b + + & -2 + a + & -2 + a +
(BRN)        6 5 2 1 0 0 -- (?) b + + & -2 + a + & -2 + a +
(ADD)          6 5 2 1 0 -- (+) + & -2 + a + & -2 + a +  
(ADD)            6 5 2 1 -- (+) & -2 + a + & -2 + a +    
(FLP)              6 5 3 -- (&) -2 + a + & -2 + a +      
(NUM)              6 3 5 -- (-2) + a + & -2 + a +        
(ADD)           6 3 5 -2 -- (+) a + & -2 + a +           
(INS)              6 3 3 -- (a) + & -2 + a +             
(DUP)              6 3 3 -- ($) 1 > ? b + & -2 + a +     
(NUM)            6 3 3 3 -- (1) > ? b + & -2 + a +       
(CMP)          6 3 3 3 1 -- (>) ? b + & -2 + a +         
(BRN)            6 3 3 1 -- (?) b + & -2 + a +           
(INS)              6 3 3 -- (b) + & -2 + a +             
(DUP)              6 3 3 -- ($) -1 + a & -2 + a + + & -2 + a +
(NUM)            6 3 3 3 -- (-1) + a & -2 + a + + & -2 + a +
(ADD)         6 3 3 3 -1 -- (+) a & -2 + a + + & -2 + a +
(INS)            6 3 3 2 -- (a) & -2 + a + + & -2 + a +  
(DUP)            6 3 3 2 -- ($) 1 > ? b & -2 + a + + & -2 + a +
(NUM)          6 3 3 2 2 -- (1) > ? b & -2 + a + + & -2 + a +
(CMP)        6 3 3 2 2 1 -- (>) ? b & -2 + a + + & -2 + a +
(BRN)          6 3 3 2 1 -- (?) b & -2 + a + + & -2 + a +
(INS)            6 3 3 2 -- (b) & -2 + a + + & -2 + a +  
(DUP)            6 3 3 2 -- ($) -1 + a & -2 + a + & -2 + a + + & -2 + a +
(NUM)          6 3 3 2 2 -- (-1) + a & -2 + a + & -2 + a + + & -2 + a +
(ADD)       6 3 3 2 2 -1 -- (+) a & -2 + a + & -2 + a + + & -2 + a +
(INS)          6 3 3 2 1 -- (a) & -2 + a + & -2 + a + + & -2 + a +
(DUP)          6 3 3 2 1 -- ($) 1 > ? b & -2 + a + & -2 + a + + & -2 + a +
(NUM)        6 3 3 2 1 1 -- (1) > ? b & -2 + a + & -2 + a + + & -2 + a +
(CMP)      6 3 3 2 1 1 1 -- (>) ? b & -2 + a + & -2 + a + + & -2 + a +
(BRN)        6 3 3 2 1 0 -- (?) b & -2 + a + & -2 + a + + & -2 + a +
(FLP)          6 3 3 2 1 -- (&) -2 + a + & -2 + a + + & -2 + a +
(NUM)          6 3 3 1 2 -- (-2) + a + & -2 + a + + & -2 + a +
(ADD)       6 3 3 1 2 -2 -- (+) a + & -2 + a + + & -2 + a +
(INS)          6 3 3 1 0 -- (a) + & -2 + a + + & -2 + a +
(DUP)          6 3 3 1 0 -- ($) 1 > ? b + & -2 + a + + & -2 + a +
(NUM)        6 3 3 1 0 0 -- (1) > ? b + & -2 + a + + & -2 + a +
(CMP)      6 3 3 1 0 0 1 -- (>) ? b + & -2 + a + + & -2 + a +
(BRN)        6 3 3 1 0 0 -- (?) b + & -2 + a + + & -2 + a +
(ADD)          6 3 3 1 0 -- (+) & -2 + a + + & -2 + a +  
(FLP)            6 3 3 1 -- (&) -2 + a + + & -2 + a +    
(NUM)            6 3 1 3 -- (-2) + a + + & -2 + a +      
(ADD)         6 3 1 3 -2 -- (+) a + + & -2 + a +         
(INS)            6 3 1 1 -- (a) + + & -2 + a +           
(DUP)            6 3 1 1 -- ($) 1 > ? b + + & -2 + a +   
(NUM)          6 3 1 1 1 -- (1) > ? b + + & -2 + a +     
(CMP)        6 3 1 1 1 1 -- (>) ? b + + & -2 + a +       
(BRN)          6 3 1 1 0 -- (?) b + + & -2 + a +         
(ADD)            6 3 1 1 -- (+) + & -2 + a +             
(ADD)              6 3 2 -- (+) & -2 + a +               
(FLP)                6 5 -- (&) -2 + a +                 
(NUM)                5 6 -- (-2) + a +                   
(ADD)             5 6 -2 -- (+) a +                      
(INS)                5 4 -- (a) +                        
(DUP)                5 4 -- ($) 1 > ? b +                
(NUM)              5 4 4 -- (1) > ? b +                  
(CMP)            5 4 4 1 -- (>) ? b +                    
(BRN)              5 4 1 -- (?) b +                      
(INS)                5 4 -- (b) +                        
(DUP)                5 4 -- ($) -1 + a & -2 + a + +      
(NUM)              5 4 4 -- (-1) + a & -2 + a + +        
(ADD)           5 4 4 -1 -- (+) a & -2 + a + +           
(INS)              5 4 3 -- (a) & -2 + a + +             
(DUP)              5 4 3 -- ($) 1 > ? b & -2 + a + +     
(NUM)            5 4 3 3 -- (1) > ? b & -2 + a + +       
(CMP)          5 4 3 3 1 -- (>) ? b & -2 + a + +         
(BRN)            5 4 3 1 -- (?) b & -2 + a + +           
(INS)              5 4 3 -- (b) & -2 + a + +             
(DUP)              5 4 3 -- ($) -1 + a & -2 + a + & -2 + a + +
(NUM)            5 4 3 3 -- (-1) + a & -2 + a + & -2 + a + +
(ADD)         5 4 3 3 -1 -- (+) a & -2 + a + & -2 + a + +
(INS)            5 4 3 2 -- (a) & -2 + a + & -2 + a + +  
(DUP)            5 4 3 2 -- ($) 1 > ? b & -2 + a + & -2 + a + +
(NUM)          5 4 3 2 2 -- (1) > ? b & -2 + a + & -2 + a + +
(CMP)        5 4 3 2 2 1 -- (>) ? b & -2 + a + & -2 + a + +
(BRN)          5 4 3 2 1 -- (?) b & -2 + a + & -2 + a + +
(INS)            5 4 3 2 -- (b) & -2 + a + & -2 + a + +  
(DUP)            5 4 3 2 -- ($) -1 + a & -2 + a + & -2 + a + & -2 + a + +
(NUM)          5 4 3 2 2 -- (-1) + a & -2 + a + & -2 + a + & -2 + a + +
(ADD)       5 4 3 2 2 -1 -- (+) a & -2 + a + & -2 + a + & -2 + a + +
(INS)          5 4 3 2 1 -- (a) & -2 + a + & -2 + a + & -2 + a + +
(DUP)          5 4 3 2 1 -- ($) 1 > ? b & -2 + a + & -2 + a + & -2 + a + +
(NUM)        5 4 3 2 1 1 -- (1) > ? b & -2 + a + & -2 + a + & -2 + a + +
(CMP)      5 4 3 2 1 1 1 -- (>) ? b & -2 + a + & -2 + a + & -2 + a + +
(BRN)        5 4 3 2 1 0 -- (?) b & -2 + a + & -2 + a + & -2 + a + +
(FLP)          5 4 3 2 1 -- (&) -2 + a + & -2 + a + & -2 + a + +
(NUM)          5 4 3 1 2 -- (-2) + a + & -2 + a + & -2 + a + +
(ADD)       5 4 3 1 2 -2 -- (+) a + & -2 + a + & -2 + a + +
(INS)          5 4 3 1 0 -- (a) + & -2 + a + & -2 + a + +
(DUP)          5 4 3 1 0 -- ($) 1 > ? b + & -2 + a + & -2 + a + +
(NUM)        5 4 3 1 0 0 -- (1) > ? b + & -2 + a + & -2 + a + +
(CMP)      5 4 3 1 0 0 1 -- (>) ? b + & -2 + a + & -2 + a + +
(BRN)        5 4 3 1 0 0 -- (?) b + & -2 + a + & -2 + a + +
(ADD)          5 4 3 1 0 -- (+) & -2 + a + & -2 + a + +  
(FLP)            5 4 3 1 -- (&) -2 + a + & -2 + a + +    
(NUM)            5 4 1 3 -- (-2) + a + & -2 + a + +      
(ADD)         5 4 1 3 -2 -- (+) a + & -2 + a + +         
(INS)            5 4 1 1 -- (a) + & -2 + a + +           
(DUP)            5 4 1 1 -- ($) 1 > ? b + & -2 + a + +   
(NUM)          5 4 1 1 1 -- (1) > ? b + & -2 + a + +     
(CMP)        5 4 1 1 1 1 -- (>) ? b + & -2 + a + +       
(BRN)          5 4 1 1 0 -- (?) b + & -2 + a + +         
(ADD)            5 4 1 1 -- (+) & -2 + a + +             
(FLP)              5 4 2 -- (&) -2 + a + +               
(NUM)              5 2 4 -- (-2) + a + +                 
(ADD)           5 2 4 -2 -- (+) a + +                    
(INS)              5 2 2 -- (a) + +                      
(DUP)              5 2 2 -- ($) 1 > ? b + +              
(NUM)            5 2 2 2 -- (1) > ? b + +                
(CMP)          5 2 2 2 1 -- (>) ? b + +                  
(BRN)            5 2 2 1 -- (?) b + +                    
(INS)              5 2 2 -- (b) + +                      
(DUP)              5 2 2 -- ($) -1 + a & -2 + a + + +    
(NUM)            5 2 2 2 -- (-1) + a & -2 + a + + +      
(ADD)         5 2 2 2 -1 -- (+) a & -2 + a + + +         
(INS)            5 2 2 1 -- (a) & -2 + a + + +           
(DUP)            5 2 2 1 -- ($) 1 > ? b & -2 + a + + +   
(NUM)          5 2 2 1 1 -- (1) > ? b & -2 + a + + +     
(CMP)        5 2 2 1 1 1 -- (>) ? b & -2 + a + + +       
(BRN)          5 2 2 1 0 -- (?) b & -2 + a + + +         
(FLP)            5 2 2 1 -- (&) -2 + a + + +             
(NUM)            5 2 1 2 -- (-2) + a + + +               
(ADD)         5 2 1 2 -2 -- (+) a + + +                  
(INS)            5 2 1 0 -- (a) + + +                    
(DUP)            5 2 1 0 -- ($) 1 > ? b + + +            
(NUM)          5 2 1 0 0 -- (1) > ? b + + +              
(CMP)        5 2 1 0 0 1 -- (>) ? b + + +                
(BRN)          5 2 1 0 0 -- (?) b + + +                  
(ADD)            5 2 1 0 -- (+) + +                      
(ADD)              5 2 1 -- (+) +                        
(ADD)                5 3 -- (+)                          
                       8 --


Odd

For the purpose of building up to the collatz sequence a function which returns 1 for odd numbers and 0 for even numbers is desired. The function "b" checks if the input is greater than 1, if so the function "a" is called. The function "a" simply decrements by 2 until the function is no longer greater than 1, meaning 1 or 0 is reached. The final output is then given by this value which is 1 if odd and 0 if even.

Checking an odd number (returns 1 for true):
Dictionary:
: a -2 + $ 1 > ? a ;
: b $ 1 > ? a ;

                         -- 3 b                        
(NUM)                    -- (3) b                        
(INS)                  3 -- (b)                          
(DUP)                  3 -- ($) 1 > ? a                  
(NUM)                3 3 -- (1) > ? a                    
(CMP)              3 3 1 -- (>) ? a                      
(BRN)                3 1 -- (?) a                        
(INS)                  3 -- (a)                          
(NUM)                  3 -- (-2) + $ 1 > ? a             
(ADD)               3 -2 -- (+) $ 1 > ? a                
(DUP)                  1 -- ($) 1 > ? a                  
(NUM)                1 1 -- (1) > ? a                    
(CMP)              1 1 1 -- (>) ? a                      
(BRN)                1 0 -- (?) a                        
                       1 --
Checking an even number (returns 0 for false):
Dictionary:
: a -2 + $ 1 > ? a ;
: b $ 1 > ? a ;

                         -- 4 b                        
(NUM)                    -- (4) b                        
(INS)                  4 -- (b)                          
(DUP)                  4 -- ($) 1 > ? a                  
(NUM)                4 4 -- (1) > ? a                    
(CMP)              4 4 1 -- (>) ? a                      
(BRN)                4 1 -- (?) a                        
(INS)                  4 -- (a)                          
(NUM)                  4 -- (-2) + $ 1 > ? a             
(ADD)               4 -2 -- (+) $ 1 > ? a                
(DUP)                  2 -- ($) 1 > ? a                  
(NUM)                2 2 -- (1) > ? a                    
(CMP)              2 2 1 -- (>) ? a                      
(BRN)                2 1 -- (?) a                        
(INS)                  2 -- (a)                          
(NUM)                  2 -- (-2) + $ 1 > ? a             
(ADD)               2 -2 -- (+) $ 1 > ? a                
(DUP)                  0 -- ($) 1 > ? a                  
(NUM)                0 0 -- (1) > ? a                    
(CMP)              0 0 1 -- (>) ? a                      
(BRN)                0 0 -- (?) a                        
                       0 --


2Div

The next function needed is a division by 2 function. This function operates by decrementing the input by 2 until it is no longer greater than 1. Before decrementing by 2 the previous number on the stack is used as a counter, starting from 0, to count the number of times the function has been decremented by 2. The body of this function takes place in "b" which adds 1 to the counter then swaps it behind the input value with &, then decrements by two and checks if still greater than 1. Otherwise the counter is placed in front again and b is called again. When there is nothing left on the stack but the counter and the input number the input number is dropped with #, leaving nothing behind but the result of the division. If the number is even the division will be exact otherwise the remainder is lost.

Even case:
Dictionary:
: a 0 b ;
: b 1 + & -2 + $ 1 > ? c ;
: c & b ;
: d a # ;

                         -- 6 d                        
(NUM)                    -- (6) d                        
(INS)                  6 -- (d)                          
(INS)                  6 -- (a) #                        
(NUM)                  6 -- (0) b #                      
(INS)                6 0 -- (b) #                        
(NUM)                6 0 -- (1) + & -2 + $ 1 > ? c #     
(ADD)              6 0 1 -- (+) & -2 + $ 1 > ? c #       
(FLP)                6 1 -- (&) -2 + $ 1 > ? c #         
(NUM)                1 6 -- (-2) + $ 1 > ? c #           
(ADD)             1 6 -2 -- (+) $ 1 > ? c #              
(DUP)                1 4 -- ($) 1 > ? c #                
(NUM)              1 4 4 -- (1) > ? c #                  
(CMP)            1 4 4 1 -- (>) ? c #                    
(BRN)              1 4 1 -- (?) c #                      
(INS)                1 4 -- (c) #                        
(FLP)                1 4 -- (&) b #                      
(INS)                4 1 -- (b) #                        
(NUM)                4 1 -- (1) + & -2 + $ 1 > ? c #     
(ADD)              4 1 1 -- (+) & -2 + $ 1 > ? c #       
(FLP)                4 2 -- (&) -2 + $ 1 > ? c #         
(NUM)                2 4 -- (-2) + $ 1 > ? c #           
(ADD)             2 4 -2 -- (+) $ 1 > ? c #              
(DUP)                2 2 -- ($) 1 > ? c #                
(NUM)              2 2 2 -- (1) > ? c #                  
(CMP)            2 2 2 1 -- (>) ? c #                    
(BRN)              2 2 1 -- (?) c #                      
(INS)                2 2 -- (c) #                        
(FLP)                2 2 -- (&) b #                      
(INS)                2 2 -- (b) #                        
(NUM)                2 2 -- (1) + & -2 + $ 1 > ? c #     
(ADD)              2 2 1 -- (+) & -2 + $ 1 > ? c #       
(FLP)                2 3 -- (&) -2 + $ 1 > ? c #         
(NUM)                3 2 -- (-2) + $ 1 > ? c #           
(ADD)             3 2 -2 -- (+) $ 1 > ? c #              
(DUP)                3 0 -- ($) 1 > ? c #                
(NUM)              3 0 0 -- (1) > ? c #                  
(CMP)            3 0 0 1 -- (>) ? c #                    
(BRN)              3 0 0 -- (?) c #                      
(DRP)                3 0 -- (#)                          
                       3 --

Odd case (remainder is dropped at the end):
Dictionary:
: a 0 b ;
: b 1 + & -2 + $ 1 > ? c ;
: c & b ;
: d a # ;

                         -- 7 d                        
(NUM)                    -- (7) d                        
(INS)                  7 -- (d)                          
(INS)                  7 -- (a) #                        
(NUM)                  7 -- (0) b #                      
(INS)                7 0 -- (b) #                        
(NUM)                7 0 -- (1) + & -2 + $ 1 > ? c #     
(ADD)              7 0 1 -- (+) & -2 + $ 1 > ? c #       
(FLP)                7 1 -- (&) -2 + $ 1 > ? c #         
(NUM)                1 7 -- (-2) + $ 1 > ? c #           
(ADD)             1 7 -2 -- (+) $ 1 > ? c #              
(DUP)                1 5 -- ($) 1 > ? c #                
(NUM)              1 5 5 -- (1) > ? c #                  
(CMP)            1 5 5 1 -- (>) ? c #                    
(BRN)              1 5 1 -- (?) c #                      
(INS)                1 5 -- (c) #                        
(FLP)                1 5 -- (&) b #                      
(INS)                5 1 -- (b) #                        
(NUM)                5 1 -- (1) + & -2 + $ 1 > ? c #     
(ADD)              5 1 1 -- (+) & -2 + $ 1 > ? c #       
(FLP)                5 2 -- (&) -2 + $ 1 > ? c #         
(NUM)                2 5 -- (-2) + $ 1 > ? c #           
(ADD)             2 5 -2 -- (+) $ 1 > ? c #              
(DUP)                2 3 -- ($) 1 > ? c #                
(NUM)              2 3 3 -- (1) > ? c #                  
(CMP)            2 3 3 1 -- (>) ? c #                    
(BRN)              2 3 1 -- (?) c #                      
(INS)                2 3 -- (c) #                        
(FLP)                2 3 -- (&) b #                      
(INS)                3 2 -- (b) #                        
(NUM)                3 2 -- (1) + & -2 + $ 1 > ? c #     
(ADD)              3 2 1 -- (+) & -2 + $ 1 > ? c #       
(FLP)                3 3 -- (&) -2 + $ 1 > ? c #         
(NUM)                3 3 -- (-2) + $ 1 > ? c #           
(ADD)             3 3 -2 -- (+) $ 1 > ? c #              
(DUP)                3 1 -- ($) 1 > ? c #                
(NUM)              3 1 1 -- (1) > ? c #                  
(CMP)            3 1 1 1 -- (>) ? c #                    
(BRN)              3 1 0 -- (?) c #                      
(DRP)                3 1 -- (#)                          
                       3 --


Collatz Sequence

Most of the work of defining the collatz sequence is done. The 2div function is copied over and renamed. A function 3np1 is added which multiplies by 3 and adds 1. The odd function is renamed and copied over. What is remaining is a check for the base case of the collataz sequence, in this case function "e" is used to duplicate the last value on the data stack and check if greater than 1, otherwise call "f". The function f carries out the body of the collatz sequence. It checks if the top of the data stack is odd, if so it calls "3np1" followed by "2div", otherwise the "3np1" instruction is dropped and only the "2div" is called before checking the base case with "e". Lastly since the top of the stack is copied over before checking if greater than 1, the sequence ends with an extra number on the stack. So the final function for calculating the collatz sequence requires one final # at the end given by the function "col". The final values left on the stack read off the collatz sequence 5, 16, 8, 4, 2, 1.
This concludes the demonstration of the language.

Dictionary:
: a 0 b ;
: b 1 + & -2 + $ 1 > ? c ;
: c & b ;
: 2div a # ;

: 3np1 3 * 1 + $ ;

: d -2 + $ 1 > ? d ;
: odd $ 1 > ? d ;

: e $ $ 1 > ? f ;
: f $ odd ? 3np1 2div e ;
: col e # ;

                         -- 5 col                      
(NUM)                    -- (5) col                      
(INS)                  5 -- (col)                        
(INS)                  5 -- (e) #                        
(DUP)                  5 -- ($) $ 1 > ? f #              
(DUP)                5 5 -- ($) 1 > ? f #                
(NUM)              5 5 5 -- (1) > ? f #                  
(CMP)            5 5 5 1 -- (>) ? f #                    
(BRN)              5 5 1 -- (?) f #                      
(INS)                5 5 -- (f) #                        
(DUP)                5 5 -- ($) odd ? 3np1 2div e #      
(INS)              5 5 5 -- (odd) ? 3np1 2div e #        
(DUP)              5 5 5 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)            5 5 5 5 -- (1) > ? d ? 3np1 2div e #    
(CMP)          5 5 5 5 1 -- (>) ? d ? 3np1 2div e #      
(BRN)            5 5 5 1 -- (?) d ? 3np1 2div e #        
(INS)              5 5 5 -- (d) ? 3np1 2div e #          
(NUM)              5 5 5 -- (-2) + $ 1 > ? d ? 3np1 2div e #
(ADD)           5 5 5 -2 -- (+) $ 1 > ? d ? 3np1 2div e #
(DUP)              5 5 3 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)            5 5 3 3 -- (1) > ? d ? 3np1 2div e #    
(CMP)          5 5 3 3 1 -- (>) ? d ? 3np1 2div e #      
(BRN)            5 5 3 1 -- (?) d ? 3np1 2div e #        
(INS)              5 5 3 -- (d) ? 3np1 2div e #          
(NUM)              5 5 3 -- (-2) + $ 1 > ? d ? 3np1 2div e #
(ADD)           5 5 3 -2 -- (+) $ 1 > ? d ? 3np1 2div e #
(DUP)              5 5 1 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)            5 5 1 1 -- (1) > ? d ? 3np1 2div e #    
(CMP)          5 5 1 1 1 -- (>) ? d ? 3np1 2div e #      
(BRN)            5 5 1 0 -- (?) d ? 3np1 2div e #        
(BRN)              5 5 1 -- (?) 3np1 2div e #            
(INS)                5 5 -- (3np1) 2div e #              
(NUM)                5 5 -- (3) * 1 + $ 2div e #         
(MUL)              5 5 3 -- (*) 1 + $ 2div e #           
(NUM)               5 15 -- (1) + $ 2div e #             
(ADD)             5 15 1 -- (+) $ 2div e #               
(DUP)               5 16 -- ($) 2div e #                 
(INS)            5 16 16 -- (2div) e #                   
(INS)            5 16 16 -- (a) # e #                    
(NUM)            5 16 16 -- (0) b # e #                  
(INS)          5 16 16 0 -- (b) # e #                    
(NUM)          5 16 16 0 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)        5 16 16 0 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)          5 16 16 1 -- (&) -2 + $ 1 > ? c # e #     
(NUM)          5 16 1 16 -- (-2) + $ 1 > ? c # e #       
(ADD)       5 16 1 16 -2 -- (+) $ 1 > ? c # e #          
(DUP)          5 16 1 14 -- ($) 1 > ? c # e #            
(NUM)       5 16 1 14 14 -- (1) > ? c # e #              
(CMP)     5 16 1 14 14 1 -- (>) ? c # e #                
(BRN)        5 16 1 14 1 -- (?) c # e #                  
(INS)          5 16 1 14 -- (c) # e #                    
(FLP)          5 16 1 14 -- (&) b # e #                  
(INS)          5 16 14 1 -- (b) # e #                    
(NUM)          5 16 14 1 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)        5 16 14 1 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)          5 16 14 2 -- (&) -2 + $ 1 > ? c # e #     
(NUM)          5 16 2 14 -- (-2) + $ 1 > ? c # e #       
(ADD)       5 16 2 14 -2 -- (+) $ 1 > ? c # e #          
(DUP)          5 16 2 12 -- ($) 1 > ? c # e #            
(NUM)       5 16 2 12 12 -- (1) > ? c # e #              
(CMP)     5 16 2 12 12 1 -- (>) ? c # e #                
(BRN)        5 16 2 12 1 -- (?) c # e #                  
(INS)          5 16 2 12 -- (c) # e #                    
(FLP)          5 16 2 12 -- (&) b # e #                  
(INS)          5 16 12 2 -- (b) # e #                    
(NUM)          5 16 12 2 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)        5 16 12 2 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)          5 16 12 3 -- (&) -2 + $ 1 > ? c # e #     
(NUM)          5 16 3 12 -- (-2) + $ 1 > ? c # e #       
(ADD)       5 16 3 12 -2 -- (+) $ 1 > ? c # e #          
(DUP)          5 16 3 10 -- ($) 1 > ? c # e #            
(NUM)       5 16 3 10 10 -- (1) > ? c # e #              
(CMP)     5 16 3 10 10 1 -- (>) ? c # e #                
(BRN)        5 16 3 10 1 -- (?) c # e #                  
(INS)          5 16 3 10 -- (c) # e #                    
(FLP)          5 16 3 10 -- (&) b # e #                  
(INS)          5 16 10 3 -- (b) # e #                    
(NUM)          5 16 10 3 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)        5 16 10 3 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)          5 16 10 4 -- (&) -2 + $ 1 > ? c # e #     
(NUM)          5 16 4 10 -- (-2) + $ 1 > ? c # e #       
(ADD)       5 16 4 10 -2 -- (+) $ 1 > ? c # e #          
(DUP)           5 16 4 8 -- ($) 1 > ? c # e #            
(NUM)         5 16 4 8 8 -- (1) > ? c # e #              
(CMP)       5 16 4 8 8 1 -- (>) ? c # e #                
(BRN)         5 16 4 8 1 -- (?) c # e #                  
(INS)           5 16 4 8 -- (c) # e #                    
(FLP)           5 16 4 8 -- (&) b # e #                  
(INS)           5 16 8 4 -- (b) # e #                    
(NUM)           5 16 8 4 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)         5 16 8 4 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)           5 16 8 5 -- (&) -2 + $ 1 > ? c # e #     
(NUM)           5 16 5 8 -- (-2) + $ 1 > ? c # e #       
(ADD)        5 16 5 8 -2 -- (+) $ 1 > ? c # e #          
(DUP)           5 16 5 6 -- ($) 1 > ? c # e #            
(NUM)         5 16 5 6 6 -- (1) > ? c # e #              
(CMP)       5 16 5 6 6 1 -- (>) ? c # e #                
(BRN)         5 16 5 6 1 -- (?) c # e #                  
(INS)           5 16 5 6 -- (c) # e #                    
(FLP)           5 16 5 6 -- (&) b # e #                  
(INS)           5 16 6 5 -- (b) # e #                    
(NUM)           5 16 6 5 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)         5 16 6 5 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)           5 16 6 6 -- (&) -2 + $ 1 > ? c # e #     
(NUM)           5 16 6 6 -- (-2) + $ 1 > ? c # e #       
(ADD)        5 16 6 6 -2 -- (+) $ 1 > ? c # e #          
(DUP)           5 16 6 4 -- ($) 1 > ? c # e #            
(NUM)         5 16 6 4 4 -- (1) > ? c # e #              
(CMP)       5 16 6 4 4 1 -- (>) ? c # e #                
(BRN)         5 16 6 4 1 -- (?) c # e #                  
(INS)           5 16 6 4 -- (c) # e #                    
(FLP)           5 16 6 4 -- (&) b # e #                  
(INS)           5 16 4 6 -- (b) # e #                    
(NUM)           5 16 4 6 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)         5 16 4 6 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)           5 16 4 7 -- (&) -2 + $ 1 > ? c # e #     
(NUM)           5 16 7 4 -- (-2) + $ 1 > ? c # e #       
(ADD)        5 16 7 4 -2 -- (+) $ 1 > ? c # e #          
(DUP)           5 16 7 2 -- ($) 1 > ? c # e #            
(NUM)         5 16 7 2 2 -- (1) > ? c # e #              
(CMP)       5 16 7 2 2 1 -- (>) ? c # e #                
(BRN)         5 16 7 2 1 -- (?) c # e #                  
(INS)           5 16 7 2 -- (c) # e #                    
(FLP)           5 16 7 2 -- (&) b # e #                  
(INS)           5 16 2 7 -- (b) # e #                    
(NUM)           5 16 2 7 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)         5 16 2 7 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)           5 16 2 8 -- (&) -2 + $ 1 > ? c # e #     
(NUM)           5 16 8 2 -- (-2) + $ 1 > ? c # e #       
(ADD)        5 16 8 2 -2 -- (+) $ 1 > ? c # e #          
(DUP)           5 16 8 0 -- ($) 1 > ? c # e #            
(NUM)         5 16 8 0 0 -- (1) > ? c # e #              
(CMP)       5 16 8 0 0 1 -- (>) ? c # e #                
(BRN)         5 16 8 0 0 -- (?) c # e #                  
(DRP)           5 16 8 0 -- (#) e #                      
(INS)             5 16 8 -- (e) #                        
(DUP)             5 16 8 -- ($) $ 1 > ? f #              
(DUP)           5 16 8 8 -- ($) 1 > ? f #                
(NUM)         5 16 8 8 8 -- (1) > ? f #                  
(CMP)       5 16 8 8 8 1 -- (>) ? f #                    
(BRN)         5 16 8 8 1 -- (?) f #                      
(INS)           5 16 8 8 -- (f) #                        
(DUP)           5 16 8 8 -- ($) odd ? 3np1 2div e #      
(INS)         5 16 8 8 8 -- (odd) ? 3np1 2div e #        
(DUP)         5 16 8 8 8 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)       5 16 8 8 8 8 -- (1) > ? d ? 3np1 2div e #    
(CMP)     5 16 8 8 8 8 1 -- (>) ? d ? 3np1 2div e #      
(BRN)       5 16 8 8 8 1 -- (?) d ? 3np1 2div e #        
(INS)         5 16 8 8 8 -- (d) ? 3np1 2div e #          
(NUM)         5 16 8 8 8 -- (-2) + $ 1 > ? d ? 3np1 2div e #
(ADD)      5 16 8 8 8 -2 -- (+) $ 1 > ? d ? 3np1 2div e #
(DUP)         5 16 8 8 6 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)       5 16 8 8 6 6 -- (1) > ? d ? 3np1 2div e #    
(CMP)     5 16 8 8 6 6 1 -- (>) ? d ? 3np1 2div e #      
(BRN)       5 16 8 8 6 1 -- (?) d ? 3np1 2div e #        
(INS)         5 16 8 8 6 -- (d) ? 3np1 2div e #          
(NUM)         5 16 8 8 6 -- (-2) + $ 1 > ? d ? 3np1 2div e #
(ADD)      5 16 8 8 6 -2 -- (+) $ 1 > ? d ? 3np1 2div e #
(DUP)         5 16 8 8 4 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)       5 16 8 8 4 4 -- (1) > ? d ? 3np1 2div e #    
(CMP)     5 16 8 8 4 4 1 -- (>) ? d ? 3np1 2div e #      
(BRN)       5 16 8 8 4 1 -- (?) d ? 3np1 2div e #        
(INS)         5 16 8 8 4 -- (d) ? 3np1 2div e #          
(NUM)         5 16 8 8 4 -- (-2) + $ 1 > ? d ? 3np1 2div e #
(ADD)      5 16 8 8 4 -2 -- (+) $ 1 > ? d ? 3np1 2div e #
(DUP)         5 16 8 8 2 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)       5 16 8 8 2 2 -- (1) > ? d ? 3np1 2div e #    
(CMP)     5 16 8 8 2 2 1 -- (>) ? d ? 3np1 2div e #      
(BRN)       5 16 8 8 2 1 -- (?) d ? 3np1 2div e #        
(INS)         5 16 8 8 2 -- (d) ? 3np1 2div e #          
(NUM)         5 16 8 8 2 -- (-2) + $ 1 > ? d ? 3np1 2div e #
(ADD)      5 16 8 8 2 -2 -- (+) $ 1 > ? d ? 3np1 2div e #
(DUP)         5 16 8 8 0 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)       5 16 8 8 0 0 -- (1) > ? d ? 3np1 2div e #    
(CMP)     5 16 8 8 0 0 1 -- (>) ? d ? 3np1 2div e #      
(BRN)       5 16 8 8 0 0 -- (?) d ? 3np1 2div e #        
(BRN)         5 16 8 8 0 -- (?) 3np1 2div e #            
(INS)           5 16 8 8 -- (2div) e #                   
(INS)           5 16 8 8 -- (a) # e #                    
(NUM)           5 16 8 8 -- (0) b # e #                  
(INS)         5 16 8 8 0 -- (b) # e #                    
(NUM)         5 16 8 8 0 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)       5 16 8 8 0 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)         5 16 8 8 1 -- (&) -2 + $ 1 > ? c # e #     
(NUM)         5 16 8 1 8 -- (-2) + $ 1 > ? c # e #       
(ADD)      5 16 8 1 8 -2 -- (+) $ 1 > ? c # e #          
(DUP)         5 16 8 1 6 -- ($) 1 > ? c # e #            
(NUM)       5 16 8 1 6 6 -- (1) > ? c # e #              
(CMP)     5 16 8 1 6 6 1 -- (>) ? c # e #                
(BRN)       5 16 8 1 6 1 -- (?) c # e #                  
(INS)         5 16 8 1 6 -- (c) # e #                    
(FLP)         5 16 8 1 6 -- (&) b # e #                  
(INS)         5 16 8 6 1 -- (b) # e #                    
(NUM)         5 16 8 6 1 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)       5 16 8 6 1 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)         5 16 8 6 2 -- (&) -2 + $ 1 > ? c # e #     
(NUM)         5 16 8 2 6 -- (-2) + $ 1 > ? c # e #       
(ADD)      5 16 8 2 6 -2 -- (+) $ 1 > ? c # e #          
(DUP)         5 16 8 2 4 -- ($) 1 > ? c # e #            
(NUM)       5 16 8 2 4 4 -- (1) > ? c # e #              
(CMP)     5 16 8 2 4 4 1 -- (>) ? c # e #                
(BRN)       5 16 8 2 4 1 -- (?) c # e #                  
(INS)         5 16 8 2 4 -- (c) # e #                    
(FLP)         5 16 8 2 4 -- (&) b # e #                  
(INS)         5 16 8 4 2 -- (b) # e #                    
(NUM)         5 16 8 4 2 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)       5 16 8 4 2 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)         5 16 8 4 3 -- (&) -2 + $ 1 > ? c # e #     
(NUM)         5 16 8 3 4 -- (-2) + $ 1 > ? c # e #       
(ADD)      5 16 8 3 4 -2 -- (+) $ 1 > ? c # e #          
(DUP)         5 16 8 3 2 -- ($) 1 > ? c # e #            
(NUM)       5 16 8 3 2 2 -- (1) > ? c # e #              
(CMP)     5 16 8 3 2 2 1 -- (>) ? c # e #                
(BRN)       5 16 8 3 2 1 -- (?) c # e #                  
(INS)         5 16 8 3 2 -- (c) # e #                    
(FLP)         5 16 8 3 2 -- (&) b # e #                  
(INS)         5 16 8 2 3 -- (b) # e #                    
(NUM)         5 16 8 2 3 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)       5 16 8 2 3 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)         5 16 8 2 4 -- (&) -2 + $ 1 > ? c # e #     
(NUM)         5 16 8 4 2 -- (-2) + $ 1 > ? c # e #       
(ADD)      5 16 8 4 2 -2 -- (+) $ 1 > ? c # e #          
(DUP)         5 16 8 4 0 -- ($) 1 > ? c # e #            
(NUM)       5 16 8 4 0 0 -- (1) > ? c # e #              
(CMP)     5 16 8 4 0 0 1 -- (>) ? c # e #                
(BRN)       5 16 8 4 0 0 -- (?) c # e #                  
(DRP)         5 16 8 4 0 -- (#) e #                      
(INS)           5 16 8 4 -- (e) #                        
(DUP)           5 16 8 4 -- ($) $ 1 > ? f #              
(DUP)         5 16 8 4 4 -- ($) 1 > ? f #                
(NUM)       5 16 8 4 4 4 -- (1) > ? f #                  
(CMP)     5 16 8 4 4 4 1 -- (>) ? f #                    
(BRN)       5 16 8 4 4 1 -- (?) f #                      
(INS)         5 16 8 4 4 -- (f) #                        
(DUP)         5 16 8 4 4 -- ($) odd ? 3np1 2div e #      
(INS)       5 16 8 4 4 4 -- (odd) ? 3np1 2div e #        
(DUP)       5 16 8 4 4 4 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)     5 16 8 4 4 4 4 -- (1) > ? d ? 3np1 2div e #    
(CMP)   5 16 8 4 4 4 4 1 -- (>) ? d ? 3np1 2div e #      
(BRN)     5 16 8 4 4 4 1 -- (?) d ? 3np1 2div e #        
(INS)       5 16 8 4 4 4 -- (d) ? 3np1 2div e #          
(NUM)       5 16 8 4 4 4 -- (-2) + $ 1 > ? d ? 3np1 2div e #
(ADD)    5 16 8 4 4 4 -2 -- (+) $ 1 > ? d ? 3np1 2div e #
(DUP)       5 16 8 4 4 2 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)     5 16 8 4 4 2 2 -- (1) > ? d ? 3np1 2div e #    
(CMP)   5 16 8 4 4 2 2 1 -- (>) ? d ? 3np1 2div e #      
(BRN)     5 16 8 4 4 2 1 -- (?) d ? 3np1 2div e #        
(INS)       5 16 8 4 4 2 -- (d) ? 3np1 2div e #          
(NUM)       5 16 8 4 4 2 -- (-2) + $ 1 > ? d ? 3np1 2div e #
(ADD)    5 16 8 4 4 2 -2 -- (+) $ 1 > ? d ? 3np1 2div e #
(DUP)       5 16 8 4 4 0 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)     5 16 8 4 4 0 0 -- (1) > ? d ? 3np1 2div e #    
(CMP)   5 16 8 4 4 0 0 1 -- (>) ? d ? 3np1 2div e #      
(BRN)     5 16 8 4 4 0 0 -- (?) d ? 3np1 2div e #        
(BRN)       5 16 8 4 4 0 -- (?) 3np1 2div e #            
(INS)         5 16 8 4 4 -- (2div) e #                   
(INS)         5 16 8 4 4 -- (a) # e #                    
(NUM)         5 16 8 4 4 -- (0) b # e #                  
(INS)       5 16 8 4 4 0 -- (b) # e #                    
(NUM)       5 16 8 4 4 0 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)     5 16 8 4 4 0 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)       5 16 8 4 4 1 -- (&) -2 + $ 1 > ? c # e #     
(NUM)       5 16 8 4 1 4 -- (-2) + $ 1 > ? c # e #       
(ADD)    5 16 8 4 1 4 -2 -- (+) $ 1 > ? c # e #          
(DUP)       5 16 8 4 1 2 -- ($) 1 > ? c # e #            
(NUM)     5 16 8 4 1 2 2 -- (1) > ? c # e #              
(CMP)   5 16 8 4 1 2 2 1 -- (>) ? c # e #                
(BRN)     5 16 8 4 1 2 1 -- (?) c # e #                  
(INS)       5 16 8 4 1 2 -- (c) # e #                    
(FLP)       5 16 8 4 1 2 -- (&) b # e #                  
(INS)       5 16 8 4 2 1 -- (b) # e #                    
(NUM)       5 16 8 4 2 1 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)     5 16 8 4 2 1 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)       5 16 8 4 2 2 -- (&) -2 + $ 1 > ? c # e #     
(NUM)       5 16 8 4 2 2 -- (-2) + $ 1 > ? c # e #       
(ADD)    5 16 8 4 2 2 -2 -- (+) $ 1 > ? c # e #          
(DUP)       5 16 8 4 2 0 -- ($) 1 > ? c # e #            
(NUM)     5 16 8 4 2 0 0 -- (1) > ? c # e #              
(CMP)   5 16 8 4 2 0 0 1 -- (>) ? c # e #                
(BRN)     5 16 8 4 2 0 0 -- (?) c # e #                  
(DRP)       5 16 8 4 2 0 -- (#) e #                      
(INS)         5 16 8 4 2 -- (e) #                        
(DUP)         5 16 8 4 2 -- ($) $ 1 > ? f #              
(DUP)       5 16 8 4 2 2 -- ($) 1 > ? f #                
(NUM)     5 16 8 4 2 2 2 -- (1) > ? f #                  
(CMP)   5 16 8 4 2 2 2 1 -- (>) ? f #                    
(BRN)     5 16 8 4 2 2 1 -- (?) f #                      
(INS)       5 16 8 4 2 2 -- (f) #                        
(DUP)       5 16 8 4 2 2 -- ($) odd ? 3np1 2div e #      
(INS)     5 16 8 4 2 2 2 -- (odd) ? 3np1 2div e #        
(DUP)     5 16 8 4 2 2 2 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)   5 16 8 4 2 2 2 2 -- (1) > ? d ? 3np1 2div e #    
(CMP) 5 16 8 4 2 2 2 2 1 -- (>) ? d ? 3np1 2div e #      
(BRN)   5 16 8 4 2 2 2 1 -- (?) d ? 3np1 2div e #        
(INS)     5 16 8 4 2 2 2 -- (d) ? 3np1 2div e #          
(NUM)     5 16 8 4 2 2 2 -- (-2) + $ 1 > ? d ? 3np1 2div e #
(ADD)  5 16 8 4 2 2 2 -2 -- (+) $ 1 > ? d ? 3np1 2div e #
(DUP)     5 16 8 4 2 2 0 -- ($) 1 > ? d ? 3np1 2div e #  
(NUM)   5 16 8 4 2 2 0 0 -- (1) > ? d ? 3np1 2div e #    
(CMP) 5 16 8 4 2 2 0 0 1 -- (>) ? d ? 3np1 2div e #      
(BRN)   5 16 8 4 2 2 0 0 -- (?) d ? 3np1 2div e #        
(BRN)     5 16 8 4 2 2 0 -- (?) 3np1 2div e #            
(INS)       5 16 8 4 2 2 -- (2div) e #                   
(INS)       5 16 8 4 2 2 -- (a) # e #                    
(NUM)       5 16 8 4 2 2 -- (0) b # e #                  
(INS)     5 16 8 4 2 2 0 -- (b) # e #                    
(NUM)     5 16 8 4 2 2 0 -- (1) + & -2 + $ 1 > ? c # e # 
(ADD)   5 16 8 4 2 2 0 1 -- (+) & -2 + $ 1 > ? c # e #   
(FLP)     5 16 8 4 2 2 1 -- (&) -2 + $ 1 > ? c # e #     
(NUM)     5 16 8 4 2 1 2 -- (-2) + $ 1 > ? c # e #       
(ADD)  5 16 8 4 2 1 2 -2 -- (+) $ 1 > ? c # e #          
(DUP)     5 16 8 4 2 1 0 -- ($) 1 > ? c # e #            
(NUM)   5 16 8 4 2 1 0 0 -- (1) > ? c # e #              
(CMP) 5 16 8 4 2 1 0 0 1 -- (>) ? c # e #                
(BRN)   5 16 8 4 2 1 0 0 -- (?) c # e #                  
(DRP)     5 16 8 4 2 1 0 -- (#) e #                      
(INS)       5 16 8 4 2 1 -- (e) #                        
(DUP)       5 16 8 4 2 1 -- ($) $ 1 > ? f #              
(DUP)     5 16 8 4 2 1 1 -- ($) 1 > ? f #                
(NUM)   5 16 8 4 2 1 1 1 -- (1) > ? f #                  
(CMP) 5 16 8 4 2 1 1 1 1 -- (>) ? f #                    
(BRN)   5 16 8 4 2 1 1 0 -- (?) f #                      
(DRP)     5 16 8 4 2 1 1 -- (#)                          
            5 16 8 4 2 1 --