How does gcc know the register size to use in inline assembly?

六眼飞鱼酱① 提交于 2021-01-04 05:47:29

问题


I have the inline assembly code:

#define read_msr(index, buf)    asm volatile ("rdmsr" : "=d"(buf[1]), "=a"(buf[0]) : "c"(index))

The code using this macro:

u32 buf[2];

read_msr(0x173, buf);

I found the disassembly is(using gnu toolchain):

mov    eax,0x173
mov    ecx,eax
rdmsr  
mov    DWORD PTR [rbp-0xc],edx
mov    DWORD PTR [rbp-0x10],eax

The question is that 0x173 is less than 0xffff, why gcc does not use "mov cx, 0x173"? Will the gcc analysis the following instruction "rdmsr"? Will the gcc always know the correct register size?` re


回答1:


It depends on the size of the value or variable passed.

If you pass a "short int" it will set "cx" and read the data from "ax" and "dx" (if buf is a short int, too).

For char it would access "cl" and so on.

So "c" refers to the "ecx" register, but this is accessed with "ecx", "cx", or "cl" depending on the size of the access, which I think makes sense.

To test you can try passing (unsigned short)0x173, it should change the code.

There is no analysis of the inline assembly (in fact it is after text substitution direclty copied to the output assembly, including syntax errors). Also there is no default register size, depending on whether you have a 32 or 64 bit target. This would be way to limiting.




回答2:


I think the answer is because the current default data size is 32-bit. In 64-bit long mode, the default data size is also 32-bit, unless you use "rex.w" prefix.




回答3:


Intel specifies the RDMSR instruction as using (all of) ECX to determine the model specific register. That being the case, and apparently as specified by your macro, GCC has every reason to load your constant into the full ECX.

So the question about why it doesn't load CX seems completely inappropriate. It looks like GCC is generating the right code.

(You didn't ask why it stages the load of ECX inefficiently by using EAX; I don't know the answer to that).



来源:https://stackoverflow.com/questions/24996601/how-does-gcc-know-the-register-size-to-use-in-inline-assembly

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