Skip to content

Instantly share code, notes, and snippets.

@pankkor
Last active June 6, 2024 12:57
Show Gist options
  • Save pankkor/c9a21f4a0bee915208b6b18daa2e5b6d to your computer and use it in GitHub Desktop.
Save pankkor/c9a21f4a0bee915208b6b18daa2e5b6d to your computer and use it in GitHub Desktop.
macOS aarch64 syscall
i64 syscall1(i64 sys_num, i64 a0) {
i64 ret;
__asm__ volatile (
"mov x16, %[sys_num]\n"
"mov x0, %[a0]\n"
"svc 0x80\n"
"mov %0, x0\n"
: "=r" (ret)
: [sys_num] "r" (sys_num), [a0] "r" (a0)
: "x16", "x0"
);
return ret;
}
i64 syscall2(i64 sys_num, i64 a0, i64 a1) {
i64 ret;
__asm__ volatile (
"mov x16, %[sys_num]\n"
"mov x0, %[a0]\n"
"mov x1, %[a1]\n"
"svc 0x80\n"
"mov %0, x0\n"
: "=r" (ret)
: [sys_num] "r" (sys_num), [a0] "r" (a0), [a1] "r" (a1)
: "x16", "x0", "x1"
);
return ret;
}
i64 syscall3(i64 sys_num, i64 a0, i64 a1, i64 a2) {
i64 ret;
__asm__ volatile (
"mov x16, %[sys_num]\n"
"mov x0, %[a0]\n"
"mov x1, %[a1]\n"
"mov x2, %[a2]\n"
"svc 0x80\n"
"mov %0, x0\n"
: "=r" (ret)
: [sys_num] "r" (sys_num), [a0] "r" (a0), [a1] "r" (a1), [a2] "r" (a2)
: "x16", "x0", "x1", "x2"
);
return ret;
}
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
#define SYS_exit 1
#define SYS_write 4
void start(void) {
const char *msg = "Hello, World!\n";
syscall3(SYS_write, STDOUT_FILENO, (i64)msg, 14);
syscall1(SYS_exit, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment