Keil Logo

__asm

2.3 __asm

This keyword passes information to the armclang assembler.

The precise action of this keyword depends on its usage.

Usage

Inline assembly

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 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 our 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 our 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 our 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 our example, this is omitted.

Embedded assembly

For embedded assembly, you cannot use the __asm keyword on the function declaration. Use the __attribute__((naked)) function attribute on the function declaration. For more information, see __attribute__((naked)). For example:

__attribute__((naked)) void foo (int i);

Naked functions with the __attribute__((naked)) function attribute only support assembler instructions in the basic format:

__asm(code);

Assembly labels

The __asm keyword can specify an assembly label for a C symbol. For example:

int count __asm__("count_v1"); // export count_v1, not count
Non-ConfidentialPDF file icon PDF version100067_0612_00_en
Copyright © 2014–2019 Arm Limited or its affiliates. All rights reserved. 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.