Hello world in assembly for macOS on Apple Silicon

Assembly code for Apple Silicon M1 (arm64) tested on macOS 14 Sonoma and macOS 15 Sequoia


.global _main
.align 2

_main:
mov x0, #1    // file descriptor 1 (stdout) in X0
adr x1, str   // address of str in X1 (don't replace adr with ldr)
mov x2, #13   // 13 (length of str) in X2
mov x16, #4   // 4 (macOS write) in X16
svc #0        // system call (supervisor call)

mov x0, #0    // 0 (program's return value) in X0
mov x16, #1   // 1 (macOS exit) in X16
svc #0

str:
.ascii "Hello World!\n"

Compile and link:

% as hello.s -o hello.o
% ld hello.o -o hello
% ./hello
Hello World!


System info:

% as --version
Apple clang version 16.0.0 (clang-1600.0.26.4)
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
% ld -v
@(#)PROGRAM:ld PROJECT:ld-1115.7.3
BUILD 07:38:57 Oct 4 2024
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
will use ld-classic for: armv6 armv7 armv7s arm64_32 i386 armv6m armv7k armv7m armv7em
LTO support using: LLVM version 16.0.0 (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 16.0.0 (tapi-1600.0.11.8)

Commenti

Post popolari in questo blog

Selvaggia Lucarelli di nuovo condannata, ma dice che non ne sa niente

Selvaggia Lucarelli non si è pentita del suicidio di Giovanna Pedretti