基本结构如下
__asm [volatile] (code); /* Basic inline assembly syntax */
/* Extended inline assembly syntax */
__asm [volatile] (code_template
: output_operand_list
[: input_operand_list
[: clobbered_register_list]]
);
其中,
code 是汇编指令, 例如 “ADD R0, R1, R2”.
code_template 是汇编指令的模板, 例如 “ADD %[result], %[input_i], %[input_j]”. 如果你列出了code_template,而不是code,那么你必须在指定optional input_operand_list 和clobbered_register_list之前,先指定output_operand_list。
output_operand_list是一个输出操作数列表, 用逗号分隔。
举例:
#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);
}
加载8到r0寄存区
void sum (void)
{
__asm (
"mov r0, #8"
);
}