Hey, how can i mov data into my struct? I'm trying to get info about my CPU with the CPUID function, using inline asm.
But i can't get the string of the vendor in my struct. I also don't know how to set a bool value in my struct either.

This is what i have right now:

typedef struct CPUINFO_TYP
{
char cpuName[48];
bool bSSE;
} CPUINFO;

CPUINFO GetCPUInfo ( ) {
CPUINFO info;

__asm
{
xor eax, eax

mov eax, 0
CPUID

//Get vendor name
mov esi,   // ERROR
mov , ebx
mov , edx
mov , ecx

//Is there support for SSE ??
mov eax, 1
test edx, 02000000h
jz __NOSSE
mov , 1 // set bool to TRUE, but when i print it it contains the value 204

__NOSSE:

}

return info;
}


Please check the comments in the code where i'm having trouble. Can someone please tell me how i can put the value i get from the ASM code into my struct?
Posted on 2010-02-16 13:53:37 by w00
w00,

Try to step through the code.
Posted on 2010-02-16 18:39:42 by baldr
Instead of
mov esi, 

Try something like
 lea esi, 
Posted on 2010-02-16 21:52:34 by ti_mo_n

Instead of
mov esi, 

Try something like
 lea esi, 



Thanks! That works for the string. Now all i''m left with is the boolean value. I can't seem to set that to TRUE.

mov , 1  //not working
mov info.bSSE, 1    //also not working

Any idea how to fix that?
@baldr
I'm really not that good im ASM. I've been looking at thew code for quite some time now. I really have no idea how to set a boolean value with inline ASM.
Posted on 2010-02-17 03:07:58 by w00

Thanks! That works for the string. Now all i''m left with is the boolean value. I can't seem to set that to TRUE.

mov , 1  //not working
mov info.bSSE, 1    //also not working

Any idea how to fix that?


I think it's because it doesn't know the size of '1'.
I believe a bool in VC++ is 32 bit, so then you can do:
mov dword ptr , 1

The 'dword ptr' will tell it that it's a dword.
If that doesn't work, then 'bool' is probably a byte, so try 'byte ptr' instead.

Edit: I think the real problem is twofold:
1) You don't set the value to 0 in case the test for SSE fails, so it will be a random value.
2) How do you try to print the bool? Afaik there's no regular way to print a boolean value with printf() etc. So it could go wrong there aswell. You could do something like: info.bSSE ? "true" : "false".
Posted on 2010-02-17 03:36:03 by Scali

Scali, I am suprised you did not know this.

BOOL is defined as (unsigned) INT, which technically, is the native word size of the machine - on a 64-bit system, it would be 64 bits.
I find this rather odd, since BOOL is also defined as FALSE = 0 and TRUE = !ZERO - one bit is enough, why they chose INT is anyones guess, but not only do we persist in this nonsense, but we are perpetuating it.
Posted on 2010-02-17 04:19:21 by Homer
Thanks, it worked with mov byte ptr :)
Posted on 2010-02-17 04:37:33 by w00
I would be careful about that assumption.
Chances are that one byte worked because the next three bytes were zero.
BOOL is NOT defined to be one byte - however it SHOULD be in my opinion.
Posted on 2010-02-17 04:44:32 by Homer


Scali, I am suprised you did not know this.

BOOL is defined as (unsigned) INT, which technically, is the native word size of the machine - on a 64-bit system, it would be 64 bits.
I find this rather odd, since BOOL is also defined as FALSE = 0 and TRUE = !ZERO - one bit is enough, why they chose INT is anyones guess, but not only do we persist in this nonsense, but we are perpetuating it.



There is a difference between BOOL (a Win32 datatype, defined in one of the Windows headers) and bool (a built-in C++ type).
BOOL is always the same size (else it would be incompatible with the Win32API), bool can vary from one compiler to the next. I was talking about bool, since that's what he was using in his code, not BOOL.


Also, an int is not necessarily the native word size of the machine. In x64 code, an int is still 32-bit, and there is a special __int64 type for a 64-bit integer (or long long with various other compilers... See my CPUInfo headers to see how I work around bool and int32/int64 issues in a compiler and OS-neutral way).
Posted on 2010-02-17 05:13:14 by Scali
microsoft defines int as 32 bits on both 32 and 64 bit systems, and yet I am lead to believe that BOOL is defined as 64 bits on a 64 bit os - which follows the c++ definition of a boolean, being an int, which is machine word size, so which is it?

Posted on 2010-02-17 05:47:28 by Homer

microsoft defines int as 32 bits on both 32 and 64 bit systems, and yet I am lead to believe that BOOL is defined as 64 bits on a 64 bit os - which follows the c++ definition of a boolean, being an int, which is machine word size, so which is it?


1) BOOL is typedef-ed as int in both 32-bit and 64-bit mode. Therefore it is 32-bit on a 64-bit OS aswell.
2) There is no C++ definition for the size of a boolean (or any other type, for that matter). The size of a datatype in C++ can vary per compiler, architecture and OS.
3) An int is not machine word size in 64-bit Windows as a result of 2).
4) The code posted by w00 does not use BOOL anywhere, it uses bool. So the size of a BOOL is not relevant to his problem.
This is more relevant to him.
Microsoft Specific
In Visual C++4.2, the Standard C++ header files contained a typedef that equated bool with int. In Visual C++ 5.0 and later, bool is implemented as a built-in type with a size of 1 byte. That means that for Visual C++ 4.2, a call of sizeof(bool) yields 4, while in Visual C++ 5.0 and later, the same call yields 1. This can cause memory corruption problems if you have defined structure members of type bool in Visual C++ 4.2 and are mixing object files (OBJ) and/or DLLs built with the 4.2 and 5.0 or later compilers.
Posted on 2010-02-17 06:02:23 by Scali

I would be careful about that assumption.
Chances are that one byte worked because the next three bytes were zero.
BOOL is NOT defined to be one byte - however it SHOULD be in my opinion.



So what are you saying, i was lucky that it worked?

I also wanted to know if my code worked on a x64 OS.. But now im not really sure if the bool part is working at all...
Posted on 2010-02-17 07:16:09 by w00
I also wanted to know if my code worked on a x64 OS..

Once compiled, 32-bit code works the same way both on a 32-bit and a 64-bit OS (well, most of the time, at least ;p). Differences may only arise between 32-bit code and 64-bit code - not between OSes they run on.

But now im not really sure if the bool part is working at all...

Why won't you debug the code?
Posted on 2010-02-17 07:24:03 by ti_mo_n

So what are you saying, i was lucky that it worked?


In a way, yes.
In general it is not recommended to use the native C++ bool type when you mix with other languages, such as C or assembly.
It's better to use a type with a wellknown size (which is probably the reason why Microsoft always stuck with their BOOL datatype, even in C++ APIs such as MFC).

I also wanted to know if my code worked on a x64 OS.. But now im not really sure if the bool part is working at all...


Did you notice my edit that you don't set the value to 0 beforehand? Any nonzero value will be considered as 'true' by the compiler, so it will not work correctly on a non-SSE machine (also, it seems you set eax to 1, but then there is no cpuid instruction following it, to retrieve the values).
Aside from that, if you are using Microsoft Visual C++, it will not support inline assembly at all, for x64 targets.
In order to work around this limitation, Microsoft has supplied the __cpuid() intrinsic function:
http://msdn.microsoft.com/en-us/library/hskdteyh(VS.80).aspx
My CPUInfo library also makes use of this (http://cpuinfo.sourceforge.net), and compiles to 32-bit and 64-bit versions of Windows, Linux, FreeBSD and OS X. CPUID itself works the same in 32-bit and 64-bit mode.
Posted on 2010-02-17 07:36:03 by Scali
hanks, ill have another look at it when im home. doing it all from a phone right now.
Posted on 2010-02-17 07:48:00 by w00
Homer, take a look at the table at 64-bit data models :)

#include <windows.h>
#include <stdio.h>

int main()
{
printf("sizeof(void*): %d\n", sizeof(void*));
printf("sizeof(BOOL): %d\n", sizeof(BOOL));
printf("sizeof(bool): %d\n", sizeof(bool));
printf("sizeof(int): %d\n", sizeof(int));
}


R:\temp>64bit
sizeof(void*): 8
sizeof(BOOL): 4
sizeof(bool): 1
sizeof(int): 4


And, slightly edited source:
f0dder@ayanami ~ $ file a.out
a.out: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
f0dder@ayanami ~ $ ./a.out
sizeof(void*): 8
sizeof(bool): 1
sizeof(int): 4
Posted on 2010-02-18 06:06:55 by f0dder