Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gcc inline assembly what does "'asm' operand has impossible constraints" mean?

I have this below code within function:

void makeSystemCall(uint32_t num, uint32_t param1, uint32_t param2, uint32_t param3){
    asm volatile (
        "mov %0, %%eax\n\t"//Move num to eax
        "mov %1, %%ebx\n\t"//Move param1 to ebx
        "mov %2, %%ecx\n\t"//Move param2 to ecx
        "mov %3, %%edx\n\t"//Move param3 to edx
        "int $0x80"//Call interrupt. Data in eax, ebx, ecx and edx
        : //No output params
        : "r" (num), "r" (param1), "r" (param2), "r" (param3)//Input params
        : "%eax", "%ebx", "%ecx", "%edx" //This handles register state pushing and popping?
    );
}

Now I have no idea why this doesn't work. Gcc says: "error: 'asm' operand has impossible constraints" I have been following gcc inline assembly tutorials and I though that this would be correct way to take parameters from c code to inline assembly block.

Also I use gcc cross compiler built for 32 bit x86.

like image 710
The amateur programmer Avatar asked Aug 30 '14 06:08

The amateur programmer


People also ask

What is asm GCC?

The asm keyword allows you to embed assembler instructions within C code. GCC provides two forms of inline asm statements.

Does GCC support inline assembly?

There are, in general, two types of inline assembly supported by C/C++ compilers: asm (or __asm__) in GCC. GCC uses a direct extension of the ISO rules: assembly code template is written in strings, with inputs, outputs, and clobbered registers specified after the strings in colons.

What is __ asm __ volatile?

The __volatile__ modifier on an __asm__ block forces the compiler's optimizer to execute the code as-is. Without it, the optimizer may think it can be either removed outright, or lifted out of a loop and cached.


1 Answers

Using the "r" constraint forces the compiler to load the parameter into a scratch register before using that scratch register for one of your mov instructions. There simply aren't 4 scratch registers available.

Use the "g" constraint instead. This is more effiecient anyway, since the compiler will be able to access the argument directly in your mov instructions using a frame pointer offsetted memory access onto the destination register instead of doing that into a scratch register then moving the scratch register into the ultimate destination.

like image 52
Michael Burr Avatar answered Sep 24 '22 16:09

Michael Burr