43
0x0000000000400507 <main+28>:    74 0c  je     0x400515 <main+42>
0x0000000000400509 <main+30>:    bf 28 06 40 00 mov    $0x400628,%edi

..

0x400507 <main+28>: 0x28bf0c74

I think shows the machine code is big-endian. Is my conclusion right?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
kern
  • 2,295
  • 4
  • 17
  • 15

2 Answers2

96

No, Intel CPUs are little endian: http://en.wikipedia.org/wiki/Endianness

linuts
  • 6,608
  • 4
  • 35
  • 37
  • 5
    Oops,seems I'm reading the endianness in the wrong direction,AGAIN...Is there a trick to avoid this kind of problem? – kern May 16 '11 at 13:53
  • 4
    @kern: little-endian has the least significant byte first, which is the opposite of written numbers where the least significant digit is last. so having to reverse 0x400628 (actually 0x00400628) to get 28 06 40 00 means its little endian. – linuts May 16 '11 at 13:59
  • 2
    It helps to understand that because computers print on the screen from left to right starting with the lowest memory address in a string buffer, the lowest memory address will be on the left, and the highest will be on the right. – James M. Lay May 09 '15 at 21:57
  • 16
    ...therefore, little endian is "little end gets printed first", and big endian is "big end gets printed first". Idk, probably doesn't help... – James M. Lay May 09 '15 at 21:59
  • 1
    Endianness determine which byte ends the encoded representation in memory. In other words, which byte uses the largest memory address. If it's the Least Significant Byte, you get little-endian encoding. If it's the Most Significant Byte, you get big-endian encoding. – Sylvain Rodrigue Jun 08 '16 at 09:18
  • 1
    @SRO: it's exactly opposite: little-endian means the least significant byte is put in the lowest memory address. In other words, endianess means which byte starts a number. – pkalinow Aug 29 '17 at 13:25
  • 49
    Little-endian should have been called little-startian. – Andrew Bainbridge Dec 18 '17 at 14:13
  • The trick I personally use to figure out little endian is something called '3Ls' -- (lowest address) - (lsb) - (little endian) ; might sound silly, but hey, works for me! :) – Sanjay T. Sharma May 18 '23 at 17:32
37

First, you need to know that the smallest data unit that nearly all modern CPUs can manipulate is a byte, which is 8 bits. For numbers, we (human beings) write and read from left to right and we write the most significant digit first, so the most significant digit is on the left.

Little-endian byte order implies two things for the CPU:

  1. Suppose that the CPU fetches 4 bytes from the memory, e.g. starting at address 0x00, and that:

    • address 0x00 holds the byte 11111111, which is 0xFF;
    • address 0x01 holds the byte 00111100, which is 0x3C;
    • address 0x02 holds the byte 00011000, which is 0x18;
    • address 0x03 holds the byte 00000000, which is 0x00.

    Then, when the CPU interprets these 4 bytes as an integer, it will interpret them as the integer value 0x00183CFF. That is, the CPU will consider the byte at the highest address as the Most Significant Byte (MSB). That means, for the CPU, the higher the address is, the more significant the byte on that address is.

memory address:           0x00      0x01      0x02      0x03
binary value at address:  11111111  00111100  00011000  00000000
equivalent value in hex:  = 0xFF    = 0x3C    = 0x18    = 0x00
  1. The same thing applies when the CPU writes an integer value like 0x00183CFF to the memory. It will put 0xFF at the lowest address, and 0x00 at the highest address. If you (human being) read bytes intuitively from low addresses to high addresses, you read out FF 3C 18 00, which byte-for-byte is in the reverse order to how 0x00183CFF is written.

For BIG-endianness, the CPU reads and writes MSBs at lower addresses and LSBs at higher addresses.

Jivan Pal
  • 182
  • 10
Gab是好人
  • 1,976
  • 1
  • 25
  • 39