Question

I met this question in an interview. I have no such experience.

So if we have two registers. One with address 0x11111111 and the other 0x22222222. We want to read and write it. The first one is a 32-bit register while the second one is 64-bit. How do we do it in C? Can anyone just give me an example?

Thanks,

Was it helpful?

Solution

You can use some kind of pointer or other, for example:

#include <stdint.h>

uint32_t volatile * p = (uint32_t volatile *) 0x11111111;
uint64_t volatile * q = (uint64_t volatile *) 0x22222222;

++*p;  // read-modify-write

(Note that this specific example is almost certainly bogus, since neither address seems to be aligned properly for the respective type.)

As you say, qualifying the pointers as volatile is necessary if the values stored at those addresses can change from outside your program; with volatile you tell the compiler that no assumptions may be made about the value (e.g. constant propagation or common subexpression elimination may not be done for volatile values).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top