Atmel CAVR-4 Manual De Usuario
![Atmel](https://files.manualsbrain.com/attachments/0369829915bda09f9c2e00fb805a7753579683b5/common/fit/150/50/8d2bf08978ec3e5bc63f4343ac5e91ce8d0e40045619fa520d910d64af8f/brand_logo.png)
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.
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.
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.
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
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
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.
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
keyword, see asm, __asm, page 205.