Last active
August 5, 2024 21:08
-
-
Save pankkor/c9a21f4a0bee915208b6b18daa2e5b6d to your computer and use it in GitHub Desktop.
macOS aarch64 nostdlib syscall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
f32 absf32(f32 v) { | |
f32 ret; | |
__asm__ volatile ( | |
"fabs %s0, %s1\n" | |
: "=w" (ret) | |
: "w" (v) | |
); | |
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