문제

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,

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top