The compiler partitions the MSP430 general purpose registers into two sets.

The registers in the first set, R12 through R15, are used for parameter passing and returning function results and are not preserved across functions calls.

The registers in the second set, R4 through R11, are used for register-allocated variables, working storage, and temporary results and must be preserved across function calls.

Parameter passing

The compiler uses the scratch registers to pass values to the called routine for all parameters of simple data type. If there are not enough scratch registers to hold all parameter data to be passed to the called routine, the excess data are passed on the stack.

Simple data types which require more than a single word of storage are passed in register pairs or register quads. The register requirement for the basic data types are:

Allocation of the scratch registers for function calls proceeds in a left-to-right fashion, starting with register R15 and progressing in reverse order to R12. The compiler tries to fit each parameter into the scratch registers and, if it can, allocates those registers to the incoming parameter. If the parameter requires more scratch registers than are free, it is noted and is passed on the stack. All parameters which are passed on the stack are pushed in reverse order.

Function return values

The compiler uses the scratch registers to return values to the caller.

Examples

This section contains some examples of the calling convention in use.

Example #1
void fun1(int u, int v); 

Reading from left to right, the parameter u is passed in register R15 and v is passed in R14. The scratch registers R12 and R13 are not used to pass parameters and can be used in fun1 without needing to be preserved.

Example #2
void fun1(int u, long v, int w); 

The parameter u is passed in register R15. Because v requires two registers to hold its value it is passed in the register pair R14R13 with R14 holding the high part of v and R13 the low part. The final parameter w is passed in R12.

Example #3
void fun1(int u, long v, int w, int x); 

The parameter u is passed in register R15. Because v requires two registers to hold its value it is passed in the register pair R14R13 with R14 holding the high part of v and R13 the low part. The final parameter w is passed in R12. As all scratch registers are now used, x is passed by placing it on the stack.

Example #4
void fun1(int u, long v, long w); 

The parameter u is passed in register R15. Because v requires two registers to hold its value it is passed in the register pair R14R13 with R14 holding the high part of v and R13 the low part. When considering w, there is only one free scratch register left, which is R12. The compiler cannot fit w into a single register and therefore places the argument onto the stack—the compiler does not split the value into two and pass half in a register and half on the stack.

Example #5
void fun1(int u, long v, long w, int x, int y); 

The parameter u is passed in register R15. Because v requires two registers to hold its value it is passed in the register pair R14R13 with R14 holding the high part of v and R13 the low part. When considering w, there is only one free scratch register left, which is R12. The compiler cannot fit w into a single register and therefore places the argument onto the stack—the compiler does not split the value into two and pass half in a register and half on the stack.

When considering x the compiler sees that R12 is unused and so passes x in R12. All scratch registers are used when considering y, so the argument is placed onto the stack. The parameters w and x are pushed onto the stack before the call and are pushed in reverse order, with y pushed before w.

This example shows two parameters, w and y, that are passed to the called routine on the stack, but they are separated by a parameter x that is passed in a register.