使用NASM在Ubuntu上编写64位汇编语言

3

我正在尝试编写一个简单的64位汇编语言的“Hello World”程序,并在64位Ubuntu系统上运行。程序如下:

global _start           ; entry point export for ld section .text   
_start:     ; system call to write message to stdout
    mov rax, 1      ; sys_write
    mov rdi, 1      ; stdout
    mov rsi, mes    ; message address
    mov rdx, len    ; message length
    syscall     ; exit sys call
    mov rax, 60     ; exit call id
    mov rdi, 0      ; return success
    syscall
section .data
    mes: db 'Hello, world!',0x0A    ; message
    len :   equ $-mes   

我使用 nasm -f elf64 hello64.asm 进行组装,尝试使用 ld -o hello64 hello64.o 进行链接,但是出现了以下错误 -

ld: 输入文件 `hello64.o' 的 i386:x86-64 架构与 i386 输出不兼容。

即使使用 --oformat elf64-x86-64 或 elf64-little 或 elf64-big 标志也会出现相同的错误。
有人可以帮忙吗?

相关:使用GNU工具从汇编构建静态/动态二进制文件_startmain,带/不带libc。可能您正在使用32位的Ubuntu安装?尝试file /usr/bin/ld,虽然我实际上期望32位的ld能够使用正确的--oformat制作64位可执行文件。 - Peter Cordes
2个回答

3
以下内容在我的系统上运行正常:
nasm -f elf64 hello64.asm
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello64 hello64.o

你安装了ia32-libs软件包吗? - Michael Spector

0

你可以考虑再次运行apt-get update。我正在更新版本15.04上运行它,对我来说它是有效的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接