hardware access

This is a discussion on hardware access within the C++ Programming forums, part of the General Programming Boards category; Is there anyway that c++ can access hardware without going through an OS? The OS must do it somehow....

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    266

    hardware access

    Is there anyway that c++ can access hardware without going through an OS? The OS must do it somehow.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    In simple terms, no. You must go through the OS.

    This question is highly specific to whatever platform and OS you're developing for. On x86 processors (most PCs), it's the above no - specific access is done by the OS. You, as a plain old application, must go through the OS. How the OS does it may not be allowable in an app. (There are, for example, certain instructions for x86 processors that only the OS can execute.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    If your OS is DOS, or any number of real time embedded operating systems - sure.
    If your OS is a protected OS like Linux or any of the NT series from Microsoft, then no.
    Be specific.
    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 support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To expand a bit on the previous replies:
    the OS, if it's a modern 32-bit OS, will allow hardware access at kernel level. Anything running as regular C or C++ code will not be allowed to touch any hardware. This is because we don't really want applications to "mess" with hardware in an disorganized manner - if you have two applications that both wants to access the same hardware, the result would likely be quite unpredictable (and with even more certainty, not what either application expects).

    The way that the OS actually gets to the hardware is via drivers. Drivers can be written in C++ in some OS's, but for the two most common PC OS's, Linux and Windows, you'd have to jump through hoops to make a driver in C++, and there are many limits on what you can and can't do in C++ when you have jumped through those hoops (exceptions, global objects for example). It can be done, but it's only really practical to do so if you have a large amount of existing code, or you are writing a very large driver that will definitely benefit from C++ coding. I'd hazzard a guess that less than 1% of existing Windows drivers in production are using C++.

    This is not the case for all OS's tho: Windows CE for example uses a completely different driver architecture, that is designed entirely around drivers written in C++.

    And of course, as Salem indicates, "simple" OS's have no protection between user-code and hardware, so any application can touch anything in the system - whether it's doing it in the right way or not.

    Finally, writing drivers, whether that is with or without protection, tends to get pretty complicated pretty quickly - it is not a beginner task by any means.

    As Salem says: Provide more specifics about what you are trying to do, and we can probably help you find a solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    The part about drivers is what I problaby really meant to ask. Any tips on where to start?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lruc View Post
    The part about drivers is what I problaby really meant to ask. Any tips on where to start?
    What OS?

    Linux - get the kernel source and the code is in the "driver" section (from memory).

    Windows - get the Windows DDK from the MS website. It's pretty large, but there is a fair bit of source and all the tools you need (including a compiler that it guaranteed to be compatible with the kernel).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Whats a kernel?

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    890

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lruc View Post
    Whats a kernel?
    It's "the actual OS", the "core" of the operating system, that handles the processes, memory and processor time (and any other resources that need to be managed).

    You may want to read up on operating systems and such, for example Andrew Tannenbaum's book [1]:
    http://www.pearsonhighered.com/educa...USS_01DBC.html

    [1] This is probably the best book on the subject ever written, and it's the book that made a certain Linus Torvalds decide to write his own OS. documented here amongst other places

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Anywhere I can get an online book for free?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lruc View Post
    Anywhere I can get an online book for free?
    Considering that amazon.com has an older edition for $0.32, I would expect that you can probably get one for less than the extra effort of finding ALL of the knowledge in various places on the web.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    What does the book exactly explain? Have you read it?

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by lruc View Post
    The part about drivers is what I problaby really meant to ask. Any tips on where to start?
    if you are trying to communicate with a peripheral device, you must obtain the programmer's documentation from the manufacturer.

    from there, just RTFM.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lruc View Post
    What does the book exactly explain? Have you read it?
    Yes, I studied this book in a "Operating System Design" class many years ago. It covers everything from basic principles of how an OS works, all the way to concrete examples of how modern OS's do things in the "real world". You can not get a better book on that subject.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Thanks for all the help but ill stick with working through an OS.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Hardware Protected Mode
    By valaris in forum Tech Board
    Replies: 9
    Last Post: 02-15-2009, 08:56 AM
  2. What is at address 0?
    By Yasir_Malik in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 07-20-2006, 06:06 AM
  3. Replies: 3
    Last Post: 09-22-2003, 10:48 PM
  4. Direct disk access in DOS
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-26-2002, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21