Note: if you haven’t yet installed 8th, please do so right now and follow the installation instructions as presented in the manual!
Simple stack manipulations
In the previous lesson you learned about the stack in general. In this one you’ll learn some of the more common stack manipulation words.
Let’s learn by playing around with 8th. Start 8th, and in the console type
There are a lot of stack manipulation words in 8th. Here are some of the most common used, along with their stack-effect diagrams:
- dup — \ a -- a a
- drop — \ a --
- swap — \ a b -- b a
- over — \ a b -- a b a
- tuck — \ a b -- b a b
- nip — \ a b -- b
- rot — \ a b c -- b c a
- -rot — \ a b c -- c a b
- 2dup — \ a b -- a b a b
- 2swap — \ a b c d -- c d a b
Try the following. Before each exercise type reset 10 20 30 — note that “reset” clears the stack so you’re starting again with a clean slate. Check the stack using the .s word:
- Make the stack look like: 10 30 20
- Make the stack look like: 10 20 30 20 30
- Make the stack look like: 20 30 10
- Make the stack look like: 20 10 30
- Make the stack look like: 20
You can now manipulate the order of items on the stack. This is a fundamental skill you’ll use over and over when programming in 8th.