-
-
Save lpsantil/99cddf60ad6c8ae8756f to your computer and use it in GitHub Desktop.
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
#include <sys/syscall.h> | |
#define sysdef(name) \ | |
int name() { \ | |
__asm__ __volatile__( \ | |
"movq %0, %%rax;" \ | |
"mov %%rcx, %%r10;" \ | |
"syscall;" \ | |
:: "i"(SYS_##name) : "rax" \ | |
); \ | |
} | |
void _start() { | |
__asm__ __volatile__ ( | |
"pop %%rbp;" // C compiler will push rbp | |
"pop %%rdi;" // argc | |
"mov %%rsp, %%rsi;" // argv | |
"andq $-16, %%rsp;" | |
"call main;" | |
"movq %%rax, %%rdi;" // exit | |
"movq %0, %%rax;" | |
"syscall;" | |
:: "i"(SYS_exit) | |
); | |
} | |
// example program | |
sysdef(write); | |
int main(int argc, char **argv) { | |
write(1, "hello world\n", 12); | |
for (int i = 0; i < argc; i++) { | |
write(1, "arg\n", 4); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment