Atmel CAVR-4 Manual De Usuario

Descargar
Página de 323
CAVR-4
Part1. Using the compiler
Assembler language interface
95
INLINE ASSEMBLER 
It is possible to insert assembler code directly into a C or C++ function. The 
asm
 
keyword assembles and inserts the supplied assembler statement, or statements, in-line. 
The following example shows how to use inline assembler to insert assembler 
instructions directly in the C source code. This example also shows the risks of using 
inline assembler.
bool flag;
void foo()
{
  while (!flag)
  {
    asm("IN R0,PIND \n
          STS flag,R0");
  }
}
In this example, the assignment of 
flag
 is not noticed by the compiler, which means the 
surrounding code cannot be expected to rely on the inline assembler statement.
The inline assembler instruction will simply be inserted at the given location in the 
program flow. The consequences or side-effects the insertion may have on the 
surrounding code have not been taken into consideration. If, for example, registers or 
memory locations are altered, they may have to be restored within the sequence of inline 
assembler instructions for the rest of the code to work properly.
Inline assembler sequences have no well-defined interface with the surrounding code 
generated from your C or C++ code. This makes the inline assembler code fragile, and 
will possibly also become a maintenance problem if you upgrade the compiler in the 
future.
 In addition, there are several limitations to using inline assembler:
The compiler’s various optimizations will disregard any effects of the inline 
sequences, which will not be optimized at all
In general, assembler directives will cause errors or have no meaning. Data 
definition directives will work as expected
Auto variables cannot be accessed.
Inline assembler is therefore often best avoided. If there is no suitable intrinsic function 
available, we recommend the use of modules written in assembler language instead of 
inline assembler, because the function call to an assembler routine normally causes less 
performance reduction.
For reference information about the 
asm