4.7.6 Function Parameters

MPLAB XC8 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 value that is passed to the function and a parameter is the variable defined by the function to store the argument.

Arguments are passed to functions via registers and consume as many register as required to hold the object. However, registers are allocated in pairs, thus there will be at least two registers allocated to each argument, even if the argument is a single byte. This make more efficient use of the AVR instruction set.

The first register pair available is r24-r25 and lower register pairs are considered after that down to register r8. For example, if a function with the prototype:

int map(unsigned long a, char b);

is called, the argument for the four-byte parameter a will be passed in registers r22 thru r25, with r22 holding the least significant byte and r25 holding the most significant byte; and the argument for parameter b will be assigned to registers r20 and r21.

If there are further arguments to pass once all the available registers have been assigned, they are passed on the stack.

Arguments to functions with variable argument lists (printf() etc.) are all passed on stack.