Keil Logo

Technical Support

On-Line Manuals

Compiler User Guide

Preface Overview of the Compiler Getting Started with the Compiler Compiler Features Compiler Coding Practices Compiler Diagnostic Messages Using the Inline and Embedded Assemblers of the AR Compiler support for inline assembly language Inline assembler support in the compiler Restrictions on inline assembler support in the co Inline assembly language syntax with the __asm key Inline assembly language syntax with the asm keywo Inline assembler rules for compiler keywords __asm Restrictions on inline assembly operations in C an Inline assembler register restrictions in C and C+ Inline assembler processor mode restrictions in C Inline assembler Thumb instruction set restriction Inline assembler Vector Floating-Point (VFP) restr Inline assembler instruction restrictions in C and Miscellaneous inline assembler restrictions in C a Inline assembler and register access in C and C++ Inline assembler and the # constant expression spe Inline assembler and instruction expansion in C an Expansion of inline assembler instructions that us Expansion of inline assembler load and store instr Inline assembler effect on processor condition fla Inline assembler expression operands in C and C++ Inline assembler register list operands in C and C Inline assembler intermediate operands in C and C+ Inline assembler function calls and branches in C Inline assembler branches and labels in C and C++ Inline assembler and virtual registers Embedded assembler support in the compiler Embedded assembler syntax in C and C++ Effect of compiler ARM and Thumb states on embedde Restrictions on embedded assembly language functio Compiler generation of embedded assembly language Access to C and C++ compile-time constant expressi Differences between expressions in embedded assemb Manual overload resolution in embedded assembler __offsetof_base keyword for related base classes i Compiler-supported keywords for calling class memb __mcall_is_virtual(D, f) __mcall_is_in_vbase(D, f) __mcall_offsetof_vbase(D, f) __mcall_this_offset(D, f) __vcall_offsetof_vfunc(D, f) Calling nonstatic member functions in embedded ass Calling a nonvirtual member function Calling a virtual member function Accessing sp (r13), lr (r14), and pc (r15) Differences in compiler support for inline and emb Compiler Command-line Options Language Extensions Compiler-specific Features C and C++ Implementation Details What is Semihosting? Via File Syntax Summary Table of GNU Language Extensions Standard C Implementation Definition Standard C++ Implementation Definition C and C++ Compiler Implementation Limits

Embedded assembler syntax in C and C++

6.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} 
  ... 
  instruction
}

Note

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)
{
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;
}
Non-ConfidentialPDF file icon PDF versionARM DUI0375H
Copyright © 2007, 2008, 2011, 2012, 2014-2016 ARM. 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.