Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
04/06/05 18:51
Read: times


 
#91131 - Alignment/byte ordering matters a lot!
Responding to: ???'s previous message
Andy Neil said:
Michael Karas said:
Depending upon what is going on there may be cases where variable alignment could be a big deal on an 8052 platform!!

The example that springs to mind is an 8051 communicating with another processor via shared memory; either the other processor would have to be forced to "pack" its variables, or the 8051 would have to be forced to observe the alignment requirements.

And then there's the issue of endian-ness, of course...

Ah, the joys of cross-platform interworking!

Most definitely true. Fortunately, tools typically provide some facilities to assist in controlling alignment. For example, Keil AX51 has an EVEN directive, C51 has the XWORD macro, etc. Still, dealing with this type of situation seems unpleasant, perhaps more than it should have to be.

The fact of the matter is though, you can run yourself into a serious alignment-related problem even without an exotic shared memory configuration. For example, you might have structs with identical declarations in C on your 8051 application and on some other platform (like a PC) that you would like to transfer between the two devices (over serial port, network, or another medium). It might be tempting to simply start sending bytes at the base address of a struct, and keep transmitting until you have sent the number of bytes which constitute the size of the struct on the transmitting system. Is the struct you get out on the other end going to be properly populated with the data you sent? If you can be sure that two conditions can simultaneously be met, probably. The conditions are:
  • Both sides use the same alignment scheme for data elemetns larger than a byte, or the struct contains no elements that are larger than a byte.
  • Both sides use the same byte ordering scheme, or the struct contains no elements that are larger than a byte.
Chances are though, these conditions will often not be met. In the event that they are not, the best case scenario is corruption of the transferred data. However, if the structs are different sizes on the different platforms (i.e., due to alignment differences), the system that receives a struct larger than it is expecting is going to experience an out of bounds memory access; the consequences of this will be system dependent, but they will not be good.

It is for these reasons that we write serializers and deserialzers for data that we wish transfer between systems. A serializer's job is to take a platform-dependent representation of data and transform it into a platform-independent, unambiguous one that may be transmitted between systems. A deserializer transforms the serialized message back into the native format understood by the receiving system.

In the end, alignment/byte ordering are critical considerations just about whenever two systems that do not honor the same conventions are communicating, regardless of whether they are 8-bit microcontrollers, 64-bit workstations, or anything in between.

--Sasha Jevtic


List of 13 messages in thread
TopicAuthorDate
alignment of variable in c            01/01/70 00:00      
   Modulo 16            01/01/70 00:00      
      thanks smith            01/01/70 00:00      
      Addressing            01/01/70 00:00      
   Not relevant to 8051            01/01/70 00:00      
      re not relevant            01/01/70 00:00      
      This subject could be relevant....            01/01/70 00:00      
         yeah right            01/01/70 00:00      
         External Influences            01/01/70 00:00      
            Alignment/byte ordering matters a lot!            01/01/70 00:00      
               you can not align when ...            01/01/70 00:00      
                  good references            01/01/70 00:00      
                     RTFM!            01/01/70 00:00      

Back to Subject List