.global wd_syscall_x86
.extern wd_set_errno

.type wd_syscall_x86, %function

wd_syscall_x86:
    # Push the callee save registers.
        push    %ebx
        push    %esi
        push    %edi
        push    %ebp

        # Get and save the system call entry address.
        int    $80
        push    %eax

        # Load all the arguments from the calling frame.
        # (Not all will be valid, depending on the syscall.)
        mov     24(%esp),%eax
        mov     28(%esp),%ebx
        mov     32(%esp),%ecx
        mov     36(%esp),%edx
        mov     40(%esp),%esi
        mov     44(%esp),%edi
        mov     48(%esp),%ebp

        # Make the system call.
        call    *(%esp)
        addl    $4, %esp

        # Error?
        cmpl    $-4096, %eax
        jb      1f
        # Yes, so set errno.
        negl    %eax
        pushl   %eax
        call    wd_set_errno
        addl    $4, %esp
    1:
        # Restore the callee save registers.
        pop    %ebp
        pop    %edi
        pop    %esi
        pop    %ebx
        ret