views:

60

answers:

1

Hi, I successfully called the exit syscall from assembly but I'm strugling to call the _getpid syscall and use it's return value. Here is the code I'm using

.text
.globl _getpiddirect

_getpiddirect:
    pushl %ebp
    movl %esp, %ebp
    subl $8, %esp
    movl $39, %eax
    int $0x80
    addl $8, %esp
    popl %ebp
    ret

and

#include <stdio.h>
#include <unistd.h>

extern unsigned long getpiddirect();

int main(int argc, const char *argv[])
{
   printf("%lu\n", getpiddirect());
   printf("%lu\n", (unsigned long) getpid());
   return 0;
}

getpiddirect keeps returning 4056.

A: 

Hello,

Thats because 39 is a code for getppid - get parent process id and that is what you're getting as 4056. The getpid code is 20, but please look at /usr/include/sys/syscall.h for the value of SYS_getpid as exact constant used on your system.

Also i'm not sure why you want 8 bytes on the stack prior to calling getpid through interrupt. It doesn't affect anything and is just useless, no?

Inso Reiges
Thanks your answers is correct. I mistaken getppid with getpid.The extra 8 bytes are necessary to keep the stack aligned as required by mac os ABI.