7.27 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++.

The __asm and asm function qualifiers 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/Thumb assembly 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.

The following example 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;
Non-ConfidentialPDF file icon PDF versionARM DUI0472M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.