Union reversing half words

J

Jay Azurin

Hello,
I'm trying to create a memory dump parser tool for which I plan to use
existing header files that contain structures of registers. My plan
is to use a union and include the structures in the header files plus
a 'dump' variable where I would store the memory dump. A simplified
version of this would look something like this:

union
{
struct
{
unsigned short first1; //first half of first word
unsigned short second1; //sencond half of first word
unsigned long whole; //second word
}words;

unsigned long dump[2]; //two words
}words_union;

main()
{
words_union.dump[0] = 0x1BADC0DE;
words_union.dump[1] = 0x1BADC0DE;
return 0;
}
Where the struct 'words' represents a register memory map, and dump[]
my dump place holder.

After running main() what I'm seeing in memory is that first1 =
0xC0DE, second1 = 0x1BAD, and whole = 0x1BADCODE.
My expectaion (and wish) is that first1 = 0x1BAD, and second1 =
0xCODE. But apparently these two half words are reversed in memory.

My feeling is that this has to do with endianess, but I'm not sure,
could anyone give a clue on how to store the half words in memory
correctly?

Thanks in advance.
 
B

Barry Schwarz

Hello,
I'm trying to create a memory dump parser tool for which I plan to use
existing header files that contain structures of registers. My plan
is to use a union and include the structures in the header files plus
a 'dump' variable where I would store the memory dump. A simplified
version of this would look something like this:

union
{
struct
{
unsigned short first1; //first half of first word
unsigned short second1; //sencond half of first word
unsigned long whole; //second word
}words;

unsigned long dump[2]; //two words
}words_union;

main()
{
words_union.dump[0] = 0x1BADC0DE;
words_union.dump[1] = 0x1BADC0DE;
return 0;
}
Where the struct 'words' represents a register memory map, and dump[]
my dump place holder.

After running main() what I'm seeing in memory is that first1 =
0xC0DE, second1 = 0x1BAD, and whole = 0x1BADCODE.
My expectaion (and wish) is that first1 = 0x1BAD, and second1 =
0xCODE. But apparently these two half words are reversed in memory.

My feeling is that this has to do with endianess, but I'm not sure,
could anyone give a clue on how to store the half words in memory
correctly?

Yes, you are suffering for little endian-ness. The easiest way to see
what memory actually looks like (or to set it to a particular sequence
of bytes) is to use an array of unsigned char. There might be a way
to do it with shifts and bitwise ands but I think this will also
suffer from endian-ness and be non-portable.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,062
Members
48,769
Latest member
Clifft

Latest Threads

Top