assembly 直接阅读asm cpuid

gab6jxml  于 2023-03-08  发布在  其他
关注(0)|答案(1)|浏览(127)

如何直接阅读本说明:

unsigned int eax, ebx, ecx, edx;
unsigned int leaf, subleaf;
unsigned int  intbuf[12];`
char *buffer;
int i,j,k,base,start,stop,length;
float freq_GHz;
float frequency;

subleaf=0;

base = 0;
for (leaf=0x80000002; leaf<0x80000005; leaf++) {
    
    __asm__ __volatile__ ("cpuid" : \
      "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (leaf), "c" (subleaf));

    intbuf[base] = eax;
    intbuf[base+1] = ebx;
    intbuf[base+2] = ecx;
    intbuf[base+3] = edx;
    base += 4;
}

我一直试着这样读它,但它不工作:

for (leaf = 0x80000002; leaf < 0x80000005; leaf++) {
        
        int regs[4];
        __cpuid(regs, leaf);
        
        intbuf[base] = (*regs),eax;
            intbuf[base + 1] = (*regs),ebx;
            intbuf[base + 2] = (*regs),ecx;
            intbuf[base + 3] = (*regs),edx;
        base += 4;
z18hc3ub

z18hc3ub1#

传递给__cpuid call 1的regs数组在返回时将依次具有四个寄存器EAXEBXECXEDX的值(例如,在数组元素regs[0]regs[3]中)。您可以使用 normal 数组操作符访问这些元素,并且不需要任何像eax这样的临时“register”变量。
因此,您的“纯”C++代码看起来像这样:

int intbuf[12];
int base = 0;
for (leaf = 0x80000002; leaf < 0x80000005; leaf++)
{        
    int regs[4];
    __cpuid(regs, leaf);    
    intbuf[base] = regs[0];     // EAX
    intbuf[base + 1] = regs[1]; // EBX
    intbuf[base + 2] = regs[2]; // ECX
    intbuf[base + 3] = regs[3]; // EDX
    base += 4;
}

如果你想把intbuf保存为unsigned int的数组,那么你应该在循环中给赋值语句添加一个强制类型转换,比如:

//...
    intbuf[base + 1] = static_cast<unsigned int>(regs[1]); // EBX

1此答案基于MSVC implementation of the __cpuid function的操作;其他编译器的版本可能略有不同,但一般原则可能保持不变。

相关问题