Thread: send() system call using _asm and interrupt

  1. #1
    Registered User
    Join Date
    Jun 2008

    send() system call using _asm and interrupt

    I wrote the following code for send() [of socket.h],
    char buffer[4];
    strcpy(buffer, "hi");
     __asm__("sub  $16,%%esp\n"
            "movl   %%ebx,(%%esp)\n"
            "movl   %%ecx,4(%%esp)\n"
            "movl   $3,8(%%esp)\n"
            "movl   $0,12(%%esp)\n" 
            "movl   $102,%%eax\n" // Interrupt number
            "movl   $16,%%ebx\n" // defined in net.h to be SYS_SENDMSG
            "movl   %%esp,%%ecx\n"
            "int    $0x80\n"
            "add    $16,%%esp\n"
            : "=a" (s)
            :"b"(sockD), "c"(buffer)
    It returns a negative number, and I ran a strace on it, which gave me the following error:
    sendmsg(3, {msg_name(0)=ptrace: umoven: Input/output error
    {...}, msg_iov(0)=[], msg_controllen=2000ptrace: umoven: Input/output error
    , msg_control=0x3, msg_flags=0}, MSG_OOB|MSG_PEEK) = -1 EFAULT (Bad address)
    So I guess I cannot pass a char buffer like this in asm. Can someone help me on how to pass it for this problem? The socket() call and connect() call before the send are going through fine.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    Usually you just push a function parameter. Though in MASM there is actually the invoke command which is kind of nifty. In either case, push is more standard. Though this is where calling convention comes into play (not so much for this C call as much as PASCAL calls).

  3. #3
    Registered User
    Join Date
    Jun 2008
    Thanks for replying, but I managed to fix the problem. I was using 16 which was for sendmsg. It worked fine when I started using 9, which was for send
    Last edited by raghu2383; 09-25-2008 at 12:09 AM. Reason: Issue resolved

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    And the purpose of NOT using the pre-defined Send() system call is what? Fiddling about with inline assembler for something that is defined in the system seems quite pointless to me.

    Edit: One reason NOT to do this would be that the kernel MAY use SYSENTER or SYSCALL (as appropriate) to call the kernel, which will be faster than the INT 0x80 route. It is also not portable to a 64-bit architecture.

    Edit2: Using push or pre-allocating the space on the stack is a matter of which processor it is for - some processors (like old 386/486 and early Pentium's) prefer to have one stack change and then offset used to store the arguments, whilst more modern processors (such as Athlon) actually perform better if you use push instructions to store to the stack - because that's what most compilers do, so the processor designers decided to make that part as fast as possible.

    Last edited by matsp; 09-25-2008 at 02:46 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Tags for this Thread