您如何在运行时使用GCC和嵌入式asm检测CPU体系结构类型? [英] How do you detect the CPU architecture type during run-time with GCC and inline asm?

查看:91
本文介绍了您如何在运行时使用GCC和嵌入式asm检测CPU体系结构类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到CPU的体系结构类型。由于计算机正在运行syslinux,因此我无法访问/ proc / cpuinfo。我知道有一种使用内联ASM的方法,但是我认为我的语法是不正确的,因为我的变量iedx的设置不正确。

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.

使用ASM,绝不是专家。如果有人有任何提示或可以向我指出正确的方向,我将非常有义务。

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


推荐答案

您可以容纳多少个错误在这么少的行中;)

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

尝试

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

这篇关于您如何在运行时使用GCC和嵌入式asm检测CPU体系结构类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆