Page 1 of 1

How to know if my OS is 64bit or 32bit?

Posted: Sat Oct 17, 2009 1:16 am
by junkoi
Hi,

Given the value of all the x86 registers (general registers, segment registers, CR<n>, MSR, ...), how can we tell the OS is 64bit or 32bit?

Many thanks,
Jun

Re: How to know if my OS is 64bit or 32bit?

Posted: Sat Oct 17, 2009 1:58 am
by zity
If you're given at list of all the registers on a system, simply look for the largest register in use.

Take the ax-register for an example
rax = 64 bit
eax = 32 bi
ax = 16 bit

Code: Select all

|63..32|31..16|15-8|7-0|
               |AX.....|
       |EAX............|
|RAX...................|
If that's what you mean?

EDIT: You could also check for e.g. the long mode enable bit.

Re: How to know if my OS is 64bit or 32bit?

Posted: Sat Oct 17, 2009 5:31 am
by pcmattman
how can we tell the OS is 64bit or 32bit
Why would you need to? Can you possibly give us a little more information about what you're trying to achieve?

Re: How to know if my OS is 64bit or 32bit?

Posted: Sat Oct 17, 2009 6:36 am
by NickJohnson
If you are talking about your host operating system (or at least your host compiler, which is what matters), this will tell you:

Code: Select all

int main() {
 switch (sizeof(void*)) {
  case 1: printf("8 bit\n"); break;
  case 2: printf("16 bit\n"); break;
  case 4: printf("32 bit\n"); break;
  case 8: printf("64 bit\n"); break;
  default: printf("uh...\n");
 }
 return 0;
}

Re: How to know if my OS is 64bit or 32bit?

Posted: Sat Oct 17, 2009 9:28 am
by Fanael
If EFER.LMA is set, then it is 64-bit, else if CR0.PE is set and gdt_entry(CS).D is set then it is 32-bit, else it is 16-bit.

Re: How to know if my OS is 64bit or 32bit?

Posted: Sat Oct 17, 2009 9:44 am
by fronty
NickJohnson wrote:If you are talking about your host operating system (or at least your host compiler, which is what matters), this will tell you:

Code: Select all

int main() {
 switch (sizeof(void*)) {
  case 1: printf("8 bit\n"); break;
  case 2: printf("16 bit\n"); break;
  case 4: printf("32 bit\n"); break;
  case 8: printf("64 bit\n"); break;
  default: printf("uh...\n");
 }
 return 0;
}
Or even

Code: Select all

int main(void) {
        printf("%d bit\n", sizeof(void *) * 8);
        return 0;
}
;)

Re: How to know if my OS is 64bit or 32bit?

Posted: Sat Oct 17, 2009 12:47 pm
by Fanael
Or portably:

Code: Select all

#include <limits.h>
#include <stdio.h>
int main(void)
{
  printf("%d bits", sizeof(void*) * CHAR_BIT);
  return 0;
}
:P