【发布时间】:2016-03-15 10:35:51
【问题描述】:
这是我第一次尝试用汇编编程。我使用的是 64 位 Mac 操作系统。我也在使用 NASM。我已经四处寻找解决方案,但找不到任何适用于我的机器的方法。
谁能帮我解决这个问题?这是代码和错误,谢谢!
hello.asm
global start
section .text
start:
mov rax, 1
mov rdi, 1
mov rsi, message
mov rdx, 13
syscall
mov eax, 60
xor rdi, rdi
syscall
message:
db "Hello, World", 10
我的执行尝试:
nasm -f macho64 hello.asm -o hello.o
ld -arch i386 -o hello hello.o
./hello
错误
ld: warning: -macosx_version_min not specified, assuming 10.10
ld: warning: ignoring file hello.o, file was built for unsupported file format ( 0xCF 0xFA 0xED 0xFE 0x07 0x00 0x00 0x01 0x03 0x00 0x00 0x00 0x01 0x00 0x00 0x00 ) which is not the architecture being linked (i386): hello.o
Undefined symbols for architecture i386:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture i386
【问题讨论】:
-
如果您的目标是 64 位代码(可执行文件),显然您正在这样做,请从 LD 命令行中删除
-arch i386。 -
@MichaelPetch 成功了,谢谢!但是,现在我收到了
Segmentation fault 11。知道如何解决这个问题吗?谢谢。 -
您的代码适用于 linux,但您显然是在 macos 上。
-
您似乎在 OS/X 上使用 Linux 系统调用。 64 位代码中 OS/X 上的写入系统调用是 0x20000004 ,退出系统调用是 0x20000001 。在 OS/X 上的 32 位代码中,您从每个系统调用中减去 0x20000000(因此它们在 32 位中是 4 和 1)。
-
哇,我难以置信的困惑。不过,感谢您解决了我的问题。如果您将其发布为答案,我会接受。
标签: macos assembly linker x86-64 nasm