运行环境为64位 linux子系统
;------- 64-> 64 ---------
nasm -f elf -o xxx.o xxx.s
;-f 为制定编译输出文件格式
;elf为和gcc编译的目标文件链接
;-o 是为生成的文件起别名
;-s 是将错误添加到stdout中
gcc -c yyy.c -o yyy.o
ld -s -o xxx_yyy xxx.o yyy.o
;-------- 64->32 ---------
nasm -f elf xxx.s -o xxx.o
gcc -m32 -c yyy.c -o yyy.o
ld -m elf_i386 -s -o xxxyyy xxx.o yyy.o
很重要的一点:这时候的环境是64位的环境。所以在编译程序的时候要注意编译成32位的。
在第6章讲到内联汇编的时候:
char *str="hello,world\n";
int count=0;
int main(){
asm("pusha;"
"movl $4,%eax;"
"movl $1,%ebx;"
"movl str,%ecx;"
"movl $12,%edx;"
"int $0x80;"
"mov %eax,count;"
"popa");
return 0;
}
如果用 linux子系统编译:可能会出现这样的错误:
/usr/bin/ld: cannot find -lgcc
这是说明该64系统缺少32位的库。
解决方法就是将库补上/usr/bin/ld: cannot find -lgcc 解决问题的文章
编译成功后,但在运行时./inlineASM.bin
时候报错:
-bash: ./inlineASM.bin: cannot execute binary file: Exec format error
使用这篇文章的1、2步骤即可。Win 64位Linux子系统运行32位elf程序(简洁明了/亲测可用)