Help with CPUID

[b][red]This message was edited by Mike85 at 2002-8-31 4:35:5[/red][/b][hr]
I'm doing a little program that shows some information about CPU and I have a little problem with CPUID instruction. After I have used CPUID inctruction, accessing to variables or properties (either via assembler or in pascal) causes access violation.

Here's the code:

procedure GetCPUVendor;
var
EBX,EDX,ECX: Cardinal;
VendStr: string;
begin
asm
mov eax,0
cpuid
mov &ebx,ebx
mov &edx,edx
mov &ecx,ecx
end;
SetLength(VendStr,12);
Move(EBX,VendStr[1],4);
Move(EDX,VendStr[5],4);
Move(ECX,VendStr[9],4);
Form1.VendLabel.Caption := VendStr;//<-Error here!
end;

I've already tryed almost everything, pusing all registers to stack before calling cpuid, and then popping them back and calling cpuid in separated procedure but it didn't help.
However this code works fine, and shows my CPU's vendor string: "AuthenticAMD" in message box:

procedure GetCPUVendor;
var
EBX,EDX,ECX: Cardinal;
VendStr: string;
begin
asm
mov eax,0
cpuid
mov &ebx,ebx
mov &edx,edx
mov &ecx,ecx
end;
SetLength(VendStr,12);
Move(EBX,VendStr[1],4);
Move(EDX,VendStr[5],4);
Move(ECX,VendStr[9],4);
ShowMessage(VendStr);
end;

So it seems that the bug is on line "Form1.VendLabel.Caption := VendStr;". Form1 is created, VendLabel is created and Caption can be accessed normally before I call cpuid, but after cpuid, accessing caption causes error. Could anyone please tell me what I am doing wrong?


Comments

  • : [b][red]This message was edited by Mike85 at 2002-8-31 4:35:5[/red][/b][hr]
    : I'm doing a little program that shows some information about CPU and I have a little problem with CPUID instruction. After I have used CPUID inctruction, accessing to variables or properties (either via assembler or in pascal) causes access violation.
    :
    : Here's the code:
    :
    : procedure GetCPUVendor;
    : var
    : EBX,EDX,ECX: Cardinal;
    : VendStr: string;
    : begin
    : asm
    : mov eax,0
    : cpuid
    : mov &ebx,ebx
    : mov &edx,edx
    : mov &ecx,ecx
    : end;
    : SetLength(VendStr,12);
    : Move(EBX,VendStr[1],4);
    : Move(EDX,VendStr[5],4);
    : Move(ECX,VendStr[9],4);
    : Form1.VendLabel.Caption := VendStr;//<-Error here!
    : end;
    :
    : I've already tryed almost everything, pusing all registers to stack before calling cpuid, and then popping them back and calling cpuid in separated procedure but it didn't help.
    : However this code works fine, and shows my CPU's vendor string: "AuthenticAMD" in message box:
    :
    : procedure GetCPUVendor;
    : var
    : EBX,EDX,ECX: Cardinal;
    : VendStr: string;
    : begin
    : asm
    : mov eax,0
    : cpuid
    : mov &ebx,ebx
    : mov &edx,edx
    : mov &ecx,ecx
    : end;
    : SetLength(VendStr,12);
    : Move(EBX,VendStr[1],4);
    : Move(EDX,VendStr[5],4);
    : Move(ECX,VendStr[9],4);
    : ShowMessage(VendStr);
    : end;
    :
    : So it seems that the bug is on line "Form1.VendLabel.Caption := VendStr;". Form1 is created, VendLabel is created and Caption can be accessed normally before I call cpuid, but after cpuid, accessing caption causes error. Could anyone please tell me what I am doing wrong?
    :
    Perhaps this is a hick-up of Delphi itself. Try to change the GetCPUVendor into a function and copy VendStr to the Result.
  • Thanks, but putting it into function didn't help :(
    This is getting even stranger, it seems that Delphi's optimizer messes up my code. The procedure works fine now when optimization is disabled. So if you ever get access violations without reason, try to disable optimization.
  • You are So correct, I must say. Even I have faced a similar problem. I had a routine in a .DLL file, and every time I called it, it would give an access violation. But, when I disabled optimizations, it worked perfect!!! The most amazing thing is that its written very clearly in the Delphi documentation that optimizations will not affect the code logic in any ways, but in many cases it does.



    : Thanks, but putting it into function didn't help :(
    : This is getting even stranger, it seems that Delphi's optimizer messes up my code. The procedure works fine now when optimization is disabled. So if you ever get access violations without reason, try to disable optimization.
    :

  • [green]

    Undefined symbol 'cpuid' is what i got when i tried your code in C++ Builder 5...any solutions ? Can I use the actual binary value in place
    of the mnemonic 'cpuid' ? If so, what's the code for cpuid ??

    haaalllpppp :-)

    [/green]
    [size=2][red]

    [b]Bikram[/b][/red]
    [blue]http://www.geocities.com/nv5050[/blue]
    [red]**************************************[/red][/size]

  • It seems that C++ Builder 5 doesn't reconize the CPUID instruction. If I remember right neither do Delphi 5, but Delphi 6 reconizes.

    You can use the binary value of inctruction, it's A20F. So just use this line instead cpuid:

    dw A20Fh
  • : It seems that C++ Builder 5 doesn't reconize the CPUID instruction. If I remember right neither do Delphi 5, but Delphi 6 reconizes.
    :
    : You can use the binary value of inctruction, it's A20F. So just use this line instead cpuid:
    :
    : dw A20Fh
    :

    [size=2][red]

    [b]Bikram[/b][/red]
    [blue]http://www.geocities.com/nv5050[/blue]
    [red]**************************************[/red][/size]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories