Skip to content

Instantly share code, notes, and snippets.

@lunixbochs
Last active March 4, 2024 00:22
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save lunixbochs/462ee21c3353c56b910f to your computer and use it in GitHub Desktop.
Save lunixbochs/462ee21c3353c56b910f to your computer and use it in GitHub Desktop.
#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