How to read content at an absolute memory address the quickest

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cchma
    New Member
    • Sep 2011
    • 1

    How to read content at an absolute memory address the quickest

    Hi,
    I'm trying to make my program read the content at a known memory address as fast as it can. I tried declaring that memory location as a variable (say 'aaaaa') normally as below:

    unsigned int * aaaaa = (unsigned int *) 0xA0000008;

    It compiled and retrieved the memory content at 0xA0000008 properly for me, except it took many assembly codes. This is because aaaaa is mapped to an internal code (say '012345') by my compiler. When running, 012345 is loaded into a CPU register, then the register content is used to get the memory address 0xA0000008, then the memory content is read from memory location 0xA0000008. (I read the assembly file generated by the compiler).

    I would like the compiler to map aaaaa to 0xA0000008 instead, so that when running aaaaa (i.e. 0xA0000008) can be loaded into the CPU register, then the memory content pointed to by the register can be read right away, skipping one level of indirection.

    Does anybody know how to write the C codes to get such effect?

    Look forward to everybody's replies.

    CCHMA
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I know of no way to do this.

    C/C++ all use logical addressing based on a flat address space. The logical address in your program is mapped by the operating system into an address that's valid for your process and the core in which it is running at the moment. You have no access to the absolute address.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      That rather depends on your platform weaknessforcats .

      There are plenty of microprocessor platforms where you have absolute access to the RAM (and FLASH too often).

      Anyway I think you have misunderstood the OPs question, the extra step comes not from having to convert virtual to real address but rather having to load the address of aaaaa then load from that the address to access and then read that final address.

      Of course eliminating 1 of those 3 steps is unlikely to make a big difference thinks would need to really critical to even consider trying.

      However I would have thought using a const rather than a normal variable (especially in C++) might achieve it like so

      Code:
      const unsigned int * aaaaa = (unsigned int *) 0xA0000008;
      unsigned int value = *aaaaa;
      If that doesn't work then try using a macro, either a macro constant or a macro function

      Code:
      #define AAAAA ((unsigned int *)0xA0000008)
      unsigned int value = *AAAAA;
      Code:
      #define READ_ADDRESS(x) (*((unsigned int *)x))
      unsigned int value = READ_ADDRESS(0xA0000008);
      All of these 3 methods attempt to remove the variable aaaaa as variable from the compiled program replacing it with an absolute address in the program. In the first case it should be compiled/optimised out because as a constant the compiler can optimise it away to an absolute value.

      Of these 3 methods the first is most preferable, if it works, because it maintains the program structure and type system more rigorously. It is more likely to work in C++ than C though because consts in C++ are much more truly constant values than they are in C.

      Comment

      Working...