For a code I'm working on, I need to know how to use the architecture of the CPU directly. I don't think it is possible in C++, but assembly is really difficult, so I just want to make sure.
For a code I'm working on, I need to know how to use the architecture of the CPU directly. I don't think it is possible in C++, but assembly is really difficult, so I just want to make sure.
Last edited by AdamLAN; 06-16-2005 at 07:36 AM.
You can use inline assembly in c++ using the keyword asm, but to answer your question, c++ can't directly access the cpu architecture.
One of the benefits of C++ is that it insulates you from the hardware. You can write a (ANSI/ISO standard) C++ program without knowing anything about the hardware.
With standard C++, there is no way to "get inside" the CPU or read/write a particular memory location. Well, there are "tricks" you can to do with pointers to read/write memory or other hardware, but they will not work with Windows.
In general, you need a Driver library (or some other special I/O library) with some non-standard functions to control and access hardware. If you are working on an embedded system, the compiler will include this stuff.
Assuming you're running Windows*, the operating system blocks user-mode programs from directly accessing hardware. You have to write a kernel-mode driver (or a program that "looks like" a driver to the operating system). In order to do this, you need the Windows Driver SDK (Software Development Kit). For example, you if you wanted to write a program that uses 100% of the CPU (and blocks multitasking), you'd have to write a driver.
The only way to get complete access and control of the CPU is with assembly. Assembly language is essentially a human readable representation of the CPU's native machine language.
* Windows 98 and below do not block hardware access.
Last edited by DougDbug; 06-16-2005 at 12:51 PM.
I know, but for my program I need to use the CPU architecture. So it's not really a benefit to me :-)One of the benefits of C++ is that it insulates you from the hardware. You can write a (ANSI/ISO standard) C++ program without knowing anything about the hardware.
Thanks, that is helpful, but I wouldn't know where to begin!Assuming you're running Windows*, the operating system blocks user-mode programs from directly accessing hardware. You have to write a kernel-mode driver (or a program that "looks like" a driver to the operating system). In order to do this, you need the Windows Driver SDK (Software Development Kit). For example, you if you wanted to write a program that uses 100% of the CPU (and blocks multitasking), you'd have to write a driver.
How do I use keyword asm? What's the syntax?
Last edited by AdamLAN; 06-16-2005 at 01:47 PM.
for gcc is like this..
http://www-106.ibm.com/developerworks/library/l-ia.html
I honestly don't understand it !!! to stupid, from my point of view... has almost nothing to do with raw assembly
for microsoft compilers its very very easy. (masm syntax)
Code:#include<iostream> __declspec(naked) int foo(int){ __asm{ mov eax, dword ptr [esp+4]; add eax, 3 ret } } int main(){ int retv=10; std::cout<<"ret is "<<retv<<std::endl; __asm{ push retv call foo add esp,4 mov retv, eax } std::cout<<"ret is "<<retv<<std::endl; return 0; }
> but for my program I need to use the CPU architecture
Perhaps if you explained your "need" better, we could help.
Simply for the hell of it, or to squeeze out a few extra cycles isn't a good reason.
As Doug says, the whole point of a high level language is to avoid getting bogged down in the detail.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I'm making an experimental program that puts random numbers into each register constantly, and randomly deletes them or sets a flag, or performs an operation on them. I would like to see what happens if the program is left alone for a long time. I'll also make a simple kernel that runs it, so there is no OS to interfere.Perhaps if you explained your "need" better, we could help.
Very funny, by the way, I love star trek.Do it yourself Borg kit - some self assimilation required.
If you're careful about which registers you use, you shouldn't get any unexpected results. If you're not careful, it will crash* faster than you can say "microsecond"to see what happens if the program is left alone for a long time.
The program should be easy for anyone with the knowledge to write an OS kernel! I've never written Pentium assembly, but think I could figure that part out in a couple of days... a couple of weeks worst case. It would probably take me months to figure-out how to load and run the program (and somehow report the results) without an OS. (It would be easy on some of the simple embedded systems that I work on... I could load the binary (machine code) into an EPROM or flash chip.)
*It will most-likely "get lost" and start executing garbage... That is, it will interpret "random" stuff in memory as CPU instructions. I see "garbage execution" every day, in my case caused by hardware defects (open or shorted address or data lines, etc.). Usually, the CPU is running like crazy but nothing is happening.
Last edited by DougDbug; 06-17-2005 at 01:08 PM.