Venkata, Bulusu (IE10) wrote:
Hi,
I'm having problems with packed structures. Given below is a sample
code("test.c") which uses a packed structure:
#include <stdio.h>
typedef struct {
char a;
int b;
}__attribute__((packed)) unaligned_struct;
unaligned_struct hg;
char dummyVar;
void main(void){
unaligned_struct* ptr=&hg;
hg.a = 0x56;
hg.b = 0x1234;
}
The equivalent assembly code generated was as follows:
- 0x1140 <main>: mov #2558, SP ;#0x09fe
- 0x1144 <main+4>: mov r1, r4 ;
13 unaligned_struct* ptr=&hg;
- 0x1146 <main+6>: mov #514, 0(r4) ;#0x0202
14
15 hg.a = 0x56;
0x114c <main+12>: mov.b #86, &0x0202 ;#0x0056
16 hg.b = 0x1234;
- 0x1152 <main+18>: mov #4660, &0x0000 ;#0x1234
- 0x1158 <main+24>: incd SP ;
- 0x115a <main+26>: br #0x115e ;
The msp430-gcc compiler gives an error/warning at line no. 16, (i.e. at hg.b
= 0x1234;):
test.c:16: internal error: unsupported relocation error
the msp430 hardware cannot acces shorts at odd addresses. but with your
struct definition you forced the compiler to place "int b" att an odd
address.
ints/shorts from odd addresses can only be read by two separte byte
reads. mspgcc doesn't generate such code at the moment, it seems.
(patches are welcome ;-)
but keep in mind that code with unaligned data will be larger and less
efficient as code for aligned variables.
However, when I tried the same with the IAR compiler, it works fine. I guess
"works" or "compiles"? have you confirmed that it realy works, or does
it just load the "int" from the wrong address?
MSPGCC doesn't guarantee support for packed structures due to the problems
MSP430 has with unaligned word accesses - so how does IAR do it? I apologize
for the long post, but I desperately need to use packed structures in my
code. Thank you..
you could define a low and high byte access instead, that code works
with both compilers, but you need to manualy recombine these two into an
16 value if you need it.
typedef struct {
char a;
char b_low;
char b_high;
}__attribute__((packed)) unaligned_struct;
and even better, if you have the data format under your control, insert
bad bytes or rearange the struct members to get all 16 bit and larger
types aligned.
if the data format is defined by an enternal communications parter it
may as well use big endian numbers (the common byte order in network
protocols) in that case, you need to convert all incomming data anyway
as the msp430 is a little endian system.
chris
Regards,
Sudhakar
-------------------------------------------------------
The SF.Net email is sponsored by: Beat the post-holiday blues
Get a FREE limited edition SourceForge.net t-shirt from ThinkGeek.
It's fun and FREE -- well, almost....http://www.thinkgeek.com/sfshirt
_______________________________________________
Mspgcc-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users