In your book you show a method for getting the CPU speed that involves using a lot of inline assembler code. I thought there was a registry key you could read to get this information.
CPU speed
This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.
-
-
A registry key is easilly tweaked by anyone running REGEDIT. It's a better idea to detect it on your own...and there's nothing wrong with inline assembler.Mr.Mike
Author, Programmer, Brewer, Patriot -
FYI
The registry key where Windows stores the CPU's frequency is:
Source Code
- HKEY hKey = 0;
- LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
- _T("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
- 0, KEY_EXECUTE, &hKey);
- if (result == ERROR_SUCCESS)
- {
- DWORD dwType = 0;
- DWORD dwFreqMHz = 0;
- DWORD dwLength = sizeof(DWORD);
- result = RegQueryValueEx(hKey, _T("~MHz"), 0, &dwType,
- reinterpret_cast<LPBYTE>(&dwFreqMHz), &dwLength);
- if (result == ERROR_SUCCESS && dwType == REG_DWORD)
- {
- __int64 regFreqHz = dwFreqMHz * 1000000;
- // do stuff with frequency here...
- }
- RegCloseKey(hKey);
- }
-
Just curious - does this work on Win9x?Mr.Mike
Author, Programmer, Brewer, Patriot -
Dunno...don't have any win9x boxes lying around to check it out on.
Can anyone who runs win9x machines check and post the answer here. I'm curious myself too -
Originally posted by dpoon
Can anyone who runs win9x machines check and post the answer here. I'm curious myself too -
Ok here's a sample Win32 application that will read the CPU frequency from the value stored in the registry.
C Source Code
- #include <windows.h>
- #include <stdio.h>
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
- {
- HKEY hKey = 0;
- char text[256] = {0};
- const char *cpuRegKey = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
- LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, cpuRegKey, NULL, KEY_EXECUTE, &hKey);
- if (result == ERROR_SUCCESS)
- {
- DWORD dwType = 0;
- DWORD dwFreqMHz = 0;
- DWORD dwLength = sizeof(DWORD);
- result = RegQueryValueEx(hKey, "~MHz", NULL, &dwType, (LPBYTE)&dwFreqMHz, &dwLength);
- if (result == ERROR_SUCCESS && dwType == REG_DWORD)
- {
- sprintf(text, "CPU Frequency is %u MHz", dwFreqMHz);
- }
- else
- {
- sprintf(text, "Unable to open the DWORD value '~MHz'", cpuRegKey);
- }
- RegCloseKey(hKey);
- }
- else
- {
- sprintf(text, "Unable to open the registry key '%s'", cpuRegKey);
- }
- MessageBox(NULL, text, "CPU Frequency", MB_OK);
- return 0;
- }
-
Originally posted by mrmike
Just curious - does this work on Win9x?
I dare say not, since the referenced registry value doesn't exist at all on my WinME machine (I just looked). -
DSPOON: The alert shown by the code you posted was Unable to open the DWORD value '~MHz'
-
Yeah I had a feeling that the reg key wouldn't be there in Win9x machines. Oh well looks like the safest way is to calculate it by yourself using the timing code in the book or something similar.
-
Nothing "wrong" with it, but many find assembly intimidating to say the least.
Personally I think that a semester with the assembler should be required before any other languages can be taken. If you don't see what's happening under the hood it's hard to see what's really happening.
For anyone who hasn't used it at least a little, I suggest Assembly Language Step-by-Step by Jeff Dunteman. It's enough to give some perspective...."Your job is not to die for your country. Your job is to make some other poor sod die for his." -
From Mr. Mike on pg. 210
Calculating CPU Speed
You'd think that grabbing the CPU speed form a Wintel box would be as easy as reading the system information. It seems completely crazy that this value has to be calculated.
Were you meaning that you should be able to read a contstant value from the processor? If so, surely this wouldn't work with people overclocking their machines, having different case tempatures etc.
Originally posted by Nebuchadnezzar
For anyone who hasn't used it at least a little, I suggest Assembly Language Step-by-Step by Jeff Dunteman. It's enough to give some perspective....
That would be me, this one:
Assembly Language Step-by-step: Programming with DOS
or this one?:
Assembly Language: Step-By-Step (Coriolis Group Book)The post was edited 1 time, last by Gerry ().
-
I'm not sure I've seen the first. I was only aware of the one by Coriolis, now in it's second edition (which added some info on assembly under linux). I own the first edition of this one.
Other good ones include:
The Revolutionary Guide to Assembly Language - Wrox
Using Assembly Language - Que
Advanced Assembly Language - Que
Assembly Language for the PC - Brady
The Undocumented PC - Addison-Wesley
A sixth that I have in my library is Teach Yourself Assembler - MIS: Press, but this one is tougher to follow because all of its examples are too short to give a good feel for usage. Good introduction material I suppose, but I tihink Jeff's book is better in that regard. A decent quick reference since the mnemonic descriptions are concise and the examples are short.
And, to wrap it up - the Graphics Programming Black Book by Michael Abrash - Coriolis. This one is where you take all of that new assembly knowledge and watch the master put it to proper use.
In case you're wondering - no, I'm not an assembly guru. I have just enough knowledge to do interesting things with it, and not enough experience with it to prevent occasional re-formatting....
My problem with assembly is that it's far too easy to do catastrophic damage if you don't watch yourself. And since it's insanely fast you'll never catch it in time if something goes wrong."Your job is not to die for your country. Your job is to make some other poor sod die for his."The post was edited 2 times, last by Nebuchadnezzar ().
-
Originally posted by Gerry
From Mr. Mike on pg. 210
Calculating CPU Speed
You'd think that grabbing the CPU speed form a Wintel box would be as easy as reading the system information. It seems completely crazy that this value has to be calculated.
Were you meaning that you should be able to read a contstant value from the processor? If so, surely this wouldn't work with people overclocking their machines, having different case tempatures etc.
I read that as the system should calculate the processor speed either at install or at boot and store it. There is a notation of the processor speed in the System Information page - and it seems to be accurate whether overclocked or not. This info should be accessible, but it seems that the system grabs it and stores it in a local variable instead of a reg key somewhere. Wonder if anyone up at Redmond would care to give us a few pointers...."Your job is not to die for your country. Your job is to make some other poor sod die for his."
-
Share
- Facebook 0
- Twitter 0
- Google Plus 0
- Reddit 0