Tier 1
Stack and Math operations
This is very basic term you need to know of to start the adventure with low-level EMC.
Don't worry if you don't understand it the first time. You can also
learn about it in various programming guidebooks. If you're familiar with
it you can omit this section.
Stack is organisation of data that accepts two basic types of operations:
- Push value on stack
- Pop value from stack
The first one is used to store given value on top of the stack. Succesive
values will be stored always on top of the stack. The second one removes
element from top of the stack and retrieves its value.
The basic rule of stack is: what you pushed the first will be popped as the last also known as FILO.
The stack is used basically to remember the place in the script and retrieve
it later (calling sub-routines) and also in calculating math expressions.
Additionally stack provides some space for data which can be used as
a space for function arguments or local variables.
I guess that this sounds terrible but be patient. I will try to describe
these terms one by one across the guide.
There are several commands to work on stack in EMC, but let's start from
most simple:
- Push - this one pushes 16bit signed value on stack,
- Eval - performs math or logic operation on two values on stack,
- False, Neg, Not - performs negation on value on stack, appropriately:
boolean, arithmetic and bitwise.
With these commands you can calculate almost everything. The types of
math operations are add, subtract, multiply and divide.
Note that the Eval will pop two values from the stack,
perform an action, then push the result. The False, Neg, Not
works on one value only.
This part of script adds two numbers: 450 and 125.
Note that I use operation name instead of Eval in this guide.
- Push 450
- Push 125
- Add
This one calculates more complicated phrase: (60 * 5) / (10 + 5)
- Push 60
- Push 5
- Multiply
- Push 10
- Push 5
- Add
- Divide
In this example division is performed on results of previous operations.
Note that you must provide arguments in reverse order because of the FILO rule (add, multiply
are simetric).
Simply? If so you can proceed to next section. If not then don't worry.
This guide is too small to provide all important informations. You could refer to any
programming book or wait for high-level compiler for EMC...