Working with the stack
Everything in Forth revolves around the stack. Since (just about) all words operate using parameters on the stack, Forth provides tools for the programmer to change the contents of the stack, or to alter the order of values on the stack.
The Forth word .s shows the contents of the stack without removing any entries. This is a useful tool for the programmer, enabling you to look at what parameters are on the stack without affecting anything. If the stack is empty, .s outputs nothing.
FlashForth also has a word .st that shows you the current base (# for decimal, $ for hex and % for binary), the current data section in use (RAM or flash) and the stack contents. An output from .st might look like this: <#,ram> 4 6 9 ok
Other useful stack words are dup (duplicate the entry on top of the stack), drop (discard the entry on top of the stack), swap (exchange the two top entries on the stack) and over (copy the second-most entry to the top of the stack).
For example, entering 5 dup results in 5 5 on the stack. 3 4 swap results in 4 3 on the stack. 1 2 over results in 1 2 1 on the stack. dup is often used to create copies of parameters when more than one word requires that parameter.
For example, here is Forth code that squares the value of 65:
The Forth word .s shows the contents of the stack without removing any entries. This is a useful tool for the programmer, enabling you to look at what parameters are on the stack without affecting anything. If the stack is empty, .s outputs nothing.
FlashForth also has a word .st that shows you the current base (# for decimal, $ for hex and % for binary), the current data section in use (RAM or flash) and the stack contents. An output from .st might look like this: <#,ram> 4 6 9 ok
Other useful stack words are dup (duplicate the entry on top of the stack), drop (discard the entry on top of the stack), swap (exchange the two top entries on the stack) and over (copy the second-most entry to the top of the stack).
For example, entering 5 dup results in 5 5 on the stack. 3 4 swap results in 4 3 on the stack. 1 2 over results in 1 2 1 on the stack. dup is often used to create copies of parameters when more than one word requires that parameter.
For example, here is Forth code that squares the value of 65:
65 dup * .
In most implementations of Forth, dup will generate an error if the stack is empty, and execution will terminate. Therefore, many Forths include a word ?dup that will first check for a parameter on the top of the stack before trying to duplicate it. In FlashForth, this isn’t necessary as dup ignores an empty stack. The FlashForth dup is the equivalent of ?dup in other Forths.
2dup will duplicate the top two stack entries. For example:
10 23 2dup
gives 10 23 10 23 on the stack. Similarly, 2swap and 2drop operate on the top two stack entries, as well as doubles (32-bit numbers), explained in a later section.
The Forth word rot rotates the third stack parameter to the top. For example:
The Forth word rot rotates the third stack parameter to the top. For example:
1 2 3 rot
results in 2 3 1 on the stack.
The word nip removes the second item from the stack, while the word tuck copies the top item to the third position.
The word nip removes the second item from the stack, while the word tuck copies the top item to the third position.
78 12 nip
gives 12 and:
78 12 tuck
gives 12 78 12 on the stack. And n pick copies the nth item to the top of the stack.
Now, let's learn how to create programs in Forth.