Unexpected Segmentation fault

Hi guys, I am working with NASM version 2.15.05 and ld64-711 BUILD 21:57:11 on a MacOS Monterey.

I wrote a simple 'Hello World' program in assembly and I receive a segmentation fault I cannot really understand. Would you be so kind to help me?

The code is:

global _main

; in the .text section, we define the program
section .text
_main:
      mov       rax, 0x2000004
      mov       rdi, 1
      mov       rsi, helloMessage
      mov       rdx, helloMessage.size
      syscall

      mov       rax, 0x2000004
      xor       rdi, rdi
      syscall

section .data
      helloMessage:     db    "Hello, World!", 10
      .size             equ   $ - helloMessage

I compile it as follows:

gbiondo@tripleX ASM % nasm  -f macho64 hello.asm

and I link it like this:

gbiondo@tripleX ASM % ld hello.o -o hello1 -lc -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib

When I run the executable, I get:

gbiondo@tripleX ASM % ./hello1 
Hello, World!
zsh: segmentation fault  ./hello1

Coming from the Linux ASM, I had these problems when I forgot the syscall to exit (in this case, the second block, starting with mov rax, 0x2000004) and ending with the syscall instruction, but here? What happens?

Thanks.

Unexpected Segmentation fault
 
 
Q