很简单,使用gcc即可,我们写如下程序:
pop eax
保存为文本文件,命名为test.s
然后使用gcc在32位的环境下编译
gcc test.s
结果会报错,为什么呢?
/usr/bin/ld: /tmp/ccj87S8M.o: relocation R_X86_64_32S against undefined >symbol
eax' can not be used when making a PIE object; recompile with -fPIC /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function_start’:
(.text+0x20): undefined reference to `main’
/usr/bin/ld: final link failed: Invalid operation
collect2: error: ld returned 1 exit status
因为gcc默认汇编是要编译链接一气呵成的。这里我们使用参数-c,即可只编译不链接。
gcc -c test.s
这样就出来了一个test.o文件,里面只有pop eax的机器码,我们可以使用objdump -d查看。
objdump -d test.o
可以看到pop eax被转化为了popq,为什么呢?因为gcc采用的是AT&T 语法pop eax要写成pop %eax再编译才行

本文探讨了使用GCC和NASM编译汇编语言程序的过程与区别。介绍了如何正确编译包含popeax指令的汇编代码,解释了GCC默认编译设置的问题,并展示了如何使用NASM进行更准确的编译。
572

被折叠的 条评论
为什么被折叠?



