SGI Techpubs Library

Linux  »  Books  »  Developer  »  
Porting IRIX Applications to SGI Altix Platforms: SGI ProPack for Linux
(document number: 007-4674-001 / published: 2004-05-14)    table of contents  |  additional info  |  download
find in page

Chapter 2. Endian Order

One of the major issues that may arise when porting an application is endian order. This chapter summarizes the differences in byte order between big endian systems and little endian systems.

A Case of Endianness

A big endian machine (such as MIPS/IRIX) will lay out a word in memory such that the highest order byte will be at the lowest address. For example the 32-bit word 0x12345678 will be laid out on a big endian machine as follows:

Memory offset

0

1

2

3

Memory content

0x12

0x34

0x56

0x78

If we view 0x12345678 as two half words, 0x1234 and 0x5678, we would see the following in a big endian machine:

Memory offset

0

2

Memory content

0x1234

0x5678

On a little endian machine (IA64/Altix) the highest order byte is at the highest address. So our examples above would look like the following:

Memory offset

0

1

2

3

Memory content

0x78

0x56

0x34

0x12

Similarly, the two half-words 0x1234 and 0x5678 would look like the following:

Memory offset

0

2

Memory content

0x3412

0x7856

It is important to realize that individual bytes are addressed in the same order on either machine. Thus, the following stream would appear in this same order on both a big endian and a little endian system:

0x12

0x34

0x56

0x78


Examples

The following examples illustrate the difference in byte order between big endian and little endian machine.

The following C program will print out “big endian” when compiled and run on a big endian machine and “little endian” when compiled and run on a little endian machine.

Example 2-1. C Program Illustrating Endian Order

#include <stdio.h>
main()
{
int i = 0x12345678;
if ( *(char *)&i == 0x12 )
printf(“Big endian\n”);
else if ( *(char *)&i == 0x78 )
    printf(“Little endian\n”);
}

The following Fortran program, when compiled, creates a file, fort_7 that contains the string “ABCDEFGHIJ”:

Example 2-2. Fortran Program Illustrating Endian Order

        program yy
        character*10 str
        integer strint(2)
!        equivalence (str,strint(1))
        str = “ABCDEFGHIJ”
!       print 10, str,strint(1),strint(2)
! 10    format(2x,a10,2x,z20,2x,z20)
        open(unit=7,form='unformatted',file=”fort_7”,status='new')
        write(unit=7) str
        close(unit=7)
        end

After compiling this program, note the results of a dump from the od command:

% yy
% od -hc fort_7
0000000 000a 0000 4241 4443 4645 4847 4a49 000a
         \n  \0  \0  \0   A   B   C   D   E   F   G   H   I   J  \n  \0
0000020 0000
         \0  \0
0000022

This program was compiled on a little endian machine. The individual bytes `A' `B' `C' etc. are in order. Notice how when read as words the `A' & `B' are swapped to 4241. When we get to the end we see the delimiter as the bytes 0a 00 00 00 and the two swapped halfwords 000a and 0000 which corresponds to a word value of 0x0000000a or 10 (decimal).

On a big endian machine the delimiter word would be as follows in bytes:

00 00 00 0a

This is what we see when we run this on a big endian machine OR set the endian environment variable mode to be big endian on an Altix, as in the following example. Note the order of the byte characters \0 \0 \0 \n.

% setenv  F_UFMTENDIAN big
% rm fort_7
% yy
% od -hc fort_7
0000000 0000 0a00 4241 4443 4645 4847 4a49 0000
         \0  \0  \0  \n   A   B   C   D   E   F   G   H   I   J  \0  \0
0000020 0a00
         \0  \n
0000022

Because we are on a little endian box, the words are swapped. So in this case 00 00 and 00 0a become 0000 and 0a00

Porting IRIX Applications to SGI Altix Platforms: SGI ProPack for Linux
(document number: 007-4674-001 / published: 2004-05-14)    table of contents  |  additional info  |  download

    Front Matter
    About This Guide
    Chapter 1. Porting Overview
    Chapter 2. Endian Order
    Chapter 3. 64-bit ABI Porting Issues
    Chapter 4. Compiler and Development Tools
    Chapter 5. Additional Development Tools
    Chapter 6. Message Passing on IRIX and Linux
    Chapter 7. POSIX Threads (pthreads) Implementations
    Chapter 8. Miscellaneous Porting Concerns
    Chapter 9. Frequently Asked Questions
    Appendix A. Application Programming Interface (API) Differences: libc
    Index


home/search | what's new | help