C语言嵌入AT&T汇编 代码段错误
int sum(int N,int arry[]){
int i = 0;
int s = 0;
for(i = 0;i < N;i++)
s += arry[i];
return s;
}
#include <stdio.h>
#include <stdlib.h>
int sum(int N,int arry[]);
int main(int argc,char *argv[]){
int i;
int temarr[argc];
temarr[0] = 0;
for(i = 1;i < argc; i++)
temarr[i] = atoi(argv[i]);
printf("Total is %d \n",sum(argc,temarr));
return 0;
}
这是C语言。现在要求是把main中调用sum()换成内嵌汇编的方式。我做的是直接把sum.o反汇编,直接截取其中的汇编代码,但是报错!Segmentation fault (core dumped)。初步判断是反汇编之后标号和地址没有改变。内嵌汇编如下:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[]){
int i;
int total = 0;
int temarr[argc];
temarr[0] = 0;
for(i = 1;i < argc; i++)
temarr[i] = atoi(argv[i]);
__asm__ __volatile__ (
"pushq %%rbp;\n\t"
"movq %%rsp,%%rbp;\n\t"
"movl %%edi,-0x14(%%rbp);\n\t"
"movq %%rsi,-0x20(%%rbp);\n\t"
"movl $0x0,-0x8(%%rbp);\n\t"
"movl $0x0,-0x4(%%rbp);\n\t"
"movl $0x0,-0x8(%%rbp);\n\t"
"jmp 7f;\n\t"
"8:\tmovl -0x8(%%rbp),%%eax;\n\t"
"cltq;\n\t"
"leaq 0x0(,%%rax,4),%%rdx;\n\t"
"movq -0x20(%%rbp),%%rax;\n\t"
"addq %%rdx,%%rax;\n\t"
"movl (%%rax),%%eax;\n\t"
"addl %%eax,-0x4(%%rbp);\n\t"
"addl $0x1,-0x8(%%rbp);\n\t"
"7:\tmovl -0x8(%%rbp),%%eax;\n\t"
"cmpl -0x14(%%rbp),%%eax;\n\t"
"jl 8b;\n\t"
"movl -0x4(%%rbp),%%eax;\n\t"
"popq %%rbp;\n\t"
"retq;\n\t"
"nopw %%cs:0x0(%%rax,%%rax,1);\n\t"
"nopl (%%rax);\n\t"
: "=a" (total)
: "b" (argc),"a" (temarr)
: "memory"
);
printf("Total is %d \n",total);
return 0;
}
求大神告知解决办法!!