【问题标题】:__asm block causing crash in Xcode 5__asm 块在 Xcode 5 中导致崩溃
【发布时间】:2015-05-03 13:43:19
【问题描述】:

我有一个小班来获取 CpuID 信息。 在类构造函数中,我有一个使用 asm 代码获取 cpuid 信息的内联函数。它在 Windows 中运行良好,在 Xcode 3 中运行良好,但现在类本身被破坏了。

这里是函数的开始:

inline void CCPUDetector::_DetectCPU()
{
    char lvendor[13] = "UnknownVendr";

    unsigned int lfamily;
    unsigned int lmodel;
    unsigned int lstepping;

    unsigned int lfeatures  = 0;
    unsigned int lBasicInfo = 0; 

    uint32_t cpu_cores = 0; 
    uint32_t cpu_count = 1;

    unsigned int signature = 0;

    // Step 1: check if a processor supports CPUID instruction
    // ============================================================
    try
    {
        __asm
        {
            // Reset all registers
            xor    eax, eax
            xor    ebx, ebx
            xor    ecx, ecx
            xor    edx, edx

            // Call CPUID with eax register == 0
            cpuid
        };
    }

    // Old processors that do not support cpuid will throw exception
    // which is caught in the command below "__except"
    catch(...)
    {
        return;
    }
}

在 __asm 块 CPUDetector 类本身之后,我在它的构造函数中的那个变得无效(NULL)。我尝试禁用不同的 Xor'scpuid,但每次都得到相同的结果。

有人可以建议我做错了什么吗?

【问题讨论】:

  • 你怎么知道是这段代码破坏了班级?
  • 您可能应该使用extended asm 并告诉编译器您销毁了寄存器eaxebxecxedx。否则,它可能会将其中一个用于重要的事情......请参阅answer with sample code
  • Ross,我在调试器中看到了。 “this”指针变为NULL,所有成员都是垃圾。
  • 小丑,你能指定我怎么知道编译器寄存器被破坏了吗?

标签: xcode assembly xcode5 clang cpuid


【解决方案1】:

clang 似乎不支持以这种方式处理 ASM。 最后,我只是使用了这两个文件中我需要的代码。

http://www.opensource.apple.com/source/xnu/xnu-1228.5.20/osfmk/i386/cpuid.h http://www.opensource.apple.com/source/xnu/xnu-1228.5.20/osfmk/i386/cpuid.c

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多