Titus
Titus

Reputation: 43

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. 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. And so on... Now I would like to execute upto 15 set of asm instruction in order to get complete CPUID output.

Ex:

        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. Thanks for the help.

Upvotes: 0

Views: 675

Answers (1)

ntshetty
ntshetty

Reputation: 1305

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

Upvotes: 2

Related Questions