Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Re: [lpc2000] Re: fixed address variable

2005-11-11 by Robert Adsett

At 07:23 AM 11/10/05 +0000, szemzoandras wrote:
>My second problem. Here is a little prog:
>unsigned int *a = (void * )0x81000000;
>
>         *a = 0x11223344;
>         unsigned int *b = (void * )0x81000004;
>         *b = 0x55667788;
>         printf("Memory0.4: %x\r\n",*a);

====> a first dereferenced here ^^.

>         printf("Memory4.4: %x\r\n",*b);
>         unsigned char *ss = (void*)0x81000001;
>         printf("Memory1.1: %x\r\n",*ss);
>         printf("Memory0.4: %x\r\n",*a);
>         ss = (void*)0x81000002;
>         printf("Memory2.1: %x\r\n",*ss);
>         ss = (void*)0x81000003;
>         printf("Memory3.1: %x\r\n",*ss);
>         *ss = 0x99;
>         printf("Memory3.1ch: %x\r\n",*ss);
>         printf("Memory0.4: %x\r\n",*a);
>         ss = (void*)0x81000003;
>         *ss = 0x77;
>         printf("Memory0.4: %x\r\n",*a);
>         ss = (void*)0x81000000;
>         *ss = 0x88;
>         printf("Memory0.1ch: %x\r\n",*ss);
>         printf("Memory0.4: %x\r\n",*a);
>
>And here is the output:
>
>Memory0.4: 11223344
>Memory4.4: 55667788
>Memory1.1: 33
>Memory0.4: 11223344
>Memory2.1: 22
>Memory3.1: 11
>Memory3.1ch: 99
>Memory0.4: 99223344
>Memory0.4: 77223344
>Memory0.1ch: 88
>Memory0.4: 77223344
>
>Why can I change the 1-3 byte of a 32bit location, but not the 0.?

I suspect the one word answer is aliasing.  You have two pointers a and ss 
pointing to the same area and the value read from dereferencing a at the 
line noted above is simply kept in a register until needed.  Since it is 
re-used so many times that makes it quite a worthwhile optimization.  And 
before anyone asks that is not a compiler bug.

Two ways of fixing it
         1) the best way is don't alias.  Sometimes it may be necessary 
although I've not run across a case where it caused anything other than 
confusion when in a single block of code like this.
         2) qualify the pointer location as volatile so that read and write 
access's are forced to occur every time.  Not a good general solution since 
it forces unnecessary access to work around what is essentially a bug in 
the code.  Might be needed if the alias is buried deep in existing code though.

Robert

" 'Freedom' has no meaning of itself.  There are always restrictions,   be 
they legal, genetic, or physical.  If you don't believe me, try to chew a 
radio signal. "  -- Kelvin Throop, III
http://www.aeolusdevelopment.com/

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.