It is possible to determine whether a system uses big-endian or little-endian addressing by comparing the way it arranges bytes in order of significance with the way it addresses fields. For example, the code shown in Listing 2-1 makes this test.
Listing 2-1 Endian addressing mode test
typedef unsigned short half;
typedef unsigned char byte;
union {
half H;
byte B[2];
} halfTrick;
halfTrick ht;
ht.H = 0x2223;
if (ht.B[0] == 0x22)
printf("I'm big-endian");
else
printf("I'm little-endian");
An important global variable that the Power Macintosh startup firmware stores in nonvolatile RAM is called little-endian?. It contains a value of 0 if the last operating system run on the computer used big-endian addressing or -1 if the last operating system used little-endian addressing. Each time the Power Macintosh startup firmware loads an operating system, it checks to see whether the system's big-endian or little-endian operation matches the value in little-endian?. If the match fails, the Power Macintosh startup firmware changes the value in little-endian? and begins the Open Firmware startup process again. The Power Macintosh nonvolatile RAM is described in Nonvolatile RAM.