| |||
Home > Using the Inline and Embedded Assemblers > Embedded assembler > Embedded assembler syntax |
An embedded assembly function definition is marked by the __asm (C and C++) or asm (C++) function qualifiers, and can be used on:
member functions
non-member functions
template functions
template class member functions.
Functions declared with __asm or asm can have arguments, and return a type. They are called from C and C++ in the same way as normal C and C++ functions. The syntax of an embedded assembly function is:
__asm return-type function-name(parameter-list){ // ARM/Thumb/Thumb-2 assembler code instruction[;instruction] ... instruction}
The initial state of the embedded assembler (ARM or Thumb) is determined by the initial state of the compiler, as specified on the command line. This means that:
if the compiler starts in ARM state, the embedded
assembler uses --arm
if the compiler starts in Thumb state, the embedded
assembler uses --thumb
.
The embedded assembler state at the start of each function
is as set by the invocation of the compiler, as modified by #pragma
arm
and #pragma thumb
pragmas.
You can change the state of the embedded assembler within
a function by using explicit ARM
, THUMB
,
or CODE16
directives in the embedded assembler function.
Such a directive within an __asm
function does
not affect the ARM or Thumb state of subsequent __asm
functions.
If you are compiling for a Thumb-2 capable processor, you can use Thumb-2 instructions when in Thumb state.
Argument names are permitted in the parameter list, but they
cannot be used in the body of the embedded assembly function. For
example, the following function uses integer i
in
the body of the function, but this is not valid in assembly:
__asm int f(int i) { ADD i, i, #1 // error }
You can use, for example, r0
instead of i
.
See the chapter on mixing C, C++, and assembly language in Developer Guide for more information on embedded assembly language in C and C++ sources.
Example 6.1 shows a string copy routine as an embedded assembler routine.
Example 6.1. String copy with embedded assembler
#include <stdio.h> __asm void my_strcpy(const char *src, char *dst) { loop LDRB r2, [r0], #1 STRB r2, [r1], #1 CMP r2, #0 BNE loop BX lr } int main(void) { const char *a = "Hello world!"; char b[20]; my_strcpy (a, b); printf("Original string: '%s'\n", a); printf("Copied string: '%s'\n", b); return 0; }