Is x86-64 machine language big endian?
Asked Answered
H

2

46
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?

Hosey answered 16/5, 2011 at 13:48 Comment(0)
R
97

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

Recalesce answered 16/5, 2011 at 13:49 Comment(8)
Oops,seems I'm reading the endianness in the wrong direction,AGAIN...Is there a trick to avoid this kind of problem?Hosey
@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.Recalesce
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.Eckhardt
...therefore, little endian is "little end gets printed first", and big endian is "big end gets printed first". Idk, probably doesn't help...Eckhardt
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.Bullis
@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.Wilt
Little-endian should have been called little-startian.Barela
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! :)Nuss
I
38

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.

Isocracy answered 29/12, 2016 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.