Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Last active August 29, 2015 13:59
Show Gist options
  • Save unixpickle/10590987 to your computer and use it in GitHub Desktop.
Save unixpickle/10590987 to your computer and use it in GitHub Desktop.
Inspect x86 CPUID results
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
void print_chars(uint64_t num);
bool isvischar(char ch);
int main(int argc, const char * argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <RAX value>\n", argv[0]);
return 0;
}
uint64_t rbx, rdx, rcx;
__asm__("xor %%rdx, %%rdx\n"
"xor %%rcx, %%rcx\n"
"xor %%rbx, %%rbx\n"
"cpuid"
: "=b" (rbx), "=d" (rdx), "=c" (rcx)
: "a" (atoll(argv[1])));
printf("rbx=%08llx rdx=%08llx rcx=%08llx\n", rbx, rdx, rcx);
printf("rbx=");
print_chars(rbx);
printf(" rdx=");
print_chars(rdx);
printf(" rcx=");
print_chars(rcx);
printf("\n");
return 0;
}
void print_chars(uint64_t num) {
uint64_t i;
for (i = 0; i < 4; i++) {
char ch = (char)(num & 0xff);
if (isvischar(ch)) {
printf("%c", ch);
} else {
printf(".");
}
num >>= 8;
}
}
bool isvischar(char ch) {
if (ch >= 0x20 && ch < 0x7f) return true;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment