FPSLIC
2
Passing Variables between C and
Assembly Code Functions
When the IAR C-compiler is used for the FPSLIC, the reg-
ister file is segmented as shown in Figure 1.
Scratch registers are not preserved across function calls.
Local registers are preserved across function calls. The Y
register (R28:R29) is used as a data stack pointer to
SRAM. The scratch registers are used for passing parame-
ters and return values between functions.
When a function is called, the parameters to be passed to
the function are placed in the register file (registers
R16 - R23). When a function is returning a value, this value
is placed in the register file (registers R16 - R19), depend-
ing on the size of the parameters and the returned value.
Table 2 shows example placement of parameters when
calling a function.
Figure 1. Segments in the Register File
For a complete reference of the supported data types and
corresponding sizes, see the AVR® IAR Compiler
Reference Guide from IAR Systems, Data Representation
section.
Example C function call:
int get_port(unsigned char temp, int num)
When calling this C function, the 1-byte parameter temp is
placed in R16 and the 2-byte parameter num is placed in
R20:R21. The function returns a 2-byte value, which is
placed in R16:R17 after return from the function.
If a function is called with more than two parameters, the
first two parameters are passed to the function as shown
above. The remaining parameters are passed to the func-
tion on the data stack. If a function is called with a struct or
union as a parameter, a pointer to the structure is passed
on to the function on the data stack.
If a function needs to use any local registers, it first pushes
the registers on the data stack. Then the return value from
the function is placed at addresses R16 - R19, depending
on the size of the returned value.
Scratch Register R0 - R3
Local Register R4 - R15
Scratch Register R16 - R23
Local Register R24 - R27
Data Stack Pointers (Y) R28 - R29
Scratch Register R30 - R31
Table 2. Placement and Parameters of C Functions
Function Parameter 1 Registers Parameter 2 Registers
func (char, char) R16 R20
func (char, int) R16 R20, R21
func (int, long) R16, R17 R20, R21, R22, R23
func (long, long) R16, R17, R18, R19 R20, R21, R22, R23