Inline ASM with class variables

Help and discussing programming in C and C++.

Moderator:Moderators

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:
Inline ASM with class variables

Post by Andyhhp » Sat Mar 01, 2008 2:35 pm

Hi,

I am trying to write some OO code that will use the cpuid instruction to determine the SIMD support of a processor.

I have some code what works fine when in a traditional C style but when I put in into a C++ class, I get stack corruption.

Here is the relevant code:

Cpuid.h

Code: Select all

#ifndef _CPUID_H_
#define _CPUID_H_

class CpuInfo
{
public:
	CpuInfo();
	~CpuInfo();

	bool Scan();

	const char* Vendor();

protected:
private:
	union{
		struct { unsigned long b,d,c; } regs;
		char str[13];
	} vendor;
};


#endif
Cpuid.cpp

Code: Select all

#include "cpuid.h"
#include "string.h"

CpuInfo::CpuInfo()
{
	memset(this,0,sizeof(CpuInfo));
}

CpuInfo::~CpuInfo()
{

}

bool CpuInfo::Scan()
{
	_asm
	{
		pusha
		xor eax,eax
		cpuid


		mov [this]CpuInfo.vendor.regs.b,ebx
		mov [this]CpuInfo.vendor.regs.c,ecx
		mov [this]CpuInfo.vendor.regs.d,edx
		popa
	}
	return true;
}

const char * CpuInfo::Vendor()
{
	return vendor.str;
}
Does anyone know why exactly the same union outside the class works but the one inside the class doesnt?

Thanks,

Andrew
Image

Post Reply