在__asm​​__ C ++错误 [英] __asm__ in c++ error

查看:178
本文介绍了在__asm​​__ C ++错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阅读下列鳕CPUID信息,但它不工作。我使用Visual Studio 2010的:

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;
}

这就是它说的:

'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

推荐答案

下面是我使用。它应该与MSVC,ICC,GCC,以及锵工作。

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
}

这篇关于在__asm​​__ C ++错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆