如何在MacOSX上使用nasm进行编译

Ily*_*nov 9 assembly nasm

我正在尝试编译和链接我在Assembler上的第一个程序.我尝试编译以下代码:

; %include "stud_io.inc"    
global _main     

section .text
_main: 
    xor eax, eax
again:
    ; PRINT "Hello"
    ; PUTCHAR 10
    inc eax     
    cmp eax, 5
    jl again
Run Code Online (Sandbox Code Playgroud)

在用于编译和链接程序的控制台命令下面:

-bash-3.2$ nasm -f macho main.asm -o main.o  && ld -e _main -macosx_version_min 10.8 -arch x86_64 main.o
Run Code Online (Sandbox Code Playgroud)

但结果是:

ld: warning: ignoring file main.o, file was built for i386 which is not the architecture being linked (x86_64): main.o
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     -u command line option
ld: symbol(s) not found for architecture x86_64
Run Code Online (Sandbox Code Playgroud)

我认为有必要为x86_64编译main.asm文件.如何正确编译我的系统程序?

Rag*_*geD 15

我建议先更新你的NASM.

之后,尝试运行:

nasm -f macho64 main.asm -o main.o  && ld -e _main -macosx_version_min 10.8 -arch x86_64 main.o -lSystem
Run Code Online (Sandbox Code Playgroud)

请注意,new命令在上面添加了JasonD的建议(macho64),但也添加-lSystemld命令来阻止ld抛出以下错误:

ld: dynamic main executables must link with libSystem.dylib for architecture x86_64
Run Code Online (Sandbox Code Playgroud)