Handling big/little endian issues

All processors have byte addressable memory. For processor dataprimitives which are larger than one byte the "endian" is the byte orderin which the data is stored to memory. Two formats exist. Little endian and big endian.

Very briefly, an explanation of endian.

Little endian
A two byte type contains the value 1234h. On a little endian processor the lowest byte in memory of the type is 34h and the next byte is 12h. 34h is the least significant byte and 12h is the most significant. If you read the type as a byte type then you would read 34h. Therefore accessing memory in varying type sizes is consistent. If the two byte memory location contained the value 34h and you read two bytes you would read 34h. If you read a one byte from the same address you would read 34h.

Big endian
For the same two byte type containing 1234h. The lowest byte in memory is 12h (most significant) and the next byte is 34h. If you read the location as a byte then you would read 12h.

This byte ordering algorithm logically extends to larger processor primitive types. Little endian stores the least significant byte of a data value at the lowest address and on up to the most significant byte in the highest address. Big endian is the opposite. Most significant in the lowest address, and least significant in the highest address.

Handling endian issues

Okay, you have code that needs to run on both little endian processors and big endian processors and they need to share data and/or disk files. What do you do.

The compiler has predefined version tags to help here. LittleEndianand BigEndian. The proper version tag will be defined depending on the target processorand operating system the compiler code generation is targeting.

The compiler has support routines to help you handle the endiandependent situations. SWAPENDIAN, LITTLEENDIAN and BIGENDIAN.

SWAPENDIAN will convert from one endian format to the other. If the data is currently little endian, after the swap you have big endian format.

LITTLEENDIAN assumes the current data order is the processornative data order and it will swap the endian if necessary to result in little endian data. On little endian processors this support routine does nothing. On big endianprocessors it generates a swap. Think of LITTLEENDIAN as

%IF BigEndian %THEN

    SWAPENDIAN(myData);

%END

BIGENDIAN is just like LITTLEENDIAN except it always results in BIGENDIAN data.

Encapsulation

A simple method of dealing with a large installed base of data on disk which must be shared is to standardize on a specific endian fomat for the data. Encapsulate the reading and writing of the data to a procedure for read and write, preferably in a compilation unit created specifically for this task. Within these encapsulation procedures you read/write the data and swap as necessary to get to native processor format. This isolates the endian swapping to a single location in the entire program. See the topic Optimal disk and memory records.