Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__asm__ in c++ error [duplicate]

I'm trying to read the cpuid information with the following cod but it doesn't work. I'm using Visual Studio 2010:

#include "stdafx.h"
#include <stdio.h>

int main()
{
  int a, b;

  for (a = 0; a < 5; a++)
  {
    __asm__("cpuid"
            :"=a"(b)                 // EAX into b (output)
            :"0"(a)                  // a into EAX (input)
            :"%ebx","%ecx","%edx");  // clobbered registers

    printf("The code %i gives %i\n", a, b);
  }

  return 0;
}

This is what it say:

'project_scs.exe': Loaded 'C:\Users\rares992\Documents\Visual Studio 2010\Projects\project_scs\Debug\project_scs.exe', Symbols loaded.
'project_scs.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Symbols loaded (source information stripped).
'project_scs.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Symbols loaded (source information stripped).
'project_scs.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Symbols loaded (source information stripped).
'project_scs.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'project_scs.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
The thread 'Win32 Thread' (0x2190) has exited with code -1073741510 (0xc000013a).
The program '[8828] project_scs.exe: Native' has exited with code -1073741510 (0xc000013a).

Can anyone tell what to do to make it run? Thanks

like image 994
user3731104 Avatar asked Jun 11 '14 23:06

user3731104


People also ask

What is asm function in C?

The asm statement allows you to include assembly instructions directly within C code. This may help you to maximize performance in time-sensitive code or to access assembly instructions that are not readily available to C programs. Note that extended asm statements must be inside a function.

What is asm volatile in C?

The __volatile__ modifier on an __asm__ block forces the compiler's optimizer to execute the code as-is. Without it, the optimizer may think it can be either removed outright, or lifted out of a loop and cached.

What is r in asm?

The lines with "r" or "=r" are operand constraints. The "=" means output operand. Essentially, this: :"=r"(y) :"r"(x) means that %0 (ie: the first operand) corresponds to y and is for output, and %1 (the second operand) corresponds to x.


2 Answers

The main issue is: You used AT&T Syntax as it is used for GCC. However, Visual Studio needs Intel Syntax. Look at my following example and look at the links in it:

#include <stdio.h>
#include <intrin.h>

#define EAX 0
#define EBX 1
#define ECX 2
#define EDX 3

int main ( void )
{
    int exx[4], a;

    // http://msdn.microsoft.com/en-us/library/4ks26t93%28v=vs.100%29.aspx
    puts ("Using VC inline assembler:");

    for (a = 0; a < 5; a++)
    {
        __asm
        {
            xor ecx, ecx            // needed for a=4
            mov eax, a
            cpuid
            mov exx[EAX], eax
        }
        printf("The code %i gives %08X\n", a, exx[EAX]);
    }

    // http://msdn.microsoft.com/en-us/library/vstudio/hskdteyh%28v=vs.100%29.aspx
    puts ("Using VC intrinsics:");

    for (a = 0; a < 5; a++)
    {
        __cpuid(exx, a);
         printf("The code %i gives %08X\n", a, exx[EAX]);
    }

    return 0;
}
like image 101
rkhb Avatar answered Oct 02 '22 01:10

rkhb


Here is what I use. It should work with MSVC, ICC, GCC, and Clang.

static inline void cpuid (int output[4], int functionnumber) {  
#if defined (_MSC_VER) || defined (__INTEL_COMPILER)       // Microsoft or Intel compiler, intrin.h included

    __cpuidex(output, functionnumber, 0);                  // intrinsic function for CPUID

#elif defined(__GNUC__) || defined(__clang__)              // use inline assembly, Gnu/AT&T syntax

   int a, b, c, d;
   __asm("cpuid" : "=a"(a),"=b"(b),"=c"(c),"=d"(d) : "a"(functionnumber),"c"(0) : );
   output[0] = a;
   output[1] = b;
   output[2] = c;
   output[3] = d;

#else                                                      // unknown platform. try inline assembly with masm/intel syntax

    __asm {
        mov eax, functionnumber
        xor ecx, ecx
        cpuid;
        mov esi, output
        mov [esi],    eax
        mov [esi+4],  ebx
        mov [esi+8],  ecx
        mov [esi+12], edx
    }

#endif
}
like image 25
Z boson Avatar answered Oct 02 '22 01:10

Z boson