c code
int add4(int i, int j){ // 3,4
int res = 10;
int res2 = 11;
asm volatile(
"add %0, %1, %2 \n"
// "str w0, %1 \n"
:"=r"(res) // 3
:"r"(i),
"r"(j) // 4
:
);
printf("res:%d, res2 value:%d, j:%d \n", res, res2, j);
return res;
}
extern int myadd(int a, int b);
int main()
{
printf("Adding 00: %d+%d results in %d\n", 4, 4, add4(3, 4));
return (0);
}
汇编注释
00000000000007b8 <add4>:
7b8: a9bd7bfd stp x29, x30, [sp, #-48]!
7bc: 910003fd mov x29, sp
7c0: b9001fa0 str w0, [x29, #28] // 3:[28,32], 第一个参数先入栈
7c4: b9001ba1 str w1, [x29, #24] // 4:[24,28]
7c8: 52800140 mov w0, #0xa // #10
7cc: b9002ba0 str w0, [x29, #40] // 10:[40,44]
7d0: 52800160 mov w0, #0xb // #11
7d4: b9002fa0 str w0, [x29, #44] // 11:[44,48]
7d8: b9401fa0 ldr w0, [x29, #28] // w0=3
7dc: b9401ba1 ldr w1, [x29, #24] // w1=4
7e0: 8b010000 add x0, x0, x1 // x0=3+4=7
7e4: b9002ba0 str w0, [x29, #40] // 7写入res的位置:[40,44]
7e8: 90000000 adrp x0, 0 <_init-0x598>
7ec: 9123c000 add x0, x0, #0x8f0 // 前面都已经入栈了,这里只需要提取就好了,从右到左提取的
7f0: b9401ba3 ldr w3, [x29, #24] // w3=4
7f4: b9402fa2 ldr w2, [x29, #44] // w2=11
7f8: b9402ba1 ldr w1, [x29, #40] // w1=7
7fc: 97ffff85 bl 610 <printf@plt> // 跳转到 printf 函数
800: b9402ba0 ldr w0, [x29, #40]
804: a8c37bfd ldp x29, x30, [sp], #48
808: d65f03c0 ret