Atmel CAVR-4 Manual De Usuario

Descargar
Página de 323
CAVR-4
94
Mixing C and assembler
AVR® IAR C/C++ Compiler
Reference Guide
The advantage of an intrinsic function compared to using inline assembler is that the 
compiler has all necessary information to interface the sequence properly with register 
allocation and variables. The compiler also knows how to optimize functions with such 
sequences; something the compiler is unable to do with inline assembler sequences. The 
result is, that you get the desired sequence properly integrated in your code, and that the 
compiler can optimize the result.
For detailed information about the available intrinsic functions, see the chapter Intrinsic 
functions
.
MIXING C AND ASSEMBLER MODULES
When an application is written partly in assembler language and partly in C or C++, you 
are faced with a number of questions:
How should the assembler code be written so that it can be called from C?
Where does the assembler code find its parameters, and how is the return value 
passed back to the caller?
How should assembler code call functions written in C?
How are global C variables accessed from code written in assembler language?
Why does not the debugger display the call stack when assembler code is being 
debugged?
The first issue is discussed in this section. The following two are covered in the section 
Calling convention, page 99.
The answer to the final question is that the call stack can be displayed when you run 
assembler code in the debugger. However, the debugger requires information about the 
call frame, which must be supplied as annotations in the assembler source file. For more 
information, see Call frame information, page 108.
It is possible to write parts of your application in assembler and mix them with your C 
or C++ modules. There are several benefits with this:
The function call mechanism is well-defined
The code will be easy to read
The optimizer can work with the C or C++ functions.
There will be some overhead in the form of a function call and return instruction 
sequences, and the compiler will regard some registers as scratch registers. However, the 
compiler will also assume that all scratch registers are destroyed by an inline assembler 
instruction. In many cases, the overhead of the extra instructions is compensated by the 
work of the optimizer.
The recommended method for mixing C or C++ and assembler modules is described in 
Calling assembler routines from C, page 96, and Calling assembler routines from C++
page 98, respecti
vely.