How to write ANY value

A

aleksa

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.
 
B

Ben Pfaff

aleksa said:
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;
 
J

Jens Thoms Toerring

aleksa said:
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
 
K

Keith Thompson

Ben Pfaff said:
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.)
 
K

Keith Thompson

aleksa said:
Writing whatever value is stored at that moment in whatever CPU
register.

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

aleksa

    #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
 
A

Anders Wegge Keller

Keith Thompson said:
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?"
 
B

Ben Pfaff

Sherm Pendley said:
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.
 
A

aleksa

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 :)
 
A

aleksa

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
 
E

Eric Sosman

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.)
 
A

Anders Wegge Keller

Keith Thompson said:
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.
 
A

aleksa

 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)
 
A

aleksa

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)
 
K

Keith Thompson

aleksa said:
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?
 
B

Ben Pfaff

Sjouke Burry said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top