Overview of stack-oriented programming – Echelon Neuron User Manual
Page 46

Overview of Stack-Oriented Programming
Neuron assembly language is a stack-oriented programming language, which
means that the data stack is the primary tool for managing data manipulation,
rather than using other tools, such as processor registers. The Neuron
instruction set provides a rich set of stack-oriented instructions, rather than
register-oriented instructions.
One of the major features of a stack-oriented language is its use of postfix
notation (also known as Reverse Polish notation) for arithmetic and logical
operations. This notation is well suited for working with a stack. Arithmetic and
logical operations consist of numeric values and arithmetic or logical operators
(for addition, subtraction, AND, OR, and so on). In postfix notation, the
operators follow the operands.
To add three and four in postfix notation, you write “3 4 +”; “3” and “4” are the
operands, and “+” is the operator. For more conventional infix notation, you add
three and four by writing “3 + 4”. For a stack-oriented language such as
Neuron assembly language, you push the values “3” and “4” (the operands) onto
the stack, and then use the ADD instruction (the operator) to add the two
numbers together. The arithmetic operations consume the operand values on the
stack and place the results of arithmetic operations onto the stack.
In postfix arithmetic, the affinity of operands to their operator is defined by the
order in which the operands and operators appear. Parentheses are not required
for postfix arithmetic.
Example: To perform the arithmetic operation that is conventionally written as
“3 − 4 + 5” in postfix notation, you write “3 4 − 5 +”. This operation is
expressed in Neuron assembly language as:
PUSH #3
PUSH #4
SUB NEXT,TOS
PUSH #5
ADD
This code performs the following tasks:
1. The first two PUSH instructions place the values 3 and 4 onto the stack.
2. The SUB NEXT,TOS instruction consumes the values 3 and 4, and
places the result of the subtraction (-1) on the top of the stack. The
operands TOS and NEXT refer to the top of the stack and the next
element below the top of the stack, respectively.
3. The next PUSH instruction places the value 5 on the stack so that the
ADD instruction can add the two values (-1 and 5) together.
4. The ADD instruction consumes the values -1 and 5, and places the result
of the addition (4) on the top of the stack. Note that because the addition
operands are on the stack, the ADD instruction itself has no operands or
parameters (that is, it uses implicit addressing).
The number signs or hashes for the PUSH instructions tell the assembler that
the values are immediate, literal, values, rather than addresses (the instructions
use immediate addressing). Because of the small data values, this example could
have used the PUSHS instruction, rather than the PUSH instruction, to produce
36
Writing a Neuron Assembly Utility Function