linux可以使用nasm和gas编译运行汇编
nasm一般编译Intel风格的汇编
流程如下
使用nasm编译汇编文件(.asm和.s都行)
nasm -f elf -o example.o example.asm
使用ld命令链接目标文件并生成可执行文件
ld -m elf_i386 -o example example.o
运行生成的可执行文件
./example
gas一般编译AT&T风格的汇编
流程如下
使用as命令编译汇编文件
as -o example.o example.s
使用ld命令链接目标文件并生成可执行文件
ld -o example example.o
运行生成的可执行文件
./example
遇见编译报错
ld: i386 架构于输入文件foo.o 与 i386:x86-64 输出不兼容
这是以64位架构的CPU,如果使用elf参数时,默认是以32位模式去处理,那么此时需要更精确的去指定这个模式,比如elf32(32位),elf64(64位)
上面写法改为
nasm -f elf64 -g -F stabs sandbox.asm -o sandbox.o
ld -o sandbox sandbox.o
./sandbox