Thread: question about using cpu

  1. #1
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56

    Question question about using cpu

    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.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    You can use inline assembly in c++ using the keyword asm, but to answer your question, c++ can't directly access the cpu architecture.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    There are a couple of issues...

    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.

  4. #4
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56
    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.
    I know, but for my program I need to use the CPU architecture. So it's not really a benefit to me :-)


    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.
    Thanks, that is helpful, but I wouldn't know where to begin!



    How do I use keyword asm? What's the syntax?
    Last edited by AdamLAN; 06-16-2005 at 01:47 PM.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    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;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  7. #7
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56
    Perhaps if you explained your "need" better, we could help.
    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.

    Do it yourself Borg kit - some self assimilation required.
    Very funny, by the way, I love star trek.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    to see what happens if the program is left alone for a long time.
    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"

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. CPU Usage so high
    By X PaYnE X in forum Windows Programming
    Replies: 9
    Last Post: 12-21-2003, 03:07 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. cpu temp
    By dP munky in forum Tech Board
    Replies: 36
    Last Post: 02-07-2003, 06:01 PM
  5. Replies: 72
    Last Post: 11-25-2002, 05:55 PM