3.2 Using inline assembly code

The compiler provides an inline assembler that enables you to write optimized assembly language routines, and to access features of the target processor not available from C or C++.

The __asm keyword can incorporate inline GCC syntax assembly code into a function. For example:

#include <stdio.h>

int add(int i, int j)
  int res = 0;
  __asm (
    "ADD %[result], %[input_i], %[input_j]"
    : [result] "=r" (res)
    : [input_i] "r" (i), [input_j] "r" (j)
  return res;

int main(void)
  int a = 1;
  int b = 2;
  int c = 0;

  c = add(a,b);

  printf("Result of %d + %d = %d\n", a, b, c);


The inline assembler does not support legacy assembly code written in ARM assembler syntax. See the Migration and Compatibility Guide for more information about migrating ARM syntax assembly code to GCC syntax.

The general form of an __asm inline assembly statement is:

__asm(code [: output_operand_list [: input_operand_list [: clobbered_register_list]]]);

code is the assembly code. In this example, this is "ADD %[result], %[input_i], %[input_j]".

output_operand_list is an optional list of output operands, separated by commas. Each operand consists of a symbolic name in square brackets, a constraint string, and a C expression in parentheses. In this example, there is a single output operand: [result] "=r" (res).

input_operand_list is an optional list of input operands, separated by commas. Input operands use the same syntax as output operands. In this example there are two input operands: [input_i] "r" (i), [input_j] "r" (j).

clobbered_register_list is an optional list of clobbered registers. In this example, this is omitted.

Non-ConfidentialPDF file icon PDF versionARM 100066_0608_00_en
Copyright © 2014–2017 ARM Limited or its affiliates. All rights reserved.