在 AT&T 语法中,您可以执行类似 asm("mov %%eax, %0 ":"=r" (a[0])); 但不在英特尔语法中。 我想将这个 AT&T 语法翻译为 intel 语法,它获取 cpu 品牌名称并打印它。
int a[10];
void brandString(int eaxValues)
{
if (eaxValues == 1) {
__asm__("mov $0x80000002 , %eax\n\t");
}
else if (eaxValues == 2) {
__asm__("mov $0x80000003 , %eax\n\t");
}
else if (eaxValues == 3) {
__asm__("mov $0x80000004 , %eax\n\t");
}
__asm__("cpuid\n\t");
__asm__("mov %%eax, %0\n\t":"=r" (a[0]));
__asm__("mov %%ebx, %0\n\t":"=r" (a[1]));
__asm__("mov %%ecx, %0\n\t":"=r" (a[2]));
__asm__("mov %%edx, %0\n\t":"=r" (a[3]));
printf("%s", &a[0]);
}
void getCpuID()
{
__asm__("xor %eax , %eax\n\t");
__asm__("xor %ebx , %ebx\n\t");
__asm__("xor %ecx , %ecx\n\t");
__asm__("xor %edx , %edx\n\t");
printf("Brand string is ");
brandString(1);
brandString(2);
brandString(3);
printf("\n");
}
int main(){
getCpuID();
}
到目前为止,我已经做到了:
char a[10];
void brandString(int eaxValues)
{
if (eaxValues == 1) {
__asm {
mov eax, 0x80000002
}
}
else if (eaxValues == 2) {
__asm {
mov eax, 0x80000003
}
}
else if (eaxValues == 3) {
__asm {
mov eax, 0x80000004
}
}
__asm {
cpuid
mov a[0], eax
mov a[1], ebx
mov a[2], ecx
mov a[3], edx
}
printf("%s", &a[0]);
}
void getCpuID() {
__asm {
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
}
printf("Brand string is ");
brandString(1);
brandString(2);
brandString(3);
printf("\n");
}
但显然你不能做 mov a[0], eax 所以我被卡住了,我不知道该怎么做。
unsigned int regs[4];
char brand[49] = {0};
// Inline assembly to call CPUID with given eax value and store results in regs
#define cpuid(eax_val, regs) \
__asm__ __volatile__ ( \
"cpuid" \
: "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3]) \
: "a" (eax_val) \
);
// Call CPUID with 0x80000002, 0x80000003, and 0x80000004 and store the brand string
cpuid(0x80000002, regs);
memcpy(brand, regs, sizeof(regs));
cpuid(0x80000003, regs);
memcpy(brand + 16, regs, sizeof(regs));
cpuid(0x80000004, regs);
memcpy(brand + 32, regs, sizeof(regs));
printf(" Brand string is: %s\n\n", brand);