#include <9pm/u.h>
#include <9pm/libc.h>

__declspec(naked)
void
pm_longjmp(pm_jmp_buf jb, int val)
{
	_asm {
		mov ebx, [esp+4]
		mov eax, [esp+8]
		cmp eax, 0
		jne nofix
		mov eax, 1
nofix:
		mov ebp, [ebx]
		mov edi, [ebx+8]
		mov esi, [ebx+12]
		mov esp, [ebx+16]
		mov ecx, [ebx+20]
		mov [esp], ecx
		mov ebx, [ebx+4]
		ret
	}
}

__declspec(naked)
int
pm_setjmp(pm_jmp_buf jb)
{
	_asm {
		mov eax, [esp+4]
		mov [eax], ebp
		mov [eax+4], ebx
		mov [eax+8], edi
		mov [eax+12], esi
		mov [eax+16], esp
		mov ebx, [esp]		/* return pc */
		mov [eax+20], ebx
		xor eax, eax
		ret
	}
}
