How to write ANY value

Discussion in 'C Programming' started by aleksa, Feb 21, 2011.

  1. aleksa

    aleksa Guest

    Some memory-mapped devices require a write to a specific
    memory address in order to do something.
    The data written is of no importance.

    How can I instruct the compiler to write
    whatever value it finds handy in that moment?

    P.S.
    That won't make the code much better, I'm just curious.
     
    aleksa, Feb 21, 2011
    #1
    1. Advertising

  2. aleksa

    Ben Pfaff Guest

    aleksa <> writes:

    > Some memory-mapped devices require a write to a specific
    > memory address in order to do something.
    > The data written is of no importance.
    >
    > How can I instruct the compiler to write
    > whatever value it finds handy in that moment?


    This may work:

    unsigned char x;

    *p = x;
    --
    Ben Pfaff
    http://benpfaff.org
     
    Ben Pfaff, Feb 21, 2011
    #2
    1. Advertising

  3. aleksa <> wrote:
    > Some memory-mapped devices require a write to a specific
    > memory address in order to do something.
    > The data written is of no importance.


    They typically are;-)

    > How can I instruct the compiler to write
    > whatever value it finds handy in that moment?


    What means "handy at that moment"? That's a bit unspecific.
    If you want to write just a single byte then, as Ben Pfaff
    pointed out, using an unsigned char pointer (with the ad-
    dress cast to that) would do. If you want to write more than
    a single byte (but that would, of course, also work for a
    single byte) you could use memcpy() to the target address,
    cast to a void pointer. But there could be problems with
    that: many devices want e.g. a single 16-bit or 32-bit wide
    write or they won't do what you expect from such an access.
    memcpy() is under no obligation to do it that way (even though
    there's some chance that it copies in larger than 1-byte wide
    chunks if possible). In device drivers you will often find
    several functions for different wide accesses. If you need
    e.g. to read or write 16-bit or 32-bit wide data instead of
    an unsigned char (or uint8_t) pointer you'd use uint16_t or
    uint32_t pointers. You have to carefully read the specifi-
    cations for the hardware and decide which function to use
    in each case - there's no fit-it-all function you could use
    without further thought.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Feb 21, 2011
    #3
  4. Ben Pfaff <> writes:
    > aleksa <> writes:
    >
    >> Some memory-mapped devices require a write to a specific
    >> memory address in order to do something.
    >> The data written is of no importance.
    >>
    >> How can I instruct the compiler to write
    >> whatever value it finds handy in that moment?

    >
    > This may work:
    >
    > unsigned char x;
    >
    > *p = x;


    Yes, but I'd be more comfortable with something like this:

    #define ARBITRARY_DATA 42
    *p = ARBITRARY_DATA;

    For something other than a character type, reading an uninitialized
    variable could allow the compiler to make certain assumptions that
    could break your code. I think Ben's point is that the standard
    makes certain extra guarantees about character types, but I wouldn't
    want to depend on those guarantees without a good reason.

    In either case, I'd say it's important to clearly document what you're
    doing. (The name "ARBITRARY_DATA" is intended to do this.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 21, 2011
    #4
  5. aleksa

    aleksa Guest

    On Feb 21, 7:23 pm, (Jens Thoms Toerring) wrote:

    > What means "handy at that moment"?


    Writing whatever value is stored at that moment in whatever CPU
    register.
     
    aleksa, Feb 21, 2011
    #5
  6. aleksa <> writes:
    > On Feb 21, 7:23 pm, (Jens Thoms Toerring) wrote:
    >> What means "handy at that moment"?

    >
    > Writing whatever value is stored at that moment in whatever CPU
    > register.


    And how is that better than, for example, always writing 42?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 21, 2011
    #6
  7. aleksa

    aleksa Guest

    On Feb 21, 8:05 pm, Keith Thompson <> wrote:
    >     #define ARBITRARY_DATA 42
    >     *p = ARBITRARY_DATA;


    I was actually trying to avoid that approach,
    because it requires the CPU to load a register with a
    specific value, which isn't really necessary.

    As I said to Jens:
    "Writing whatever value is stored at that moment in whatever CPU
    register",
    without the need to initialize it first.

    Maybe it is doable with in-line assembly, but I'm not sure how.
    Maybe someone can adjust this (assuming DWORD access):
    __asm *p = eax; // IA-32, write whatever was stored in EAX register.
    __asm *p = R0; // ARM arch
     
    aleksa, Feb 21, 2011
    #7
  8. Keith Thompson <> writes:

    > aleksa <> writes:
    >> On Feb 21, 7:23 pm, (Jens Thoms Toerring) wrote:
    >>> What means "handy at that moment"?

    >>
    >> Writing whatever value is stored at that moment in whatever CPU
    >> register.

    >
    > And how is that better than, for example, always writing 42?


    It shaves a whole instruction cycle off the write to the watchdog
    register :)

    I read the OP's question as "I know this won't make any difference,
    but as an academic excersize, how could it be done?"

    --
    /Wegge

    Leder efter redundant peering af dk.*,linux.debian.*
     
    Anders Wegge Keller, Feb 21, 2011
    #8
  9. aleksa

    aleksa Guest

    On Feb 21, 8:37 pm, Anders Wegge Keller <> wrote:

    >  I read the OP's question as "I know this won't make any difference,
    > but as an academic excersize, how could it be done?"


    Exactly :)
     
    aleksa, Feb 21, 2011
    #9
  10. aleksa

    Ben Pfaff Guest

    Sherm Pendley <> writes:

    > aleksa <> writes:
    >
    >> On Feb 21, 8:05 pm, Keith Thompson <> wrote:
    >>>     #define ARBITRARY_DATA 42
    >>>     *p = ARBITRARY_DATA;

    >>
    >> I was actually trying to avoid that approach,
    >> because it requires the CPU to load a register with a
    >> specific value, which isn't really necessary.

    >
    > Not necessarily - all of the assembler languages I'm familiar with have
    > an instruction to store a literal value, and I assume any decent compiler
    > would emit such an instruction if given the above code.


    Based on a few minutes looking around the Internet, I don't think
    that ARM has a "store immediate" instruction.
    --
    "Give me a couple of years and a large research grant,
    and I'll give you a receipt." --Richard Heathfield
     
    Ben Pfaff, Feb 21, 2011
    #10
  11. aleksa

    aleksa Guest

    On Feb 21, 8:45 pm, Sherm Pendley <> wrote:
    > aleksa <> writes:
    > > On Feb 21, 8:05 pm, Keith Thompson <> wrote:
    > >>     #define ARBITRARY_DATA 42
    > >>     *p = ARBITRARY_DATA;

    >
    > > I was actually trying to avoid that approach,
    > > because it requires the CPU to load a register with a
    > > specific value, which isn't really necessary.

    >
    > Not necessarily - all of the assembler languages I'm familiar with have
    > an instruction to store a literal value, and I assume any decent compiler
    > would emit such an instruction if given the above code.


    Yes, I've forgotten about that...
    But that is usually a 1 byte longer instruction :)
     
    aleksa, Feb 21, 2011
    #11
  12. aleksa

    aleksa Guest

    On Feb 21, 8:45 pm, Sherm Pendley <> wrote:
    > aleksa <> writes:
    > > On Feb 21, 8:05 pm, Keith Thompson <> wrote:
    > >>     #define ARBITRARY_DATA 42
    > >>     *p = ARBITRARY_DATA;

    >
    > > I was actually trying to avoid that approach,
    > > because it requires the CPU to load a register with a
    > > specific value, which isn't really necessary.

    >
    > Not necessarily - all of the assembler languages I'm familiar with have
    > an instruction to store a literal value, and I assume any decent compiler
    > would emit such an instruction if given the above code.


    Yes, I've forgotten about that...
    But that is usually a 1 byte longer instruction :)

    And I'm not sure if ARM can do that, especially if its
    base + offset = data
     
    aleksa, Feb 21, 2011
    #12
  13. aleksa

    Eric Sosman Guest

    On 2/21/2011 2:36 PM, aleksa wrote:
    > On Feb 21, 8:05 pm, Keith Thompson<> wrote:
    >> #define ARBITRARY_DATA 42
    >> *p = ARBITRARY_DATA;

    >
    > I was actually trying to avoid that approach,
    > because it requires the CPU to load a register with a
    > specific value, which isn't really necessary.
    >
    > As I said to Jens:
    > "Writing whatever value is stored at that moment in whatever CPU
    > register",
    > without the need to initialize it first.


    There's no way to do this in C. There may be ways to do it in
    a particular implementation of C, with a particular set of compiler
    options, under a particular set of conditions that enable or prevent
    particular optimizations, if you're a left-handed seventh son of a
    seventh son programming in an abandoned churchyard by the light of
    the second full Moon of an odd-numbered month.

    (In short, if you're counting the individual instructions that
    the machine executes to carry out the intent of your code, C is not
    the way to express that intent. Wrong tool; use another.)

    --
    Eric Sosman
    d
     
    Eric Sosman, Feb 21, 2011
    #13
  14. Keith Thompson <> writes:

    > Yes, but I'd be more comfortable with something like this:
    >
    > #define ARBITRARY_DATA 42
    > *p = ARBITRARY_DATA;


    It just occurred to me, that noone have mentioned the volatile
    keyword yet. Especially this case, where the same value is written
    repeatedly, is warranting a volatile declaration of p. Otherwise te
    compile may end up optimizing any but the first write into oblibion.

    --
    /Wegge

    Leder efter redundant peering af dk.*,linux.debian.*
     
    Anders Wegge Keller, Feb 21, 2011
    #14
  15. aleksa

    aleksa Guest

    On Feb 21, 9:36 pm, Anders Wegge Keller <> wrote:

    >  It just occurred to me, that noone have mentioned the volatile
    > keyword yet.


    Hardware registers are to be defined as volatile, yes.
    (not that it changes the original question, though)
     
    aleksa, Feb 21, 2011
    #15
  16. aleksa

    aleksa Guest

    On Feb 21, 6:42 pm, Ben Pfaff <> wrote:
    > This may work:
    >
    >     unsigned char x;
    >
    >     *p = x;


    It doesn't as I would like.
    My compiler stores 0 in reg, then reg to memory.
    (as opposed to just storing a reg to memory)
     
    aleksa, Feb 21, 2011
    #16
  17. aleksa <> writes:
    > On Feb 21, 6:42 pm, Ben Pfaff <> wrote:
    >> This may work:
    >>
    >>     unsigned char x;
    >>
    >>     *p = x;

    >
    > It doesn't as I would like.
    > My compiler stores 0 in reg, then reg to memory.
    > (as opposed to just storing a reg to memory)


    Does your compiler generate code to store 0, or does the register
    just happen to contain 0?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 21, 2011
    #17
  18. aleksa

    aleksa Guest

    On Feb 21, 10:14 pm, Keith Thompson <> wrote:

    > Does your compiler generate code to store 0, or does the register
    > just happen to contain 0?


    The first one.
     
    aleksa, Feb 21, 2011
    #18
  19. aleksa

    Sjouke Burry Guest

    Keith Thompson wrote:
    > aleksa <> writes:
    >> On Feb 21, 6:42 pm, Ben Pfaff <> wrote:
    >>> This may work:
    >>>
    >>> unsigned char x;
    >>>
    >>> *p = x;

    >> It doesn't as I would like.
    >> My compiler stores 0 in reg, then reg to memory.
    >> (as opposed to just storing a reg to memory)

    >
    > Does your compiler generate code to store 0, or does the register
    > just happen to contain 0?
    >

    It might be useful to give x some value.......
     
    Sjouke Burry, Feb 21, 2011
    #19
  20. aleksa

    Ben Pfaff Guest

    Sjouke Burry <> writes:

    > Keith Thompson wrote:
    >> aleksa <> writes:
    >>> On Feb 21, 6:42 pm, Ben Pfaff <> wrote:
    >>>> This may work:
    >>>>
    >>>> unsigned char x;
    >>>>
    >>>> *p = x;
    >>> It doesn't as I would like.
    >>> My compiler stores 0 in reg, then reg to memory.
    >>> (as opposed to just storing a reg to memory)

    >>
    >> Does your compiler generate code to store 0, or does the register
    >> just happen to contain 0?
    >>

    > It might be useful to give x some value.......


    The whole point was to not give x a value; please read back to
    the beginning of the thread.
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
     
    Ben Pfaff, Feb 21, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QWJlbCBDaGFu?=
    Replies:
    6
    Views:
    9,358
    Rahul
    May 3, 2006
  2. Peter Hansen
    Replies:
    1
    Views:
    386
    Joona I Palaste
    Aug 24, 2003
  3. JasBascom
    Replies:
    1
    Views:
    368
    John Harrison
    Feb 24, 2004
  4. Peter Hansen
    Replies:
    1
    Views:
    418
    Joona I Palaste
    Aug 24, 2003
  5. Chuck Amadi
    Replies:
    0
    Views:
    523
    Chuck Amadi
    Jun 22, 2004
Loading...

Share This Page