Microchip Technology SW006021-2 Manuale Utente
C Language Features
2012 Microchip Technology Inc.
DS52053B-page 203
You may use the asm() form of inline assembly at any point in the C source code as it
will correctly interact with all C flow-of-control structures.
will correctly interact with all C flow-of-control structures.
The following example shows both methods used:
unsigned int var;
void main(void)
{
var = 1;
#asm // like this...
BCF 0,3
BANKSEL(_var)
RLF (_var)&07fh
RLF (_var+1)&07fh
#endasm
// do it again the other way...
asm(“BCF 0,3”);
asm(“BANKSEL _var”);
asm(“RLF (_var)&07fh”);
asm(“RLF (_var+1)&07fh”);
}
inline assembly code is never optimized by the assembler optimizer.
When using inline assembler code, great care must be taken to avoid interacting with
compiler-generated code. The code generator cannot scan the assembler code for reg-
ister usage and so will remain unaware if registers are clobbered or used by the assem-
bly code.
compiler-generated code. The code generator cannot scan the assembler code for reg-
ister usage and so will remain unaware if registers are clobbered or used by the assem-
bly code.
The registers used by the compiler are explained in Section 5.7 “Register Usage”. If
you are in doubt as to which registers are being used in surrounding code, compile your
program with the --ASMLIST option (see Section 4.8.16 “--ADDRQUAL: Set Com-
piler Response to Memory Qualifiers”) and examine the assembler code generated
by the compiler. Remember that as the rest of the program changes, the registers and
code strategy used by the compiler will change as well.
you are in doubt as to which registers are being used in surrounding code, compile your
program with the --ASMLIST option (see Section 4.8.16 “--ADDRQUAL: Set Com-
piler Response to Memory Qualifiers”) and examine the assembler code generated
by the compiler. Remember that as the rest of the program changes, the registers and
code strategy used by the compiler will change as well.
If a C function is called from main-line and interrupt code, it may be duplicated, see
Section 5.9.5 “Function Duplication”. Although a special prefix is used to ensure
that labels generated by the compiler are not duplicated, this does not apply to labels
defined in hand-written, inline assembly code in C functions. Thus, you should not
define assembly labels in inline assembly if the containing function might be duplicated.
Section 5.9.5 “Function Duplication”. Although a special prefix is used to ensure
that labels generated by the compiler are not duplicated, this does not apply to labels
defined in hand-written, inline assembly code in C functions. Thus, you should not
define assembly labels in inline assembly if the containing function might be duplicated.
5.12.3
Interaction Between Assembly and C Code
MPLAB XC8 C Compiler incorporates several features designed to allow C code to
obey requirements of user-defined assembly code. There are also precautions that
must be followed to ensure that assembly code does not interfere with the assembly
generated from C code.
obey requirements of user-defined assembly code. There are also precautions that
must be followed to ensure that assembly code does not interfere with the assembly
generated from C code.
The command-line driver ensures that all user-defined assembly files have been pro-
cessed first, before compilation of C source files begin. The driver is able to read and
analyze certain information in the relocatable object files and pass this information to
the code generator. This information is used to ensure the code generator takes into
account requirement of the assembly code. See Section 4.3.4 “Compilation of
Assembly Source” for further information on the compile sequence.
cessed first, before compilation of C source files begin. The driver is able to read and
analyze certain information in the relocatable object files and pass this information to
the code generator. This information is used to ensure the code generator takes into
account requirement of the assembly code. See Section 4.3.4 “Compilation of
Assembly Source” for further information on the compile sequence.