Skip to content

Instantly share code, notes, and snippets.

@Tpaefawzen
Created July 26, 2025 08:03
Show Gist options
  • Select an option

  • Save Tpaefawzen/6173670b17e8e9afb579b8cacdb84dc5 to your computer and use it in GitHub Desktop.

Select an option

Save Tpaefawzen/6173670b17e8e9afb579b8cacdb84dc5 to your computer and use it in GitHub Desktop.
Inline assembly for AArch64 in C program when you want to call system call on Linux
static
long long os_write(long long fd, void *buf, long long len) {
register long long x0 __asm__("x0") = fd;
register long long x1 __asm__("x1") = (long long)buf;
register long long x2 __asm__("x2") = len;
register long long x8 __asm__("x8") = 64;
__asm__ volatile (
"svc 0"
: "+r"(x0)
: "r"(x1), "r"(x2), "r"(x8)
);
return x0;
}
static
void
os_exit(long long n) {
register long long x0 __asm__("x0") = n;
register long long x8 __asm__("x8") = 93;
__asm__ volatile (
"svc 0"
:
: "r"(x0), "r"(x8)
);
__builtin_unreachable();
}
void _start(void) {
char msg[] = "Hi\n";
long long n = os_write(2, msg, sizeof(msg)-1);
os_exit(n);
__builtin_unreachable();
}

How to build

make CFLAGS=-nostdlib\ -static\ -O2 34-asm

I suffered from finding the program that the optimizer will never ruin my code.

failure examples

static long long
os_write(long long fd, void *buf, long long len) {
__asm__ volatile (
    "mov x8,64\n"
    "svc 0\n"
    "ret\n"
);
    __builtin_unreachable();
}
__asm__ (
   ".globl os_write\n"
   "os_write:\n"
   "mov x8,64\n"
   "svc 0\n"
   "ret\n"
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment