views:

3039

answers:

7

I'm going though a computers system course and I'm trying to establish, for sure, if my AMD based computer is a little endian machine? I believe it is because it would be Intel-compatible.

Specifically, my processor is an AMD 64 Athlon x2.

I understand that this can matter in C programming. I'm writing C programs and a method I'm using would be affected by this. I'm trying to figure out if I'd get the same results if I ran the program on an Intel based machine (assuming that is little endian machine).

Finally, let me ask this: Would any and all machines capable of running Windows (XP, Vista, 2000, Server 2003, etc) and, say, Ubuntu Linux desktop be little endian?

Thank You,
Frank

+24  A: 

All x86 and x86-64 machines (which is just an extension to x86) are little-endian.

You can confirm it with something like this:

main() {
   int a = 0x12345678;
   unsigned char *c = (unsigned char*)(&a);
   if (*c == 0x78)
      printf("little-endian\n");
   else
      printf("big-endian\n");
}
Mehrdad Afshari
there is a small typo in your code at the final printf: printf("big-endian\n"); The code allowed me to confirm. Thank you.
Frank V
Fixed. Thanks for pointing it out.
Mehrdad Afshari
While legal, it's bad practice to omit the return type of main, as well as the "return 0" at the end.
Adam Rosenfield
@Adam: Definitely, for real world code. In fact, it's also bad practice to omit include directives. I wanted to post a short "complete" C program that checks endianness and I was too lazy to type them here.
Mehrdad Afshari
+3  A: 

In answer to your final question, the answer is no. Linux is capable of running on big endian machines like e.g., the older generation PowerMacs.

1800 INFORMATION
Is that distribution, that I linked to, able to run on big endian. Thank you for address that part of my question.
Frank V
I think they're asking if those operating systems can run on little endian machines, which they can. In fact, I think they have to make special versions for the older generation PowerMacs because the PowerPC architecture is big-endian.
indyK1ng
Now that Ubuntu has ARM support, its possible for "Ubuntu" to run on a big endian processor. Recent ARM cores can run in either little or big endian mode.
Mark
+2  A: 

You have to download a version of Ubuntu designed for big endian machines. I know only of the PowerPC versions. I'm sure you can find some place which has a more generic big-endian implementation.

indyK1ng
I understand. THank you and thank you for the link.
Frank V
Ubuntu's second class ports include ia64, armel, hppa, powerpc, and sparc. In earlier releases, PowerPC was a first class port, and there was one release where SPARC was up there too.
ephemient
+3  A: 

An easy way to know the endiannes is listed in the article Writing endian-independent code in C

const int i = 1;
#define is_bigendian() ( (*(char*)&i) == 0 )
FCo
Great answer but your link is broken. Can you please correct it?
Frank V
+2  A: 

"Intel-compatible" isn't very precise.

Intel used to make big-endian processors, notably the StrongARM and XScale. These do not use the IA32 ISA, commonly known as x86.

Further back in history, Intel also made the little-endian i860 and i960, which are also not x86-compatible.

Further back in history, the prececessors of the x86 (8080, 8008, etc.) are not x86-compatible either. Being 8-bit processors, endianness doesn't really matter...

Nowadays, Intel still makes the Itanium (IA64), which is bi-endian: normal operation is big-endian, but the processor can also run in little-endian mode. It does happen to be able to run x86 code in little-endian mode, but the native ISA is not IA32.

To my knowledge, all of AMD's processors have been x86-compatible, with some extensions like x86_64, and thus are necessarily little-endian.

Ubuntu is available for x86 (little-endian) and x86_64 (little-endian), with less complete ports for ia64 (big-endian), ARM(el) (little-endian), PA-RISC (big-endian, though the processor supports both), PowerPC (big-endian), and SPARC (big-endian). I don't believe there is an ARM(eb) (big-endian) port.

ephemient
Wow, thank you for the detail. This is great support information.
Frank V
Two minor corrections: endianness matters also for 8 bit processors as some instructions refer to 16 bit quantities like addresses (`LDA $1234` (loading a byte from address $1234) will be coded `AD 34 12` on the 6502. And AMD did have another architecture than x86 it was the 29000 series RISC processors that was very popular in embedded designs like laser printers.
tristopia
@tristopia Thanks for the info, I wasn't aware of all of that.
ephemient
+2  A: 

Assuming you have Python installed, you can run this one-liner, which will print "little" on little-endian machines and "big" on big-endian ones:

python -c "import struct; print 'little' if ord(struct.pack('L', 1)[0]) else 'big'"
benhoyt
I do have Python installed. Thank you for the code.
Frank V
+1  A: 

Endianness White Paper from Intel Corp published November 15, 2004 http://www.intel.com/design/intarch/papers/endian.pdf

Contents
=========
Introduction .................................................5
Analysis     .................................................5
    Code Portability .........................................5
    Shared Data ..............................................5
    Best Known Methods .......................................5

Definition of Endianness .....................................5

Merits of Endian Architectures ...............................6
    Relevance of Endian Order ................................7

Byte Swapping ................................................8
    Byte Swapping Methods ....................................8
        Network I/O Macros ...................................8
        Custom Byte Swap Macros ..............................9
    Byte Swap Controls .......................................9
        Compile Time Controls ...............................10
        Run Time Controls ...................................10
    Recovering Byte Swap Overhead ...........................11

Platform Porting Considerations .............................11
    Data Storage and Shared Memory ..........................11
    Data Transfer ...........................................12
    Data Types ..............................................12
        Unions ..............................................12
        Byte Arrays .........................................12
        Bit Fields and Bit Masks ............................12
        Pointer Casts .......................................13
    Native Data Types .......................................14

Endian-Neutral Code .........................................14

Guidelines for Implementing Endian-neutral Code .............15
    Endian-neutral Coding Practices .........................15
    Code Analysis ...........................................15
        The Good ............................................16
        The Bad .............................................16
        The Ugly ............................................16

Converting Endian-specific to Endian-neutral Code ...........16

Reversing Endian-specific Architecture of Code ..............16

Conclusion ..................................................17
Soumen Sarkar
I appreciate your contribution, however a white-paper on Endianness (from intel no less) is a bit excessive for this question, furthermore the question is answered and has been for a while. Also, why post the TOC?
Frank V