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
How to know if my OS is 64bit or 32bit?
Re: How to know if my OS is 64bit or 32bit?
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
If that's what you mean?
EDIT: You could also check for e.g. the long mode enable bit.
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...................|
EDIT: You could also check for e.g. the long mode enable bit.
-
- Member
- Posts: 2566
- Joined: Sun Jan 14, 2007 9:15 pm
- Libera.chat IRC: miselin
- Location: Sydney, Australia (I come from a land down under!)
- Contact:
Re: How to know if my 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?how can we tell the OS is 64bit or 32bit
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: How to know if my OS is 64bit or 32bit?
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?
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?
Or evenNickJohnson 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; }
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?
Or portably:
Code: Select all
#include <limits.h>
#include <stdio.h>
int main(void)
{
printf("%d bits", sizeof(void*) * CHAR_BIT);
return 0;
}