jdupre
April 30, 2009, 12:48am
1
I have the following defined:
extern unsigned char macAddr[6];
macAddr[0] = 0x00;
macAddr[1] = 0x0D;
macAddr[2] = 0x77;
macAddr[3] = 0x0A;
macAddr[4] = 0x0B;
macAddr[5] = 0x0C;
The Eclipse debugger tells me that macAddr is at 0x40000045
When I look at memory using the mdb command it shows this:
> mdb 0x40000045 0x7
0x40000045: 00 0d 77 0a 0b 0c 00
Just as I expect t it to be.
But when I use mdw it shows this:
> mdw 0x40000040 0xff
0x40000040: 40000f04 770d0041 000c0b0a 00001000 0000196e abcd0000 ffffffff 0000ffff
^^^^^^^^^^^^^^^^^
Is this a bug, or am I ignorant about how memory works?
jdupre
April 30, 2009, 4:34pm
2
OK, I see what is happening. The mdw command shows the words “byte-swoped”. Flipping the bytes of each word puts the bytes of the array in the “correct” order.
However, when I look at a 32bit register stored on the stack, the word is displayed in the “correct” order. In other words, if R14 (LR) is 0x003654C and I look at the stack:
0x40007800: e5e5e5e5 e5e5e5e5 e5e5e5e5 e5e5e5e5 e5e5e5e5 e5e5e5e5 e5e5e5e5 e5e5e5e5
0x40007820: e5e5e5e5 e5e5e5e5 e5e5e5e5 e5e5e5e5 e5e5e5e5 e5e5e5e5 e5e5e5e5 40005618
0x40007840: 4000786c 40007850 0003255c 000366a8 00000000 00000000 00000000 00000000
0x40007860: 40007884 40007870 000379b4 00032228 400055e8 4000789c 40007884 00034008
0x40007880: 00000000 400055d4 40004304 4000430c 40004318 400055c0 400078a4 0003654c
^^^^^^^^
0x400078a0: 000334e0 40000080 0000ea60 400078c8 400078bc 00036950 00036498 400078e4
0x400078c0: 400078cc 00032198 0003694c 00000000 00000000 00000001 400078fc 400078e8
0x400078e0: 00003190 0003212c 00000001 00000000 ffffffff 40007900 000010e0 00003100
There, the bytes are displayed in the “correct” order.
I don’t understand. Why is byte order of data in memory swoped in some cases but not in others?
JJ
April 30, 2009, 7:17pm
3
The Eclipse debugger tells me that macAddr is at 0x40000045
Using word accesses for unaligned locations causes the data loaded to appear byte shifted or swapped on ARMv4/5. This is fairly typical for RISC processors. Cortex allows unaligned loads and stores at the cost of an extra bus cycle.
fordp
May 8, 2009, 3:03pm
4
If you are using GCC you can use this macro :-
#define AL(in) attribute ((aligned (in)))
Then define your variable :-
AL(4) unsigned char ByteAreay[42];
It should be better to view in the debugger then !
If you are worried about ram wastage in the finally application you can do this
#ifdef DEBUG
#define AL(in) attribute ((aligned (in)))
#else
#define AL(in)
#endif
Then the wasted ram can be removed in your release version !