Skip to content

Instantly share code, notes, and snippets.

@rbento
Last active August 28, 2024 15:51
Show Gist options
  • Save rbento/48d7468fd3b662e1d8c40c6463a45fcf to your computer and use it in GitHub Desktop.
Save rbento/48d7468fd3b662e1d8c40c6463a45fcf to your computer and use it in GitHub Desktop.
My notes on Apple Silicon ARM Assembly programming

Apple Silicon ARM Assembly

Linking with macOS SDK

clang hello.c -c -o hello.o
ld hello.o -o hello

Undefined symbols for architecture arm64: "_printf", referenced from: _main in hello.o ld: symbol(s) not found for architecture arm64

xcrun -sdk macosx --show-sdk-path

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk

ld hello.o -o hello -l System -e _main -arch arm64 -syslibroot `xcrun -sdk macosx --show-sdk-path`

ARM Assembler

as --version

Apple clang version 15.0.0 (clang-1500.3.9.4) Target: arm64-apple-darwin23.5.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Program

// hello.s

.global _main
.align 2

helloworld: .ascii "hello world\n" // output string

// entry point
_main:
    b _printf
    b _terminate

_printf:                  // 4	AUE_NULL	ALL	{ user_ssize_t write(int fd, user_addr_t cbuf, user_size_t nbyte); } 
    mov X0, #0            // stdout ---------------------------------------------+                |                   |
    adr X1, helloworld    // address of output string --------------------------------------------+                   |
    mov X2, #12           // length of output string in bytes --------------------------------------------------------+
    mov X16, #4           // write to stdout
    svc 0                 // syscall

_terminate:
    mov X0, #0            // return 0
    mov X16, #1           // exit
    svc 0                 // syscall

Compiling & Linking

as hello.s -o hello.o
ld hello.o -o hello -l System -e _main -arch arm64 -syslibroot `xcrun -sdk macosx --show-sdk-path`

Supervisor Call - SVC

Instruction Set

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