Техническая Спецификация для Microchip Technology SW006023-1N
MPLAB
®
XC32 C/C++ COMPILER
USER’S GUIDE
2012 Microchip Technology Inc.
DS51686E-page 161
Chapter 14. Mixing C/C++ and Assembly Language
14.1
INTRODUCTION
Assembly language code can be mixed with C/C++ code using two different tech-
niques: writing assembly code and placing it into a separate assembler module, or
including it as in-line assembly in a C/C++ module.This section describes how to use
assembly language and C/C++ modules together. It gives examples of using C/C++
variables and functions in assembly code, and examples of using assembly language
variables and functions in C/C++.
niques: writing assembly code and placing it into a separate assembler module, or
including it as in-line assembly in a C/C++ module.This section describes how to use
assembly language and C/C++ modules together. It gives examples of using C/C++
variables and functions in assembly code, and examples of using assembly language
variables and functions in C/C++.
The more assembly code a project contains, the more difficult and time consuming its
maintenance will be. As the project is developed, the compiler may work in different
ways as some optimizations look at the entire program. The assembly code is more
likely to fail if the compiler is updated due to differences in the way the updated compiler
may work. These factors do not affect code written in C/C++
maintenance will be. As the project is developed, the compiler may work in different
ways as some optimizations look at the entire program. The assembly code is more
likely to fail if the compiler is updated due to differences in the way the updated compiler
may work. These factors do not affect code written in C/C++
14.2
USING INLINE ASSEMBLY LANGUAGE
Within a C/C++ function, the asm statement may be used to insert a line of assembly
language code into the assembly language that the compiler generates. Inline
assembly has two forms: simple and extended.
language code into the assembly language that the compiler generates. Inline
assembly has two forms: simple and extended.
In the simple form, the assembler instruction is written using the syntax:
asm ("instruction");
where instruction is a valid assembly-language construct. If you are writing inline
assembly in ANSI C programs, write __asm__ instead of asm.
assembly in ANSI C programs, write __asm__ instead of asm.
In an extended assembler instruction using asm, the operands of the instruction are
specified using C/C++ expressions. The extended syntax is:
specified using C/C++ expressions. The extended syntax is:
asm("template" [ : [ "constraint"(output-operand) [ , ... ] ]
[ : [ "constraint"(input-operand) [ , ... ] ]
[ "clobber" [ , ... ] ]
]
]);
You must specify an assembler instruction template, plus an operand constraint
string for each operand. The template specifies the instruction mnemonic, and
optionally placeholders for the operands. The constraint strings specify operand
constraints, for example, that an operand must be in a register (the usual case), or that
an operand must be an immediate value.
string for each operand. The template specifies the instruction mnemonic, and
optionally placeholders for the operands. The constraint strings specify operand
constraints, for example, that an operand must be in a register (the usual case), or that
an operand must be an immediate value.
Note:
If assembly must be added, it is preferable to write this as self-contained
routine in a separate assembly module rather than in-lining it in C code.
routine in a separate assembly module rather than in-lining it in C code.
Note:
Only a single string can be passed to the simple form of inline
assembly.
assembly.