Microchip Technology SW006022-2N Data Sheet

Page of 338
Functions
 2012 Microchip Technology Inc.
DS52071B-page 161
10.8
FUNCTION CALL CONVENTIONS
When calling a function:
• Registers W0-W7 are caller saved. The calling function must preserve these val-
ues before the function call if their value is required subsequently from the func-
tion call. The stack is a good place to preserve these values.
• Registers W8-W14 are callee saved. The function being called must save any of 
these registers it will modify.
• Registers W0-W4 are used for function return values.
• Registers W0-W7 are used for argument transmission.
• DBRPAG/PSVPAG should be preserved if the -mconst-in-code (auto_psv) 
memory model is being used.
TABLE 10-2:
REGISTERS REQUIRED 
Parameters are placed in the first aligned contiguous register(s) that are available. The 
calling function must preserve the parameters, if required. Structures do not have any 
alignment restrictions; a structure parameter will occupy registers if there are enough 
registers to hold the entire structure. Function results are stored in consecutive 
registers, beginning with W0.
10.8.1
Function Parameters
The first eight working registers (W0-W7) are used for function parameters.Parameters 
are allocated to registers in left-to-right order, and a parameter is assigned to the first 
available register that is suitably aligned.
In the following example, all parameters are passed in registers, although not in the 
order that they appear in the declaration. This format allows the compiler to make the 
most efficient use of the available parameter registers.
EXAMPLE 10-1: 
FUNCTION CALL MODEL
void
params0(short p0, long p1, int p2, char p3, float p4, void *p5)
{
        /*
        ** W0           p0
        ** W1           p2
        ** W3:W2        p1
        ** W4           p3
        ** W5           p5
        ** W7:W6        p4
        */
        ...
Data Type
Number of Registers Required
char
1
int
1
short
1
pointer
1
long
2 (contiguous – aligned to even numbered register)
float
2 (contiguous – aligned to even numbered register)
double
*
2 (contiguous – aligned to even numbered register)
long double
4 (contiguous – aligned to quad numbered register)
structure
1 register per 2 bytes in structure
* double is equivalent to long double if -fno-short-double is used.