Specifications
CAVR-4
96
Calling assembler routines from C
AVR® IAR C/C++ Compiler
Reference Guide
Calling assembler routines from C
An assembler routine that is to be called from C must:
● Conform to the calling convention
● Have a PUBLIC entry-point label
● Be declared as external before any call, to allow type checking and optional
promotion of parameters, as in the following examples:
extern int foo(void);
or
extern int foo(int i, int j);
One way of fulfilling these requirements is to create skeleton code in C, compile it, and
study the assembler list file.
CREATING SKELETON CODE
The recommended way to create an assembler language routine with the correct
interface is to start with an assembler language source file created by the C compiler.
Note that you must create skeleton code for each function prototype.
The following example shows how to create skeleton code to which you can easily add
the functional body of the routine. The skeleton source code only needs to declare the
variables required and perform simple accesses to them. In this example, the assembler
routine takes an
int and a double, and then returns an int:
extern int gInt;
extern double gDouble;
int func(int arg1, double arg2)
{
int locInt = arg1;
gInt = arg1;
gDouble = arg2;
return locInt;
}
int main()
{
int locInt = gInt;
gInt = func(locInt, gDouble);
return 0;
}