From 57fb0272168371de0fac1d54d0f5830b1565aa94 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 26 Jan 2019 06:23:16 +0100 Subject: [PATCH] LibC: Implement setjmp() and longjmp(). I think this is correct. The following registers are saved and restored: EBX, ESI, EDI, EBP, ESP and EIP. --- LibC/setjmp.cpp | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/LibC/setjmp.cpp b/LibC/setjmp.cpp index 48fed07ff8..fca2a81a0b 100644 --- a/LibC/setjmp.cpp +++ b/LibC/setjmp.cpp @@ -2,13 +2,32 @@ #include #include -int setjmp(jmp_buf) -{ - //assert(false); - return 0; -} +asm( +"setjmp:\n\ + movl %ebx, 0(%eax)\n\ + movl %esi, 4(%eax)\n\ + movl %edi, 8(%eax)\n\ + movl %ebp, 12(%eax)\n\ + movl %esp, 16(%eax)\n\ + movl (%esp), %ecx\n\ + movl %ecx, 20(%eax)\n\ + xorl %eax, %eax\n\ + ret\n\ +"); -void longjmp(jmp_buf, int) -{ - assert(false); -} +asm( +"longjmp:\n\ + xchgl %edx, %eax\n\ + test %eax, %eax\n\ + jnz 1f\n\ + incl %eax\n\ +1:\n\ + mov 0(%edx), %ebx\n\ + mov 4(%edx), %esi\n\ + mov 8(%edx), %edi\n\ + mov 12(%edx), %ebp\n\ + mov 16(%edx), %ecx\n\ + mov %ecx, %esp\n\ + mov 20(%edx), %ecx\n\ + jmp *%ecx\n\ +");