Created
September 13, 2012 16:21
-
-
Save bnoordhuis/3715514 to your computer and use it in GitHub Desktop.
print cpuid info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2012, Ben Noordhuis <info@bnoordhuis.nl> | |
* | |
* Permission to use, copy, modify, and/or distribute this software for any | |
* purpose with or without fee is hereby granted, provided that the above | |
* copyright notice and this permission notice appear in all copies. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
*/ | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
static void cpuid(uint32_t cmd, uint32_t vals[4]) | |
{ | |
__asm__ __volatile__ ( | |
"cpuid" | |
: "=a" (vals[0]), | |
"=b" (vals[1]), | |
"=c" (vals[2]), | |
"=d" (vals[3]) | |
: "a" (cmd) | |
); | |
} | |
static int istext(char c) | |
{ | |
return (c >= '0' && c <= '9') | |
|| (c >= 'A' && c <= 'Z') | |
|| (c >= 'a' && c <= 'z'); | |
} | |
static char totext(char c) | |
{ | |
return istext(c) ? c : '.'; | |
} | |
static const char *dec2text(uint32_t val, char buf[5]) | |
{ | |
buf[0] = totext(val & 255); | |
buf[1] = totext((val >> 8) & 255); | |
buf[2] = totext((val >> 16) & 255); | |
buf[3] = totext((val >> 24) & 255); | |
buf[4] = '\0'; | |
return buf; | |
} | |
static const char *dec2bin(uint32_t val, char buf[50]) | |
{ | |
int i, k; | |
for (i = 0, k = 31; k >= 0; i++, k--) { | |
buf[i] = '0' + !!(val & (1 << k)); | |
if (k != 0 && k % 8 == 0) buf[++i] = ' '; | |
} | |
buf[i] = '\0'; | |
return buf; | |
} | |
int main(int argc, char **argv) | |
{ | |
uint32_t vals[4] = { 0, 0, 0, 0 }; | |
char text[20]; | |
char bin[200]; | |
cpuid(argv[1] ? atoi(argv[1]) : 0, vals); | |
printf("eax %08x %s %s\n" | |
"ebx %08x %s %s\n" | |
"ecx %08x %s %s\n" | |
"edx %08x %s %s\n", | |
vals[0], dec2text(vals[0], text + 0), dec2bin(vals[0], bin + 0), | |
vals[1], dec2text(vals[1], text + 5), dec2bin(vals[1], bin + 50), | |
vals[2], dec2text(vals[2], text + 10), dec2bin(vals[2], bin + 100), | |
vals[3], dec2text(vals[3], text + 15), dec2bin(vals[3], bin + 150)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment