%macro pcall 1-*
; call subroutines < (name, param1, param2, param3)
  %rep %0 - 1
    %rotate -1
	push    dword %1
  %endrep
  %rotate -1
	call    %1
	add     esp, (%0 - 1)*4
%endmacro

%macro scall 1-4
; system call
;   < (interrupt number, param1, param2, param3)
  %if %0 > 1
	push    ebx	            ; it can only spoil eax, ecx, edx
  %endif

  %rep %0
    %rotate -1
	push    dword %1	    ; push in reverse order
  %endrep
	pop     eax
  %if %0 > 1
	pop     ebx
    %if %0 > 2
	pop     ecx
      %if %0 > 3
	pop     edx
      %endif
    %endif
  %endif
	int     80h

	cmp     eax, 0xfffff000
	jb %%ok
	mov     ecx, -1
	jmp short %%end
%%ok:
	xor     ecx, ecx
%%end:
  %if %0 > 1
	pop     ebx
  %endif
%endmacro

%macro pbegin 1-4
; procedure header < (sub, reg1, reg2, reg3)
	push    ebp
	mov     ebp, esp
	sub     esp, %1
  %rep %0-1
    %rotate 1
	push    %1
  %endrep
%endmacro

%macro pend 0-3
; procedure tail < (reg1, reg2, reg3)
  %rep %0
    %rotate -1
	pop     %1
  %endrep
	mov     esp, ebp
	pop     ebp
	ret
%endmacro
