You copied the Doc URL to your clipboard.

Embedded assembler syntax in C and C++

An embedded assembly language function definition is marked by the __asm function qualifier in C and C++, or the asm function qualifier in C++, 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 language function is:

__asm return-type function-name(parameter-list)
  // ARM/16-bit Thumb/32-bit Thumb assembler code
  instruction{;comment is optional} 


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.

Example 41 shows a string copy routine as a not very optimal embedded assembler routine.

#include <stdio.h>
__asm void my_strcpy(const char *src, char *dst)
      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;