Designing a neuron assembly function – Echelon Neuron User Manual
Page 47

more compact code; see PUSHS (Push Short) for a description of the PUSHS
instruction.
Note that the stacks and all data registers are 8 bits wide. Operations on larger
scalars typically require the combination of more than one instruction. Chapter
8, System-Provided Functions, describes system functions that can be used to
accomplish many common tasks, including arithmetic with 16-bit operands.
Designing a Neuron Assembly Function
A general approach to writing Neuron Assembly functions is to push arguments
for a called function onto the stack, call the function, push more arguments, call
another function, and so on, and then finally, after the last operation, clean up
the stack frame. This approach, however, can lead to suboptimal code and larger
than necessary stack frames. For more efficient Neuron assembly programming,
it often helps to look ahead into the remainder of the algorithm, and to get the
stack layout correct as soon as possible.
Optimized assembly functions can often consume stack arguments rather than
work with copies of the function arguments (which the caller would later dispose
of).
Note that different rules might apply when interfacing Neuron C functions; see
Chapter 4, Interfacing with a Neuron C Application, for more information.
One way to create a correct stack layout that is suited for postfix arithmetic is to
create a prefix formula, and then read that formula in reverse to generate a
postfix formula that is appropriate for creating the stack frames. This technique
generally applies to mathematical problems, but also applies to other types of
problems, and is often easier for programmers who not used to postfix notation.
Generate a prefix form of the algorithm, using function names for the operators.
For example, use “add(A,B)” for the more conventional infix form “(A + B)”.
Then, read the expression from right-to-left to generate the postfix form
“(B,A,add)”. From the postfix form, you can generate the necessary stack frames:
push A, push B, add.
Example:
For this extended example, assume that you need to write a function that
computes a circular area, given a radius R. That is, the function has the
following characteristics:
•
Input: radius, R
•
Output: area, πR
2
Assume that this function will be part of a mathematics library that you plan to
write in Neuron assembly language.
The first decision to make is whether the function will be called from Neuron C
applications or only from other Neuron assembly functions. For this example,
the function will be called from Neuron C applications.
If you name the function “carea”, its function prototype in C is:
// carea(radius) returns pi * radius^2
unsigned long carea(unsigned long radius);
The following tools are available to help you write this function:
Neuron Assembly Language Reference
37