16.6 Function Parameters
MPLAB XC uses a fixed convention to pass arguments to a function. The method used to pass the arguments depends on the size and number of -arguments involved.
Note: The names “argument” and “parameter” are often used interchangeably, but typically an
argument is the actual value that is passed to the function and a parameter is the
variable defined by the function to store the argument.
The Stack Pointer is always aligned on an 8-byte boundary.
- All integer types smaller than a
32-bit integer are first converted to a 32-bit value. The first four 32 bits of
arguments are passed via registers
a0
-a3
(see the table below for how many registers are required for each data type). - Although some arguments may be passed in registers, space is still allocated on the stack for all arguments to be passed to a function (see the figure below). Application code should not assume that the current argument value is on the stack, even when space is allocated.
- When calling a function:
- Registers
a0
-a3
are used for passing arguments to functions. Values in these registers are not preserved across function calls. - Registers
t0
-t7
andt8
-t9
are caller saved registers. The calling function must push these values onto the stack for the registers’ values to be saved. - Registers
s0
-s7
are called saved registers. The function being called must save any of these registers it modifies. - Register
s8
is a saved register if the optimizer eliminates its use as the Frame Pointer.s8
is a reserved register otherwise. - Register
ra
contains the return address of a function call.
- Registers
Data Type | Number of Registers Required |
---|---|
char |
1 |
short |
1 |
int |
1 |
long |
1 |
long long |
2 |
float |
1 |
double |
1 |
long double |
2 |
structure |
Up to 4, depending on the size of the struct. |