3.11.2 #asm, #endasm and asm()
PIC instructions may also be directly embedded “in-line” into C code using the directives #asm,
#endasm or the statement asm().
The #asm and #endasm directives are used to start and end a block of assembly instructions which
are to be embedded into the assembly output of the code generator. The #asm and #endasm construct
is not syntactically part of the C program, and thus it does not obey normal C flow-of-control rules,
however you can easily include multiple instructions with this form of in-line assembly.
The asm() statement is used to embed a single assembler instruction. This form looks and behaves
like a C statement, however each instruction must be encapsulated within an asm() statement.
•
You should not use a #asm block within any C constructs such as if, while, do etc.
In these cases, use only the asm("") form, which is a C statement and 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
63
Mixing C and Assembler Code C Language Features
rlf _var
rlf _var+1
#endasm
// or like this
asm("bcf 0,3");
asm("rlf _var");
asm("rlf _var+1");
}
When using in-line assembler code, great care must be taken to avoid interacting with compilergenerated
code. The code generator cannot scan the assembler code for register usage and so will
remain unaware if registers are clobberred or used by the code. If in doubt, compile your program
with the PICC -S option and examine the assembler code generated by the compiler.
3.11.3 Accessing C objects from within Assembly Code
The following applies regardless of whether the assembly is part of a separate assembly module, or
in-line with C code.
For any non-local assembly symbol, the GLOBAL directive must be used to link in with the symbol
if it was defined elsewhere. If it is a local symbol, then it may be used immediately.
Partager