question about how to declare something

I'm encountering a problem involving ?something? named __asm__. It's not an int,
and it's not a typical char string (can't declare it as such anyhow). The
build errors I see indicate a missing semi-colon and closed bracket, but the code looks right as far as that's concerned. Any suggestion(s) from people in
this forum about the correct way to declare __asm__ would be appreciated, and
how to ultimately resolve the few build errors I get. It builds under VS C++.
This code is supposed to let me get and print my laptop's CPU ID.

#include <stdio.h>

void getPSN(char *PSN)
{
char __asm__[30];
int varEAX, varEBX, varECX, varEDX;
char str[9];

//%eax=1 gives most significant 32 bits in eax
__asm__ __volatile__ ("cpuid" : "=a" (varEAX), "=b" (varEBX), "=c" (varECX), "=d" (varEDX) : "a" (1));
//sprintf(str, "%08X", varEAX); //i.e. XXXX-XXXX-xxxx-xxxx-xxxx-xxxx
//sprintf(PSN, "%C%C%C%C-%C%C%C%C", str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
// //%eax=3 gives least significant 64 bits in edx and ecx [if PN is enabled]
//__asm__ __volatile__ ("cpuid" : "=a" (varEAX), "=b" (varEBX), "=c" (varECX), "=d" (varEDX) : "a" (3));
//sprintf(str, "%08X", varEDX); //i.e. xxxx-xxxx-XXXX-XXXX-xxxx-xxxx
//sprintf(PSN, "%s-%C%C%C%C-%C%C%C%C", PSN, str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
//sprintf(str, "%08X", varECX); //i.e. xxxx-xxxx-xxxx-xxxx-XXXX-XXXX
//sprintf(PSN, "%s-%C%C%C%C-%C%C%C%C", PSN, str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
}

int main()
{
char PSN[30]; //24 Hex digits, 5 '-' separators, and a '\0'
getPSN(PSN);
printf("%s\n", PSN); //compare with: lshw | grep serial:
return 0;
}
I'm encountering a problem involving ?something? named __asm__

The contents of the string inside the __asm__ block is a small program written in an extended assembler dialect. Normally __asm__ is written asm.

These non-standard notations (and obviously the meaning of the contents of the asm-declaration) differ between implementations. You need not declare it, and it's a reserved identifier anyway.
__volatile__ in this context means very roughly the same thing as the language keyword volatile on a declaration (i.e., it serves as an optimization barrier). See:
http://en.cppreference.com/w/cpp/language/asm
Last edited on
Helpful feedback, thank-you!
if this isn't working, we used to have to call the "emit" assembler function to run some of the internal / unusual cpu instructions. You literally code up the cpu instruction yourself in hex, and send that directly to the CPU, bypassing even the assembly language level. This sounds like one of "those" instructions, but I am not 100% sure. IF you mess up, the system has a good chance of locking up entirely, so this is a last resort if the rest of it just won't work right.

and in the total other direction, I am pretty sure windows has a library call to get the CPU and machine ID codes. They use these for some of their copy protections on the OS, many programs do (esp M$ programs). I think its a standard bit of data. Under the hood, its probably doing the asm call, but it may be cleaner to let them do that...
Last edited on
Topic archived. No new replies allowed.