SLAU132V October 2004 – February 2020
A called function (child function) must perform the following tasks:
size of all local variables + max = constant
The max argument specifies the size of all parameters placed in the argument block for each call.
Structures and unions with size 32 bits or less are passed by value, either in registers or on the stack. Structures and unions larger than 32 bits are passed by reference.
In this way, the caller can be smart about telling the called function where to return the structure. For example, in the statement s = func(x), where s is a structure and f is a function that returns a structure, the caller can simply pass the address of s as the first argument and call f. The function f then copies the return structure directly into s, performing the assignment automatically.
You must be careful to properly declare functions that return structures, both at the point where they are called (so the caller properly sets up the first argument) and at the point where they are declared (so the function knows to copy the result).
The following example is typical of how a called function responds to a call:
func: ; Called function entry point
PUSH.W r10
PUSH.W r9 ; Save SOE registers
SUB.W #2,SP ; Allocate the frame
:
: ; Body of function
:
ADD.W #2,SP ; Deallocate the frame
POP r9 ; Restore SOE registers
POP r10
RET ; Return