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

巧了我就是萌 提交于 2019-12-02 03:57:06

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.

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