Big endian and Little endian representations

2019-03-06 23:12发布

问题:

If I write the following

section .data
    align 4
    X:  db 1
    Y:  dw 5
    Z:  db 0x11
section .text
    add dword [X], 0xAA000101

I'm trying to understand the differences between the big endian and the little endian representations, and I don't understand what will be the value of each variable for each representation? Will they be the same?

回答1:

Have a look at these pictures:

This is the list of endiannesses for all architectures/instruction sets



回答2:

In a big-endian configuration, the most significant byte of a doubleword (32-bits on x86) is stored in the smallest address, and the least significant byte is stored in the largest address.
In a little-endian configuration, the least significant byte is stored in the smallest address.

Let's take the big-endian example first:

If we lay out your variables in memory in a big-endian configuration we get:

; -> Address increases ->

X: 01
Y: 00 05
Z: 11

or, grouped together:

 01 00 05 11
MSB       LSB

When viewed as a 32-bit value that equals 0x01000511. Adding 0x01000511 and 0xAA000101 gives us 0xAB000612. If we view the individual bytes in memory again we get:

; -> Address increases ->
AB 00 06 12

So the result is:

X = 0xAB
Y = 6
Z = 0x12

In a little-endian configuration we would have:

; -> Address increases ->

X: 01
Y: 05 00
Z: 11

or, grouped together:

 01 05 00 11
LSB       MSB

Viewed as a 32-bit value that equals 0x11000501. Adding 0xAA000101 gives us 0xBB000602. And when we view the individual bytes we get:

02 06 00 BB

With the result:

X = 2
Y = 6
Z = 0xBB

(Note: all x86 processors, AFAIK, are little-endian)