%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 %%end
	mov     [sys_errno], eax
	mov     eax, -1
%%end:
  %if %0 > 1
	pop     ebx
  %endif
%endmacro

%macro pbegin 0
    push	ebp
    mov		ebp, esp
%endmacro

%macro pend 0
    mov		esp, ebp
    pop		ebp
    ret
%endmacro

global sys_errno
global sys_exit
global sys_read
global sys_write
global sys_open
global sys_close

section .bss

sys_errno resd 1

section .text

sys_exit:
	pbegin
	scall	1, [ebp+8]
	pend

sys_read:
	pbegin
	scall	3, [ebp+8], [ebp+12], [ebp+16]
	pend

sys_write:
	pbegin
	scall	4, [ebp+8], [ebp+12], [ebp+16]
	pend

sys_open:
	pbegin
	scall	5, [ebp+8], [ebp+12], [ebp+16]
	pend

sys_close:
	pbegin
	scall	6, [esp+8]
	pend
