/* mainprog.c - An example of calling an assembly function
这里用的是 Professional Assembly Language 源码
我的环境 gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3)
我改了源码 把所有32位寄存器全改这64位。 同时 指令后辍改为q
1。源码的第一个坑 在c 文件里要加extern int asmfunc();
2。第二个坑 直接 gcc -o a.out xxxx.s xxxxxx.c
报:
allenboy@allenboy:~/桌面$ gcc -static -o allen mainprog.c asmfunc.s
allenboy@allenboy:~/桌面$ gcc -o allen mainprog.c asmfunc.s
/usr/bin/ld: /tmp/cc1qsSmY.o: relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC
加上
gcc -fpic -o allen mainprog.c asmfunc.s
报:一样的错误
allenboy@allenboy:~/桌面$ gcc -fpic -o allen mainprog.c asmfunc.s
/usr/bin/ld: /tmp/cc5B1l8Q.o: relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: 最后的链结失败: 输出不可表示的节
collect2: error: ld returned 1 exit status
这个坑一直没解决 (对gcc不太了解)
这里解决办法是他报不能生成动态库 ,我强制生成静态的编译通过
allenboy@allenboy:~/桌面$ gcc -static -o allen mainprog.c asmfunc.s
allenboy@allenboy:~/桌面$ ls
1438742382648.jpg main10.c main5.c main.cpp
allen main11.c main6.c mainprog.c
asmfunc.s main2.c main7.c VSCode-linux-x64
code main3.cpp main8.c 美河制作.汇编语言程序设计(jb51.net).pdf
ida main4.c main9.c
allenboy@allenboy:~/桌面$ ./allen
This is a test.
This is a test message from the asm function
Now for the second time.
This is a test message from the asm function
This completes the test.
*/
#include <stdio.h>
extern int asmfunc();
int main()
{
printf("This is a test.\n");
asmfunc();
printf("Now for the second time.\n");
asmfunc();
printf("This completes the test.\n");
return 0;
}
# asmfunc.s - An example of a simple assembly language function
#.file "asmfunc.c"
.section .data
testdata:
.ascii "This is a test message from the asm function\n"
datasize:
.int 45
.section .text
.type asmfunc, @function
.globl asmfunc
asmfunc:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
movq $4, %rax
movq $1, %rbx
movq $testdata, %rcx
movq datasize, %rdx
int $0x80
popq %rbx
movq %rbp, %rsp
popq %rbp
ret
源码地址 http://download.csdn.net/download/sinat_35360663/10224892/* mainprog.c - An example of calling an assembly function 这里用的是 Professional Assembly Language 源码我的环境 gcc version 7.2.0 (Ubuntu 7.2.0-8u