【发布时间】:2021-11-04 11:14:02
【问题描述】:
您好,我想开始编写一些汇编代码。
我已经安装了最新的 nasm 版本 (2.15)。我按照所有步骤操作并正确设置了 nasm。
现在我想运行我的第一个 hello world 程序,使用:
nasm -f macho64 hello.asm
也可以
nasm -fmacho64 hello.asm && ld hello.o && ./a.out
也可以
nasm -f macho64 hello.asm -o hell.o && ld -e _main -macosx_version_min 10.8 -arch x86_64 hello.o -lSystem
这些都不起作用。实际上nasm -f macho64 hello.asm 不会抛出错误,但也不会打印到屏幕上。这是我复制并粘贴的代码,看看是否一切正常:
; Writes "Hello, World" to the console using only system calls. Runs on 64-b
; To assemble and run:
;
; nasm -fmacho64 hello.asm && ld hello.o && ./a.out
; --------------------------------------------------------------------------
global start
section .text
start: mov rax, 0x02000004 ; system call for write
mov rdi, 1 ; file handle 1 is stdout
mov rsi, message ; address of string to output
mov rdx, 13 ; number of bytes
syscall ; invoke operating system to do
mov rax, 0x02000001 ; system call for exit
xor rdi, rdi ; exit code 0
syscall ; invoke operating system to exi
section .data
message: db "Hello, World", 10 ; note the newline at the end
这是我在运行其他两个命令时得到的错误:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
有人可以帮助我并告诉我如何正确设置所有内容。正如我所说,命令nasm -f macho64 hello.asm 没有给出任何错误,但它没有打印出任何内容,但它应该打印出来,所以要么是实际代码的问题,要么是 nasm 本身的问题。您能否也简要解释一下其他两个命令失败的原因。在此先感谢:)
【问题讨论】:
-
不——
nasm不应该比这更响亮。如果没有要打印的错误,nasm将不会打印任何内容。同样代表例如gcc. -
是的,我可以通过使用以下 nasm -fmacho64 hello.asm && gcc hello.o && ./a.out 在屏幕上打印一些东西
标签: assembly x86 x86-64 nasm macos-big-sur