Skip to content

Instantly share code, notes, and snippets.

@n2o
Last active February 17, 2025 19:26
Show Gist options
  • Save n2o/ed84ce7bcc5e3d4eb4ed8e57a2015ef2 to your computer and use it in GitHub Desktop.
Save n2o/ed84ce7bcc5e3d4eb4ed8e57a2015ef2 to your computer and use it in GitHub Desktop.
Compiling x86_64 assembly program on M1 Mac

If you have an M1 Mac and want to compile and execute x86_64 assembly code, make sure you install Rosetta 2 and nasm (brew install nasm).

Than, take a 64 Bit assembly program, e.g. from this tutorial page (https://cs.lmu.edu/~ray/notes/nasmtutorial/) in Section "Your First Program" for macOS, save it to a file called hello.asm.

Compile, link and execute the program:

nasm -f macho64 hello.asm
ld hello.o -o hello -macosx_version_min 11.0 -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lSystem
./hello
@mattgillard
Copy link

mattgillard commented Jun 26, 2024

This does not work on Sonoma.

Need to change this line:
mov rsi, message
to
lea rsi, [rel message]

Also global label needs to be _start
global _start

and link:

ld hello.o -o hello -macos_version_min 13.0 -e _start

@walles
Copy link

walles commented Feb 17, 2025

@mattgillard I applied your changes but Undefined symbols for architecture x86_64 _main.

/t/hej $ ld hello.o -o hello -macos_version_min 13.0
ld: warning: alignment (1) of atom 'start' (/private/tmp/hej/hello.o) is too small and may result in unaligned pointers
ld: warning: no platform load command found in '/private/tmp/hej/hello.o', assuming: macOS
Undefined symbols for architecture x86_64:
  "_main", referenced from:
      <initial-undefines>
ld: symbol(s) not found for architecture x86_64
/t/hej [1]$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment