by showmyiq » Sun Mar 17, 2013 2:38 pm
In this chapter we are going to discuss some embedded stack manipulations.
The first command is count. It will count the total elements in the stack and push the result in the top of the stack. For example if you have a stack with X elements inside and if you execute the command – count, the result will be X and the value X will be added as an element at the top of the stack (so now the total count of the elements will be X+1).
The second command is exch. It is abbreviation from the word exchange. It will exchange the top 2 elements of the stack. Let’s test that with this command: 1 2 3 4 5 6 exch
5
6
4
3
2
1
GS<6>
As you can see the top 2 elements of the stack are exchanged.
The next command is copy. The syntax is X copy. The X should be an integer value, which defines how many elements from the top of the stack should be copied. For example if you execute this command: 0 copy, then you will do nothing – the stack will stay unchanged. If you execute: 1 copy, the top element will be copied and situated on the top of the stack. What about the command: 2 copy? Let’s find out:
1 2 3 4 5 2 copy
The new modified stack will look like this:
5
4
5
4
3
2
1
GS<7>
You can see that the elements are copied in the same order. The command dup is subset of the copy command, because dup is replicating the top element of the stack – exactly what 1 copy does. Test by yourself the command: 1 2 3 4 dup
The last command we are going to look through this chapter is roll. You should pay attention here, because this is the most difficult command from this chapter. The syntax is X Y roll, where X and Y are integer values. X defines the count of the top elements that will participate in the roll operation, the Y on the other hand defines the number of rolls we are going to do and the direction of the roll. I know it sounds complicated, but in fact it’s very amusing command. You know those bicycle locks, used to secure the bike on some solid fence, tree, etc.? Usually the locking mechanism consists of 4 metal circles with all the digits 0-9 (total 10 digits). You should guess the correct combination of the four metal discs in order to unlock the mechanism. Imagine you have some in your hands now. Let’s start manipulating the first metal disc. You can rotate it up or down. That’s why we implemented Y as direction. You can rotate it just once in the specific direction, or twice or more. That’s why we implemented Y as the count of rolls in the specific direction. Because the direction is binary value (as you saw we can rotate in 2 directions only – up or down) we can use the sign of the Y as the direction. The value of Y on the other hand, will be how many times we are going to rotate on that direction.
In this specific first metal disc the X is equal to 10 (because we have 10 digits). But what if we need to rotate only 3 digits? Or five? Well, that’s why we need X as argument too, so we can be more flexible.
Enough talking, let’s do some examples. Execute this command:
1 2 3 4
2 1 roll
stack
The result should be:
3
4
2
1
GS<4>
As you can see the original stack was 1 2 3 4. Then the 2 1 roll command is executed. Which translated to our language means – take the top 2 elements of the stack and rotate them up with 1 round.
Let’s now try this command:
1 2 3 4
3 1 roll
Stack
The result should be:
3
2
4
1
GS<4>
Now what if we executed this command instead: 3 -1 roll
Let’s see what would be the result:
2
4
3
1
GS<4>
As you can see now we rotated the “disc” down and the result is different.
You can test and analyze the roll command with different examples and stacks. As a conclusion maybe you have already noticed that in fact exch is a subset of the roll command. It is equal to the command: 2 1 roll (which on the other hand is equal to 2 -1 roll too)
In the next chapter we are going to see how to compare values and what happens with the stack.