Mac nasm 汇编入门

下载

brew install nasm
  • code
SECTION .data

msg: db "hello world", 0x0a
len: equ $-msg

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
  • 编译
nasm -f macho64 -o asm1.o asm1.asm
  • 错误链接命令

    ld -o asm1 -e _main asm1.o 
    
  • 提示如下

    ld: dynamic main executables must link with libSystem.dylib for architecture x86_64
    
  • 正确链接

# 方式一   -macosx_version_min 10.15你的Mac版本
ld -o asm1 -e _main asm1.o -macosx_version_min 10.15 -static
# 方式二 ,会提示警告 ,如下
ld -o asm1 -e _main asm1.o -macosx_version_min 10.15 -lSystem
  • 警告如下

    ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in _main from asm1.o. To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie
    
原文地址:https://www.cnblogs.com/tanoak/p/12072190.html