如何在运行时使用 GCC 和内联汇编检测 CPU 架构类型?

发布于 2024-08-14 19:18:41 字数 426 浏览 4 评论 0原文

我需要找到CPU的架构类型。我无法访问 /proc/cpuinfo,因为机器正在运行 syslinux。我知道有一种方法可以使用内联 ASM 来做到这一点,但是我相信我的语法不正确,因为我的变量 iedx 没有正确设置。

我正在与 ASM 打交道,但绝不是专家。如果有人有任何提示或可以指出我正确的方向,我将非常感激。

static int is64Bit(void) {
    int iedx = 0;
    asm("mov %eax, 0x80000001");
    asm("cpuid");
    asm("mov %0, %%eax" : : "a" (iedx));
    if ((iedx) && (1 << 29))
    {
        return 1;
    }
    return 0;
}

I need to find the architecture type of a CPU. I do not have access to /proc/cpuinfo, as the machine is running syslinux. I know there is a way to do it with inline ASM, however I believe my syntax is incorrect as my variable iedx is not being set properly.

I'm drudging along with ASM, and by no means an expert. If anyone has any tips or can point me in the right direction, I would be much obliged.

static int is64Bit(void) {
    int iedx = 0;
    asm("mov %eax, 0x80000001");
    asm("cpuid");
    asm("mov %0, %%eax" : : "a" (iedx));
    if ((iedx) && (1 << 29))
    {
        return 1;
    }
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月光色 2024-08-21 19:18:41

这么少的行可以容纳多少错误;)

尝试一下

static int is64bit(void) {
        int iedx = 0;
        asm volatile ("movl $0x80000001, %%eax\n"
                "cpuid\n"
        : "=d"(iedx)
        : /* No Inputs */
        : "eax", "ebx", "ecx"
        );

        if(iedx & (1 << 29))
        {
                return 1;
        }
        return 0;
}

How many bugs can you fit in so few lines ;)

Try

static int is64bit(void) {
        int iedx = 0;
        asm volatile ("movl $0x80000001, %%eax\n"
                "cpuid\n"
        : "=d"(iedx)
        : /* No Inputs */
        : "eax", "ebx", "ecx"
        );

        if(iedx & (1 << 29))
        {
                return 1;
        }
        return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文