13 December 2015

Calculating in 8th

Today we will have a look at some practical examples of 8th. We will use 8th as a calculator.

In earlier blog entries I created nice pictures to explain the inner workings of the stack.
As I might assume you now have an idea of how the stack works.

The stack in 8th is a data stack. This means that the stack only contains data needed for computations and operations.

In the following examples I will use the 8th prompt "ok>".

Example 1


First we show that the stack is empty in our example case.

.s \ displays the current stack contents
stack empty

ok> 3 4

This means the following:
   1. push the value 3 on the data stack
   2. push the value 4 on the data stack

Let's see what is on the stack now:

ok> .s
2 n: 0000000002ccbe90 1   4
1 n: 0000000002ccbe60 1   3

The output means (per line for each item on the stack):
   1st  value = the index of this item on the stack
   2nd value = the class of this item on the stack (we will talk about that later)
   3rd  value = the memory address of this item on the stack
   4th  value = the reference count of this item on the stack (needed for garbage collection)

Now we are going to use the + word with SED \ + x y -- z 

ok> +

The + word takes the 2 topmost items from the stack and adds them. Then pushes the result back on the stack. Let's check the stack contents again:

ok> .s
1 n: 0000000002ccbec0 1   7

Using the earlier explanation we now see that:
   - items with index 1 and 2 (having values 3 and 4) have disappeared from the stack
   - a new item is now on the stack with the resulting value 7

In 8th is does not matter how you enter these things; you also might have done:
  ok> 3
  ok> 4
  ok> +
or the more common way:
  ok> 3 4 +

Example 2


Now a more complicated example. We assume that the stack is empty when we begin.

ok> 3 4 + 8 * 6 -
ok> .s
1 n: 0000000002ccc400 1   50

Explanation:
   1. 8th pushes in turn the values 3 and 4 on the stack
   2. the + word takes those values, adds them and pushes the result back on the stack,
       so the stack then contains 1 item with value 7
   3. 8th pushes value 8 on the stack,
       so the stack then contains 2 items with values 7 and 8
   4. the * word takes those values, multiplies them and pushes the result back on the stack,
       so the stack then contains 1 item with value 56
   5. 8th pushes the value 6 on the stack,
       so the stack then contains 2 items with values 56 and 6
   6. the - word takes those values from the stack, subtracts the TOS value from the other value
       and pushes the result back on the stack,
       so then the stack contains 1 item with value 50

We mentioned the * and - words here. They have the following SED's:
  * \ x y -- z
      where z = x * y
   - \ x y -- z
      where z = x - y


Conclusion


It is very easy to calculate in 8th. Always keep in mind that the stack should contain the appropriate data when you'd like to evaluate an 8th word.

It is strongly advised to keep the number of items on the stack as low as possible.
Reasons:
   - much easier to reason about what is going on at a given moment
   - the stack has a maximum number of items it can hold

Now you have seen the words for calculation in action, you will be able to understand other 8th words also. Please try it yourself.

Please have a look at the 8th website and grab your free version there: http://8th-dev.com/

No comments:

Post a Comment