How to Write Inline Assembly in ARM C: Syntax and Examples
In ARM C, you write inline assembly using the
__asm__ or asm keyword followed by the assembly code in quotes. You can embed simple instructions directly or use extended syntax with input/output operands and clobbers for more control.Syntax
The basic syntax for inline assembly in ARM C uses the asm or __asm__ keyword followed by the assembly code in double quotes. For simple instructions, you write:
asm ("assembly instructions");For more complex usage, the extended syntax allows specifying output operands, input operands, and clobbered registers:
asm volatile ("assembly code"
: output_operands
: input_operands
: clobbered_registers);
Here:
- asm volatile: Prevents the compiler from optimizing away the assembly.
- output_operands: Variables modified by assembly.
- input_operands: Variables used as inputs.
- clobbered_registers: Registers or flags changed by assembly.
c
asm volatile ("mov r0, #1\n" "add r0, r0, #2\n" : /* no outputs */ : /* no inputs */ : "r0");
Example
This example shows how to add two numbers using inline assembly in ARM C and return the result.
c
#include <stdio.h> int add_two_numbers(int a, int b) { int result; asm volatile ( "add %[res], %[val1], %[val2]" : [res] "=r" (result) // output operand : [val1] "r" (a), [val2] "r" (b) // input operands : /* no clobbers */ ); return result; } int main() { int x = 5, y = 7; int sum = add_two_numbers(x, y); printf("Sum of %d and %d is %d\n", x, y, sum); return 0; }
Output
Sum of 5 and 7 is 12
Common Pitfalls
Common mistakes when writing inline assembly in ARM C include:
- Not specifying
volatilewhen needed, causing the compiler to optimize away the assembly. - Incorrectly declaring input/output operands, leading to wrong register usage or values.
- Forgetting to list clobbered registers, which can cause unpredictable behavior.
- Using assembly instructions incompatible with the target ARM architecture.
Always test your assembly code carefully and use the extended syntax for clarity and safety.
c
/* Wrong: missing output operand and clobber */ asm ("mov r0, #1"); /* Right: specify clobber to inform compiler */ asm volatile ("mov r0, #1" : : : "r0");
Quick Reference
| Component | Description | Example |
|---|---|---|
| asm / __asm__ | Keyword to start inline assembly | asm ("mov r0, #0"); |
| volatile | Prevents optimization removal | asm volatile ("nop"); |
| Output operands | Variables modified by assembly | : "=r" (result) |
| Input operands | Variables used as inputs | : "r" (input_var) |
| Clobbered registers | Registers changed by assembly | : "r0", "cc" |
Key Takeaways
Use the asm or __asm__ keyword with quotes to write inline assembly in ARM C.
Prefer extended asm syntax with input/output operands and clobbers for safe and clear code.
Always use volatile if the assembly must not be optimized away.
Declare clobbered registers to avoid unexpected compiler behavior.
Test inline assembly carefully on your target ARM architecture.