2
  1. Sorry for my English.
  2. Thanks for every answer.

I've got a following code in C and inline assembly which should do the call of function: int gettimeofday(struct timeval *tp, struct timezone *tzp);

val = (struct timeval*)malloc(sizeof(struct timeval));
zone = (struct timezone*)malloc(sizeof(struct timezone));

__asm__("push $0;"
        "push %2;"
        "push %1;"
        "movl $116, %%eax;" 
        "int $0x80;"
        :"=r"(val)
        :"r"(val),"r"(zone)
        :"%eax");

The problem is, that I don't know why I need to have this line "push $0;" and my teacher said, that my arguments are not in correct order and it's just luck that it works.

How should I change this code to make it correct? Why is there "push $0;" if is it somehow correct?

lagugula
  • 63
  • 8
  • These `malloc`s are needless, given the context. These structures can be local variables just as well. – Daniel Kamil Kozar May 16 '16 at 21:14
  • You are right, my bad. – lagugula May 16 '16 at 21:18
  • I have never done any FreeBSD low-level programming myself, but [according to this](https://www.freebsd.org/doc/en_US.ISO8859-1/books/developers-handbook/x86-system-calls.html), the order of your arguments is fine (e.g. `tzone` is pushed first, then `tvalue`), but you're supposed to have another dword push before the interrupt call, whose contents don't matter. – Daniel Kamil Kozar May 16 '16 at 21:20
  • I tried to move that push as last argument, but then it does nothing with tvalue – lagugula May 16 '16 at 21:31
  • Does FreeBSD have an `int $0x80` software trap for system calls? That is a Linux thing, yes, but is it a FreeBSD thing too? – Kaz May 16 '16 at 23:02
  • Kaz, yes, as far as I know freebsd isn't much different from linux, and this interupt is same for both, but system call numbers are. – lagugula May 19 '16 at 23:30

1 Answers1

0

So, thanks to Daniel Kamil Kozar I found the problem deleting malloc-s. My teacher was bit right with order, but it was in malloc-s too. Malloc is just giving memory with "garbage", so calloc would be okay. I set zone to zero values and i could move push $0; under other 2.

lagugula
  • 63
  • 8