How do I tell GCC asm that an input register is clobbered?

别等时光非礼了梦想. 提交于 2019-12-29 07:48:06

问题


I'm trying to do a 64=32x32 multiply via the x86 mul instruction, but I only need the high dword of the result (the edx register). So naturally, I tried listing edx as an output register and eax as a clobbered register.

This seems natural to me, but eax is also an input register. When I try to tell GCC that eax is clobbered, it gives an error message.

__asm__("mull\t%2" : "=d"(div10) : "%a"(UINT32_C(0x1999999A)), "r"(number)
    : "cc", "rax");

If I try that, it throws this error message:

divmod10.cpp:76:91: error: can’t find a register in class ‘AREG’ while reloading
‘asm’
divmod10.cpp:76:91: error: ‘asm’ operand has impossible constraints

Omitting it compiles, but breaks the code. GCC ends up relying upon eax not being clobbered, which is incorrect:

        movl    $429496730, %eax
#APP
# 76 "divmod10.cpp" 1
        mull    %esi
# 0 "" 2
#NO_APP
        movl    %edx, %esi
#APP
# 78 "divmod10.cpp" 1
        mull    %edx
# 0 "" 2
#NO_APP

How do I do what I want?


回答1:


Just make a useless temp for the output to go into and the compiler will optimize it out. For example:

__asm__("mull\t%2" : "=d"(div10), "=a"((int){0})
    : "a"(UINT32_C(0x1999999A)), "r"(number) : "cc");

That's the easiest way I know to handle clobbered inputs.



来源:https://stackoverflow.com/questions/26942064/how-do-i-tell-gcc-asm-that-an-input-register-is-clobbered

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