Tier 3
Custom procedures, Arguments and Variables
If you wish
to make advanced scripts you must read the text below.
These things have the very same purpose like in C. But the difference is
that you need to know how to prepare the stuff in order to get them to work.
Let's begin from custom procedures. This can be any piece of your script, you don't
need to declare anything. However it's good to mark it with useful label.
To call your procedure use the very same Goto command preceeded by new Save command.
The Save command pushes the current position in script to retrieve it later with Return command.
The arguments and return code are carried the very same way like Exec instruction, so the code:
- Push 2
- Push 7
- Save
- Goto bizkit
- AddSP 2
- Return
will call the routine "bizkit" with two arguments, When the routine done the job
execution of the script is continued from next instruction - "AddSP 1" in our example, then the "Return" command should stop
the script (note that it will only stop when the stack is empty which means there is no address we may return to).
Let's say that our "bizkit" procedure will count a power of a number.
How would the actual routine look like? Before we will write it we must get to know of how to
access function arguments within the routine. There are two special commands provided for this purpose.
They are:
- PushArg - pushes routine argument on stack,
- PopArg - pops routine argument from stack.
Do not mix PopArg up with providing arguments for routine.
It's used aswell as the PushArg to read/write arguments within a routine.
Index the arguments from 1 (not from 0). Remember also that EMC stack stores 16bit values only.
There is no support for bigger numbers. OK, so how our route would it like then? Here's example implementation:
- PushArg 2
- Goto 5
- PushArg 2
- Multiply
- PushArg 1
- Push 1
- Subtract
- PopArg 1
- PushArg 1
- False
- IfNotGo 3
- PopRC
- Return
Things begin to look messy here. But this function works. Don't try to call this routine with a power lower or equal to zero.
As an exercise you can try to modify it to handle such arguments. Another fine tool that make use of stack space are local variables.
To use local variables first you must make space on stack using new SubSP instruction (as opposite to AddSP)
Call SubSP always as your first command in a sub-routine
or you'll encounter problems with accessing your variables. The stack space is also called as stack frame. Remember
to retrieve stack space with AddSP on the end of the routine.
Using the variables is similiar to routine arguments:
- PushVar - pushes variable on stack,
- PopVar - pops variable from stack.
And one last note: remember to use the stack with care becasue very intensive use of it
will result in stack overflow. Needless to say avoid overflow at all costs or unexpected things may happen. As a tip keep in mind that
the actual stack size is only 16 values. Hurrah! You reached the end. I suggest that you look at Appendice also because it
will give you the best start to make the working changes to script.