assembly 在汇编指令中添加变量(asm inline C示例)

kxkpmulp  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(207)

我想执行asm行代码的循环。实际上我正在尝试读取我的核心的CPUID,需要读取CPUID的所有参数。如果我将0写入eax寄存器,然后我会得到制造商名称,如果我写1到eax,那么下一组CPUID信息将会收到。等等...现在,我想执行多达15组asm指令,以获得完整的CPUID输出。
例如:

unsigned int cpuid_reg[4];

        __asm__ volatile("mov $1, %eax");
        __asm__ volatile("cpuid" : "=a" (cpuid_reg[0]), "=b" (cpuid_reg[1]), "=c" (cpuid_reg[2]), "=d" (cpuid_reg[3]));
        printf("reg[0] 0x%x , cpuid_reg[1] 0x%x, cpuid_reg[2] 0x%x, cpuid_reg[3] 0x%x\n", cpuid_reg[0], cpuid_reg[1], cpuid_reg[2], cpuid_reg[3]);

        __asm__ volatile("mov $2, %eax");
        __asm__ volatile("cpuid" : "=a" (cpuid_reg[0]), "=b" (cpuid_reg[1]), "=c" (cpuid_reg[2]), "=d" (cpuid_reg[3]));
        printf("cpuid_reg[0] 0x%x , cpuid_reg[1] 0x%x, cpuid_reg[2] 0x%x, cpuid_reg[3] 0x%x\n", cpuid_reg[0], cpuid_reg[1], cpuid_reg[2], cpuid_reg[3]);

代替这两组代码,我需要一组,我可以写0到15到'eax'寄存器,以获得完整的CPUID数据了。谢谢帮助。

vmdwslir

vmdwslir1#

您可以读取类似thisthis的内容

static inline void cpuid(int code, uint32_t *a, uint32_t *d) {
  asm volatile("cpuid":"=a"(*a),"=d"(*d):"a"(code):"ecx","ebx");
}

/** issue a complete request, storing general registers output as a string
 */
static inline int cpuid_string(int code, uint32_t where[4]) {
  asm volatile("cpuid":"=a"(*where),"=b"(*(where+1)),
               "=c"(*(where+2)),"=d"(*(where+3)):"a"(code));
  return (int)where[0];
}

相关问题