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

六眼飞鱼酱① 提交于 2019-12-08 20:40:14

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/25579759/gcc-inline-assembly-what-does-asm-operand-has-impossible-constraints-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!