源代码:
#include <stdio.h>
int add(int n)
{
int sum = 0;
for (int i = 0; i < n; i++) {
sum += i;
}
return sum;
}
int main(int argc, const char * argv[])
{
printf("%d\n",add(6));
return 0;
}
经过gcc 编译AT&T 汇编代码为:
_add:
Leh_func_begin1:
pushq %rbp
Ltmp0:
movq %rsp, %rbp
Ltmp1:
movl %edi, -4(%rbp) //保存传进来的参数 n
movl $0, -16(%rbp) //sum
movl $0, -20(%rbp) // i
jmp LBB1_2
LBB1_1:
movl -16(%rbp), %eax //sum
movl -20(%rbp), %ecx //i
addl %ecx, %eax //sum + i
movl %eax, -16(%rbp) //sum = sum + i
movl -20(%rbp), %eax //保存sum
addl $1, %eax //i++
movl %eax, -20(%rbp) //保存sum
LBB1_2:
movl -20(%rbp), %