Convert inline Intel ASM to AT&T ASM with GCC Extended ASM

◇◆丶佛笑我妖孽 提交于 2019-12-02 13:29:01

问题


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?


回答1:


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



回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!