Table of Contents
Previous topic Next topic
C/C++ COMPILER->In-Line Assembler Code->Introducing In-Line Assembler Code
Assembler code can be embedded into your C source code in two ways:
The pre-processor directives #asm and #endasm allow assembler code to be included anywhere within the C source code file, the only restriction being that it cannot be positioned within an expression. All lines between #asm and #endasm are passed straight through unmodified to the intermediate file processed by the assembler and so all of the rules for the cross assembler source code are supported.
- using the #asm/#endasm preprocessor directives
- using the _asm keyword
The pre-processor directives #if, #ifdef, #ifndef, #else, #elif and #endif are valid between #asm and #endasm and so can be used to maintain the assembler code if required.
The _asm keyword can only be used within functions. It is used with following syntax:
The string constant is passed straight through unmodified as a single line to the intermediate file processed by the assembler. Each <string constant> should therefore be a valid line of assembler code.
The advantage of the _asm syntax is that it is subject to token replacement by the C preprocessor. Therefore the statement can be generated by a series of macros.
The following example shows how to use both forms of in-line assembler:
Initialise equ 0
#define TRAP(n) _asm( " trap #" #n )
#define SETUP() TRAP(Initialise)
Also with the _asm syntax, the compiler supports a special construct to enable easy access to C variables. If the variable name is placed in the string constant within curly braces, the compiler replaces the variable name (and the curly braces) with the appropriate substring depending upon the location of the variable. See the following sections for more details.
The compiler generates upper case mnemonics and so if lower case is chosen for the in-line assembler mnemonics they can be clearly distinguished from the compiler generated code in the list file.