3

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;
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • If you're running 64-bit code, then you're running on a 64-bit processor. Easy. – Anon. Dec 16 '09 at 21:13
  • 1
    Anon: If you're running 32-bit code, you could be running on something such as a x86-64 architecture, not just a 32-bit one. – Rushyo Dec 16 '09 at 21:24

1 Answers1

6

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;
}
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
tyranid
  • 13,028
  • 1
  • 32
  • 34
  • 1
    you need ebx and edx clobbers for cpuid too. – Peeter Joot Nov 24 '12 at 15:21
  • 1
    @HolyBlackCat and \@Peeter: `"=d"` is the EDX output. You can't clobber an output operand, that won't compile. – Peter Cordes Jun 21 '18 at 23:21
  • 1
    You can also use constraints to give it the EAX input, with the side benefit that it will compile with `-masm=intel` https://godbolt.org/g/Ltsww1. Or better, use an existing `cpuid` wrapper function from `cpuid.h`. [How do I call "cpuid" in Linux?](https://stackoverflow.com/q/14266772) – Peter Cordes Jun 21 '18 at 23:28