semi-colon / closed bracket error !?!

I'm encountering a problem involving an integer named __asm__. I declared it: int __asm__;
but then see build errors related to a missing semi-colon and closed bracket, however the code
looks right as far as that's concerned. Any suggestion?
This code is supposed to let me get and print my laptop's CPU ID.

#include <stdio.h>

void getPSN(char *PSN)
{
int __asm__;
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;
}
__asm__ isn't an integer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

void getPSN(char *PSN)
{
   int varEAX, varEBX, varECX, varEDX;
   char str[9];
   __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]);   
   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:
}



I think it will do the same as (edited)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;


string getPSN()
{
   int EAX, EBX, ECX, EDX;
   __asm__ __volatile__ ("cpuid" : "=a" (EAX), "=b" (EBX), "=c" (ECX), "=d" (EDX) : "a" (1));

   stringstream ss;
   #define SP << hex << uppercase << setfill( '0' ) << setw( 8 ) << 
   ss SP EAX SP EDX SP ECX;
   string s = ss.str();
   for ( int i = 1; i <= 5; i++ ) s.insert( 5 * i - 1, "-" );

   return s;
}


int main()
{
   cout << getPSN();
}
Last edited on
Thanks kindly for your example, which is neater code to look at. Unfortunately VS 2010 still takes issue with line 10:
__asm__ __volatile__ ("cpuid" : "=a" (EAX), "=b" (EBX), "=c" (ECX), "=d" (EDX) : "a" (1));
giving this error:
NewCode.cpp(10) : error C2065: '__asm__' : undeclared identifier
I'm afraid it is assembly language, so will be different in every implementation.

It works well with the gnu g++ compiler. It even works in CPP shell.

If you can't install a different compiler then you may have to try a different approach - all of them non-portable; e.g.
- get a relevant environment variable with getenv() - see http://www.cplusplus.com/reference/cstdlib/getenv/
- issue a system call like (on Windows):
1
2
3
4
5
6
7
#include <cstdlib>
using namespace std;

int main()
{
   system( "wmic cpu get ProcessorId" );
}

(although you might as well have done that from the command line).


If you are stuck with Visual Studio, then try googling '__cpuid' and '__cpuidex'.
Last edited on
"wmic cpu get ProcessorId" certainly isn't a command recognized on my Win7x64 machine.
wmic alone changed my root directory!? I'm guessing your system call example is for
Linux, right?

Apart from that curiosity, your other feedback was very useful, thanks kindly!
As I'm commited to building under MS VS for now, so I'll explore some of your
suggestions, such as finding out the Win7 equivalent for checking CPU serial #,
or exploring uses of getenv() or '__cpuid' and '__cpuidex'.
"wmic cpu get ProcessorId" certainly isn't a command recognized on my Win7x64 machine.


That's funny: it works fine (from the command line) on both Win7x64 machines I have access to.

'wmic' alone puts you in the wmic environment - which looks like it has changed directory (but hasn't). Type /? within this environment to get help. Type 'exit' to leave it and go back to your original environment.

From googling, '__cpuid' and '__cpuidex' seem appropriate for Visual Studio.
Last edited on
Topic archived. No new replies allowed.