fork(1) download
  1. #include <stdio.h>
  2.  
  3. char stack[0x10000] __attribute__((aligned(16))) = {0};
  4.  
  5. void test(void *ctx) {
  6. printf("%s\n", (const char*)ctx);
  7. }
  8.  
  9. void call_with_new_stack(void* sp, void (*fn)(void*), void* ctx)
  10. {
  11. __asm volatile(
  12. "xchg %%rsp, %%rdi\n\t" //
  13. "sub $8, %%rsp\n\t"
  14. "push %%rdi\n\t"
  15. "mov %%rdx, %%rdi\n\t"
  16. "call *%%rsi\n\t"
  17. "pop %%rsp\n\t"
  18. : : "D"(sp), "S"(fn), "d"(ctx) : "memory"
  19. );
  20. }
  21.  
  22. int main() {
  23. call_with_new_stack(stack + sizeof(stack), &test, "Hello, world!");
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0s 5500KB
stdin
Standard input is empty
stdout
Hello, world!