【问题标题】:compiling assembly code on mac os big sur (x8664)在 mac os big sur (x8664) 上编译汇编代码
【发布时间】: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


【解决方案1】:

您只需稍微调整代码并将其与正确的开关链接(请参阅_main 而不是start):

          global    _main

          section   .text
_main:    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
$ nasm -fmacho64 hello.asm && ld -e _main hello.o -lSystem && ./a.out
Hello, World
$

【讨论】:

  • 如果不是实际上调用的东西_main通常不是一个好主意,它被称为具有有效返回地址的main,并且argc、argv和libc已初始化。而不是更改源代码中的符号,将ld arg 更改为-e start 是否更有意义?或者这个命令是否仍然运行 CRT 初始化函数和东西,所以你可以使用 ret 代替 _exit 系统调用?
猜你喜欢
  • 2021-08-26
  • 2013-02-06
  • 2022-01-14
  • 2023-04-03
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多