Echelon Neuron User Manual
Page 63

4. Call the function.
5. Process optional function results and clear the stack frame.
6. Restore general-purpose registers saved.
Example:
The following example shows how to set up and call a diagnosis function that is
defined in Neuron C.
The Neuron C definition for this function is:
void diagnosis(unsigned currentValue,
unsigned remainingSize,
const char* remainingData){
...
}
The following Neuron assembly language code shows how to call this Neuron C
function:
pData EQU 0
IFDEF _DEBUG
IMPORT %diagnosis ; (cs, size, pData -- )
... ; (cs) R(size)
pushd [pdata] ; (pData(2), cs) R(size)
pushd [pData] ; (pData(2), pData(2), cs) R(size)
push [rsp] ; (size, pData(2), pData(2), cs) R(size)
push [dsp][-4] ; (cs, size, pData(2), pData(2), cs) R(size)
callf %diagnosis ; (pData(2), cs) R(size)
popd [pData] ; (cs) R(size)
ENDIF
Thus, the Neuron assembly code performs the general steps for calling the
Neuron C function:
1. Import the diagnosis symbol (IMPORT %diagnosis).
2. Preserve all general-purpose registers in use (pushd [pData]).
3. Push the function arguments onto the data stack, from right to left
(pushd [pData], push [rsp], push [dsp][-4]).
4. Call the diagnosis function (callf %diagnosis).
5. Process optional function results and clear the stack frame. This example
does not show processing for function results.
6. Restore general-purpose registers saved (popd [pData]).
This implementation uses conditional assembly based on a predefined symbol
_DEBUG. Using conditional assembly allows this function to be called only
within debug builds. However, conditional assembly is not available with
Neuron C programs; you must create a Neuron assembly code library and link
the library with the Neuron C program.
The assembly function pushes the pointer register P0 on to the stack twice
(pushd [pData]): once to save the register content across the call to the
diagnosis function, and a second time to provide the current pointer value to the
diagnosis function itself. An alternative implementation could push the pointer
once (pushd [pData]), then duplicate the value on the stack using two push
Neuron Assembly Language Reference
53