cgccassemblyinline-assemblycpu-registers

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


I have this below code within a 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?
    );
}

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 thought that this would be the correct way to take parameters from C code to an inline assembly block.

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


Solution

  • 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.