参考和感谢:

https://blog.csdn.net/s793223706/article/details/82469971 教你在Mac上写汇编

https://stackoverflow.com/questions/14103624/how-to-compile-using-nasm-on-macosx 解决了执行ld指令时候的警告问题

其实想学学linux相关的,但是这一个月的经验告诉我,最好将事情一件一件干好,不能朝三暮四。否则一件事情都干不好。

mac上安装nasm(需要先安装brew)

brew install nasm

确认是否已经安装成功

#xushiyundeMBP:~ xushiyun$ nasm -v
NASM version 2.14.02 compiled on Dec 27 2018

编写helloworld(照着网上敲得,现在还是小白)

SECTION .text
global _main

kernel:
        syscall
        ret

_main:
        mov rax,0x2000004
        mov rdi,1
        mov rsi,msg
        mov rdx,len
        call kernel

        mov rax,0x2000001
        mov rdi,0
        call kernel

汇编:

xushiyundeMBP:nasm xushiyun$ nasm -f macho64 -o helloworld.o helloworld.asm

连接:

ld -o helloworld -e _main helloworld.o -lSystem -macosx_version_min 10.8

这两个后缀(-lSystem 和 -maxosx_version_min 10.8)需要加上去,否则会报一下警告:

ld: warning: No version-min specified on command line
ld: dynamic main executables must link with libSystem.dylib for inferred architecture x86_64

执行:

./helloworld

执行结果:

xushiyundeMBP:nasm xushiyun$ ./helloworld
helloworld!

更多推荐