Hello world in assembly for macOS on Apple Silicon

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


.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 15.0.0 (clang-1500.3.9.4)
Target: arm64-apple-darwin23.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
% ld -v
@(#)PROGRAM:ld PROJECT:ld-1053.12
BUILD 15:45:29 Feb 3 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 15.0.0 (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 15.0.0 (tapi-1500.3.2.2)
% uname -a
Darwin macbookpro.fritz.box 23.6.0 Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:30 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6000 arm64

Commenti

Post popolari in questo blog

Selvaggia Lucarelli non si è pentita del suicidio di Giovanna Pedretti

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

Quel che resta del giornalismo