Thread: constant address

  1. #1
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42

    constant address

    Hi guys,

    Just wondering is it possible to force compiler to place a constant at some specific address?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    For some compilers, it might be possible to make it place a variable somewhere. Most of the time, you probably should not need to do that.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yes, it is;
    Code:
    int main()
    {
           unsigned int *address = (unsigned int *)0x378;
           *address = 42;
    }
    This sort of thing works with some older operating systems (eg under MS-DOS, this will typically write the value 42 to the address 0x378, which often corresponds to the data port of the first parallel port).

    It does not work with more modern operating systems (windows NT/2000/XP/Vista, Unix ....) as those systems are specifically designed to prevent user programs from accessing system resources (memory, etc) directly by such means. You will normally need to find some other way (eg use an system-supplied API designed to access particular devices, etc)

    Technically, it is also undefined behaviour too ..... the C/C++ standards offer no guarantee of the result of dereferencing some arbitrary pointer (and a pointer with a value picked by the programmer falls in the category of "arbitrary").

  4. #4
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42

    constant's message

    Sorry, but I misstyped. I mean how can i force a compiler to build a value in ROM at address I want. I think that this must be done at compiling/linking stage. I need this to embedded system.

    Thing is that I want to have let say at address 0 in rom 2B reserved for constant initially 0x0000. Then I want to be able to open that HEX file (by some software) and change the value. This value can be for example a calibrating value for I/O etc.

    In software I will be using simple pointer to read that value, so there is only need to make this 2B in ROM reserved.

    Hope this is clear.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And what if the address is already taken by another app/the OS? Usually the address would be stored as an offset from the start of the program, then when the OS loads it, it punches in the addresses (for where it resides in memory).

    We're living in a day of protected memory here, although for embedded systems it could perhaps be different

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Again, it depends on your compiler. C doesn't support this natively. Your compiler and target system must support it. RTM.

  7. #7
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    Quote Originally Posted by zacs7 View Post
    And what if the address is already taken by another app/the OS? Usually the address would be stored as an offset from the start of the program, then when the OS loads it, it punches in the addresses (for where it resides in memory).

    We're living in a day of protected memory here, although for embedded systems it could perhaps be different
    See I tought that I can maybe mess around in cstartup file. It has code to set whole stuff for OS. So if at the beginning of file I do in asm:
    org 0
    --define me 2B--
    then that would work? Don't know asm well so I'm not sure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM