4.4 The global and local Stack

<< Click to Display Table of Contents >>

Navigation:  2. Components > 4. Special features >

4.4 The global and local Stack

Stack, GLOBAL and LOCAL

Previous Top Next


MiniRobotLanguage (MRL)

 

The Stack - global and Local

Intention

 

Why can you do with a stack?

 

The stack is a space where you can store hundreds of items. These items can be strings (text) or numeric items. Note that

 

Commands use it, too

Many commands leave their results on the stack. The stack organization can be customized to user needs.

 

Customize your stacks

First you can change the number of elements the stack can hold. Use the STV. - command for this.

 

Any stack can be organized as a stack (LIFO - Last-In-First-Out) or as a que (First-In-First-Out). The default Stack setting is LIFO. Use the STS. - command to switch the stack between LIFO and FIFO. Note that if a stack gets to many elements, some overflowing elements are automatically deleted. These overflowing elements are generally the oldest elements. In a LIFO stack the oldest element is the first element added, while in FIFO (Que), the last added elements added ist the "oldest".

 

Another setting is, if t he stack element is remove if you touch it. Normally a stack element it deleted immediately when you touch it. This way you get all stack elements one-by-one, just by using $tos$ which is always the TOS (Top of Stack) in the actual selected stack. If you want to get the TOS without having it removed, use $dtos$.

 

What if i jump into a subprogram that also uses the stack?

You can anytime take the whole stack-content and put it into a variable, using the STV.-command. When you return from your subprogram, you can "restore" the complete stack from that variable using the STV.-command.

 

Use the script local stack

If you use Subscripts (scripts in separate files) you can take advantage of another feature.

Local and Global stacks. In fact, we have two stacks, a script-local stack and a global stack. The global stack is valid through the whole script execution. The local stack holds the same rules like the script local variables.

 

If a script calls another Subscript in another file, using the JNF. command. At this time the content of its local stack is preserved and automatically restored on return from the subscript. Thats the use of a local stack. To hold your script local data.

 

However, the Local stack is cleared when the Script returns upwards to a Main-Script, from which it was called, using JNF./RTS. Its the same sytem like when using script local variables.

 

How do I select the actual stack/que?

To select which stack you want to work with, use the STS.GLOB or STS.LOCA command.

These will make the selected stack to the actual stack.

 

If you want to know which stack is currently the actual stack, use the #stack# systemvariable. It will return a number that shows you if the Local or the Global stack is currently selected as the actual stack.

#stack# -> 1  GLOBAL Stack is selected

#stack# -> 2  LOCAL Stack is selected

 

 

There are shortcuts to accessing the stack values inside your script.

 

§§000 to §§999

for  directly accessing the local stack

 

$$000 to $$999

for directly accessing the global stack

 

§§000 - Top of Stack (TOS) local stack

$$000 - Top of Stack (TOS) global stack

 

The global stack keeps his content anywhere in a script or subscript.

 

There are different Methods to access the stack. First you can use the Stack-variables above to directly access the first 999 stack elements. Secondly you can use multiple stack-commands.

 

 

    PUS. - Push Parameter onto Stack/Que

    PUV. PUsh variables on the User-Stack

    POP. - POP Variable from Stack

    POV. - POp Variables from User-Stack

    STJ. - STack Job

    STS. - Set STack Settings

    STV. - Stack To/from Variable

 

 

Most Commands may return results directly into stack-variables if you give these for the result.

 

 

Example

 

' Choose LOCAL Stack as Actual Stack

STS.LOCA

' Organize Stack as First-in-First-Out

STS.FIFO

 

' Put 10 Items on actual Stack

FOR.$$A01|1|10

 VAR.$$B00=Stackplace $$A01

 PUV.$$B00

NEX.

 

' Show all Items on Local Stack

DMP.5

MBX.Ready

ENR.

 

 

Remarks

The $$000 and §§000 refer to the TOS (Top of stack). See several example scripts in the examples folder.

 

 

Limitations:

Per default the maximum stack size limit is set to 25 Elements. This can be changed using the STS.SIZE command.

How long can stack-entry get? Can you strore a file in it?

What is the maximum size? 100 MB ? 250, 500 MB ?

It depends on the amount of unfragmented Memory that the robot can get from the OS.

Under normal circumstances any stack entry can always exceed a size of 50 or 100 MB (Megabyte) without problems. Once you clear the stack entry, in any way, the memory is safely returned to the OS. Of course this happens automatically when the script ends.

 

 

 

See also:

 

STJ. - STack Job

STS. - Set STack Settings

STV. - Stack To/from Variable

PUS. - Push Parameter onto Stack/Que

PUV. PUsh variables on the User-Stack

POP. - POP Variable from Stack

POV. - POp Variables from User-Stack