0

I'm trying to get cpu info by embeding assembly into C++.

Inspirating comes from Microsoft code

enter image description here

But I guess code here should be different because I'm on macOS compiling with

CXXFLAGS = -g -Wall
LDLIBS   = -msse2
OBJS     = main.o  
PROGRAM  = main
$(PROGRAM): $(OBJS)
  $(CXX) $^ $(LDLIBS) -o $@

My code acctually is

#include <iostream>
#include <string>
#include <array>
#include <memory>
#include <x86intrin.h>
using namespace std;
string get_cpu_name() {
  uint32_t data[4] = { 0 };
  asm 
  (  
    "cpuid\n"
    "mov data[0], ebx"
  );
  return string((const char*)data);
}
int main() {
  cout << "hi" << endl; 
}

And the error

c++ -g -Wall   -c -o main.o main.cpp
main.cpp:15:6: error: unexpected token in argument list
    "mov data[0], ebx"
     ^
<inline asm>:2:9: note: instantiated into assembly here
mov data[0], ebx
        ^
1 error generated.
make: *** [main.o] Error 1

So question is around how to embed assembly code in C++ to get cpu info?

KcFnMi
  • 5,516
  • 10
  • 62
  • 136

0 Answers0