What is a stack?
To explain what a stack is and how it can be used, I drew this picture.Imagine a vertical round pole fixed to the middle of a round bottom plate.
Also imagine round items that can be put on the stack, because those imaginary items have a round hole in it to fit around the pole.
At first the stack is empty.
You then can put item 1 on the stack, by pushing it over the pole, until it rests at the bottom plate.
When you then push item 2 on the stack in the same way, item 2 is right on top of item 1. Item 3 can then be pushed onto item 2.
When you want to remove item 3 in the picture, there is no problem. However, when you want to remove item 2 or item 1, you first have to remove all items on top from the stack before you can do that!
How is the stack used in 8th?
The stack is only used to temporarily store individual data items. Therefore it is often called the data stack.In 8th we call all native and all self defined functions or procudures words.
There are quite a lot of words which can manipulate the stack.
Pushing items on the stack in 8th
The .s word shows the current contents of the stack. Right now it is empty.Now we enter the values 1, 2 and 3.
With .s we now can see that 1 is at the bottom of the stack and 3 at the top (TOS).
When we enter another value (4) that item lands at TOS.
Instead of entering 3 values in a row, we could also enter those values one by one. It makes no difference.
With the reset word we clear the stack. All items are removed.
Afterwards the stack is empty again.

No comments:
Post a Comment