D
D
Dmitry2014-04-13 20:07:05
linux
Dmitry, 2014-04-13 20:07:05

System call not working in inline assembler, what's the problem?

int main(void)
{
  char *path = "tmp/path";
  asm(
        "movl $39, %%eax\n\t"
        "movl %0, %%ecx\n\t"
        "movl $0x1ff, %%ebx\n\t"
        "int $0x80\n\t" : "r"(path) : "%eax" : "%ebx" : "%ecx"
    );
    return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2014-04-14
@Liro

Let's start with what doesn't "doesn't work" and doesn't even compile. Because
1. you have the only in-parameter, you need to specify it after the second colon
2. you have 3 clobbered registers, you need to specify them with a comma after the third colon, i.e.

int main(void)
{
        char *path = "tmp/path";
        asm (
             "movl $39, %%eax\n\t"
             "movl %0, %%ecx\n\t"
             "movl $0x1ff, %%ebx\n\t"
             "int $0x80\n\t"
             : : "r"(path)
             : "%eax", "%ebx", "%ecx"
            );
        return 0;
}

3. it is necessary to collect, judging by the code, under 32 bits, i.e. gcc -m32
And after it is compiled, it doesn't work, because
4. you have the order of the arguments mixed up, the path should go to ebx, the attributes of the created directory to ecx

D
Dinisoide, 2014-04-13
@Dinisoide

Please read the official documentation on inline assembler from gnu and many things will fall into place, here is an example of calling syscall exit

int main(void)
{
__asm__("movl $1,%eax\n\t"         
"xorl %ebx,%ebx\n\t"    
"int  $0x80\n\t");
return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question