I've spent the last 2 days to study AT&T inline assembly, but I'm having some problems converting this one:
static char vendername[50] = {0};
_asm {
mov eax, 0
cpuid
mov dword ptr [vendername], ebx
mov dword ptr [vendername+4], edx
mov dword ptr [vendername+8], ecx
}
Here is my try:
static char vendername[50] = {0};
__asm__(
"movl $0,%%eax \n"
"cpuid \n"
"movl %%ebx, %[vendername] \n"
"movl %%edx, %[vendername+$4] \n"
"movl %%ecx, %[vendername+$8] \n"
:"=r"(vendername) //user vendername as output
:[vendername]"m"(vendername) //use vendername as input
:"eax","ebx","edx","ecx" // clobbers those regs
);
This code doesn't work, gcc give me the following errors:
error: undefined named operand ‘vendername+4’
and
error: undefined named operand ‘vendername+8’
How do I convert the code from Intel assembly to AT&T assembly?
You can do it as:
#include <stdio.h>
int main(int argc, char **argv) {
static char vendername[50] = {0};
__asm__ __volatile__ (
"movl $0, %%eax\n"
"cpuid\n"
"movl %%ebx, %0\n"
"movl %%edx, %0 + 4\n"
"movl %%ecx, %0 + 8\n"
:"=m"(vendername)
:
:"eax", "ebx", "edx", "ecx"
);
printf("%s\n", vendername);
return 0;
}
Rule of thumb is, if you ever write a mov
in inline asm you are probably doing it wrong :)
The compiler can load/store values for you on its own, ie.
int dummy;
union {
char text[12];
struct {
int ebx;
int edx;
int ecx;
};
} vendorname;
__asm__(
"cpuid \n"
: "=b" (vendorname.ebx), "=d" (vendorname.edx), "=c" (vendorname.ecx), "=a" (dummy)
: "a" (0)
);
Note that the case was complicated by having to interpret the 3 dwords as a string.
来源:https://stackoverflow.com/questions/19411410/convert-inline-intel-asm-to-att-asm-with-gcc-extended-asm