简体   繁体   English

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

[英]Add variable in assembly instruction (asm inline C example)

I would like to execute loop of asm lines of code. 我想执行循环汇编代码行。 Actually I am trying to read the CPUID of my core, need to read all the parameters of CPUID. 实际上我正在尝试读取内核的CPUID,需要读取CPUID的所有参数。 If I write 0 to eax register, then I will get the Manufacturer name and if I write 1 to eax then next set of CPUID information will received. 如果将0写入eax寄存器,则将获得制造商名称;如果将1写入eax,则将接收下一组CPUID信息。 And so on... Now I would like to execute upto 15 set of asm instruction in order to get complete CPUID output. 依此类推...现在,我想执行多达15组的asm指令,以获得完整的CPUID输出。

Ex: unsigned int cpuid_reg[4]; 例如: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]);

Instead of these two sets of code, I need to one set where I can write 0 to 15 into 'eax' register to get the complete CPUID data out. 代替这两组代码,我需要一组代码,可以在其中向eax寄存器写入0到15,以获取完整的CPUID数据。 Thanks for the help. 谢谢您的帮助。

You can read something like this and this 您可以阅读像这样这个

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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM