用nasm汇编器汇编不同位数格式的ELF
在网上找到一个把32位汇编程序转为64位的存储库,
https://codeload.github.com/theadityalunawat/assembler_converter/zip/refs/heads/master
把代码下载下来以后,
32_bit.asm
extern printf
global mainsection .datamessage db "Hello World", 10, 0section .textmain:pushadpush dword messagecall printfadd esp, 4popadret
64_bit.asm
global _startsection .datamessage db "Hello Cloud!",10 , 0section .text
_start:mov rax,1mov rdi,1mov rsi, messagemov rdx, 13syscallmov rax, 60mov rdi, 0syscall
先用fasm程序汇编
结果报各种错误
./fasm asmcov/32_bit.asm
flat assembler version 1.73.32 (16384 kilobytes memory)
asmcov/32_bit.asm [1]:
extern printf
processed: extern printf
error: illegal instruction../fasm -f elf64 asmcov/64_bit.asm
flat assembler version 1.73.32
usage: fasm <source> [output]
optional settings:-m <limit> set the limit in kilobytes for the available memory-p <limit> set the maximum allowed number of passes-d <name>=<value> define symbolic variable-s <file> dump symbolic information for debugging./fasm.x64 asmcov/64_bit.asm
flat assembler version 1.73.32 (16384 kilobytes memory, x64)
asmcov/64_bit.asm [1]:
global _start
processed: global _start
error: illegal instruction.
到nasm主页下载了源代码
编译安装很简单./configure 和 make就行了。
# nasm-2.16.03/nasm asmcov/64_bit.asm
asmcov/64_bit.asm:8: error: instruction not supported in 16-bit mode
asmcov/64_bit.asm:9: error: instruction not supported in 16-bit mode
asmcov/64_bit.asm:10: error: instruction not supported in 16-bit mode
asmcov/64_bit.asm:11: error: instruction not supported in 16-bit mode
asmcov/64_bit.asm:13: error: instruction not supported in 16-bit mode
asmcov/64_bit.asm:14: error: instruction not supported in 16-bit mode
直接汇编出错,加了-f选项就可以汇编出.o文件了,注意它和asm默认保存在同一个目录下,按照assembler_converter源码中README的步骤链接成功,执行结果正确。
# nasm-2.16.03/nasm -f elf64 asmcov/64_bit.asm# nasm-2.16.03/nasm -f elf32 asmcov/32_bit.asm
# ld 64_bit.o -o 64_bit_run
ld: cannot find 64_bit.o: No such file or directory
# ld asmcov/64_bit.o -o 64_bit_run
# ./64_bit_run
Hello Cloud!
# gcc-12 -m32 asmcov/32_bit.o -o 32_bit_run
/usr/bin/ld: warning: asmcov/32_bit.o: missing .note.GNU-stack section implies executable stack
/usr/bin/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
/usr/bin/ld: asmcov/32_bit.o: warning: relocation in read-only section `.text'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
# ./32_bit_run
Hello World
但是用他的翻译器翻译的结果,虽然通过了汇编,但链接警告,执行也卡住,看来是无法工作的。
# g++ asmcov/translator.cpp
# cd asmcov/asmcov# ../a.out
new 64 bit file has been created
/asmcov# cd ..
# nasm-2.16.03/nasm -f elf64 asmcov/64_bit_conv.asm
# ld asmcov/64_bit_conv.o -o 64_bit_conv_run
ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000
# ./64_bit_conv_run
^C