[bits 32] global setjmp global longjmp ;typedef struct { ; uint32_t ebx; ; uint32_t esp; ; uint32_t ebp; ; uint32_t esi; ; uint32_t edi; ; uint32_t eip; ;} jmp_buf[1]; ; int setjmp(jmp_buf env) setjmp: mov eax, [esp+4] ; the adress of the jump buffer mov [eax], ebx ; safe registers mov [eax+4], esp mov [eax+8], ebp mov [eax+12], esi mov [eax+16], edi mov edx, [esp] ; get return address from the stack (pushed here by call) mov [eax+20], edx mov eax, 0 ; indicate that we come from setjmp, not from a longjmp ret ; void longjmp(jmp_buf env, int value); longjmp: mov eax, [esp+4] ; the address of the jump buffer mov ecx, [esp+8] ; the return value for setjmp mov ebx, [eax] ; restore registers mov esp, [eax+4] mov ebp, [eax+8] mov esi, [eax+12] mov edi, [eax+16] mov edx, [eax+20] ; get jump address and store it on the stack for 'ret' mov [esp], edx mov eax, ecx ; get return value cmp eax, 0 jnz .return ; non zero, ok, return it mov eax, 1 ; setjmp called with 0, not good, return 1 .return: ret