ARM64 assembly hello world


Hello world in ARM64 assembly for Linux and Macos. First example is how to compile hello world for raspberry pi 4, as its supports ARMv8 instruction set. Second example is how to run assembly on Apple M1 chip that also supports ARMv8 instruction set

The two assembly examples are equivalent to C code

int main() {
    char *s="Hello ARM64";

Raspberry Pi 4

Running 64bit linux. To detect with architecture and what bitness of os run command


Architecture shown as aarch64 enoughs to indicate that os ir 64bit

Linux raspberrypi 5.4.42-v8+ #1319 SMP PREEMPT Wed May 20 14:18:56 BST 2020 aarch64 GNU/Linux

/* Data segment: define our message string and calculate its length. */
    .ascii        "Hello, ARM64!\n"
helloworld_len = . - helloworld


/* Our application's entry point. */
.globl _start
    /* syscall write(int fd, const void *buf, size_t count) */
    mov     x0, #1              /* fd := STDOUT_FILENO */
    ldr     x1, =helloworld     /* buf := msg */
    ldr     x2, =helloworld_len /* count := len */
    mov     w8, #64             /* write is syscall #64 */
    svc     #0                  /* invoke syscall */

    /* syscall exit(int status) */
    mov     x0, #0               /* status := 0 */
    mov     w8, #93              /* exit is syscall #1 */
    svc     #0                   /* invoke syscall */


Too compile check if you have installed gnu gcc, other compilers such as clang also should work perfectly fine.

    as hello.s -o hello.o
    gcc hello.o -o hello

Apple M1

.global _start            // Provide program starting address to linker
.align 2                  // Make sure everything is aligned properly

/* syscall write(int fd, const void *buf, size_t count) */
    mov    X0, #1         // 1 = StdOut
    adr    X1, helloworld     // string to print
    mov    X2, helloworld_len // length of our string
    mov    X16, #4            // Unix write system call
    svc    #0x80              // Call kernel to output the string

/* syscall exit(int status) */
    mov     X0, #0            // Use 0 return code
    mov     X16, #1           // System call number 1 terminates this program
    svc     #0x80             // Call kernel to terminate the program

helloworld:      .ascii  "Hello, ARM64!\n"
helloworld_len = . - helloworld


Install xcode tools before compilation

    as -o hello.o hello.s
    ld -macosx_version_min 11.0.0 -o hello hello.o -lSystem -syslibroot `xcrun -sdk macosx --show-sdk-path` -e _start -arch arm64