Difference between revisions of "Stack"

From WikiManual
Jump to: navigation, search
 
(The Stack)
Line 1: Line 1:
 
==The Stack==
 
==The Stack==
'''Overview''' The basic principle is that everything is done passing through a last-in first-out stack. It's like a stack of dishes: you can take or put a dish only from the top of the stack, all others are inaccessible. The language's instructions work on the data that is on the top of the stack, removing them and putting the results in their place.
+
'''Overview'''  
 +
 
 +
The basic principle is that everything is done passing through a last-in first-out stack. It's like a stack of dishes: you can take or put a dish only from the top of the stack, all others are inaccessible. The language's instructions work on the data that is on the top of the stack, removing them and putting the results in their place.
 
----
 
----
 
For example, to sum two numbers, you should put them in the stack, and then invoke the sum instruction. It will remove the two numbers, calculate the sum, and put the result on top of the stack.
 
For example, to sum two numbers, you should put them in the stack, and then invoke the sum instruction. It will remove the two numbers, calculate the sum, and put the result on top of the stack.
Line 14: Line 16:
 
The add instruction removed b and c from the stack and replaced it with the sum of the two, a.
 
The add instruction removed b and c from the stack and replaced it with the sum of the two, a.
  
Just like a pile of dishes can get too big, when the stack fills up with numbers it will begin to overflow and will not accept any new numbers. Ordinarily very few numbers remain in the stack for more than one gene or cycle, but sloppy or mutated DNA can cause this. It is easy to empty the stack by placing several add, sub, or mult commands in a row.
+
Just like a pile of dishes can get too big, when the stack fills up with numbers it will begin to overflow and will lose the oldest numbers. Ordinarily very few numbers remain in the stack for more than one gene or cycle, but sloppy or mutated DNA can cause this. It is easy to empty the stack by placing several add, sub, or mult commands in a row.

Revision as of 21:16, 18 September 2005

The Stack

Overview

The basic principle is that everything is done passing through a last-in first-out stack. It's like a stack of dishes: you can take or put a dish only from the top of the stack, all others are inaccessible. The language's instructions work on the data that is on the top of the stack, removing them and putting the results in their place.


For example, to sum two numbers, you should put them in the stack, and then invoke the sum instruction. It will remove the two numbers, calculate the sum, and put the result on top of the stack.

It may seem complicated, but's not. For instance, where you usually write

b+c=a

now you should write

b c add

The add instruction removed b and c from the stack and replaced it with the sum of the two, a.

Just like a pile of dishes can get too big, when the stack fills up with numbers it will begin to overflow and will lose the oldest numbers. Ordinarily very few numbers remain in the stack for more than one gene or cycle, but sloppy or mutated DNA can cause this. It is easy to empty the stack by placing several add, sub, or mult commands in a row.