Cpu's big endian mode little endian mode comparison

First, the origin of big endian mode and little endian mode

There is an interesting story about the origin of the big endian noun, from Jonathan Swift's Gulliver's Travels: the two powers of Lilliput and Blefuscu have been fighting hard for the past 36 months. The reason for the war: Everyone knows that when eating eggs, the original method is to break the larger end of the egg. The grandfather of the emperor at that time can break the finger in this way because he eats eggs in the hour, so his Father, ordered that all the people should eat the eggs, they must first break the smaller end of the egg, the offender is severely punished. Then the people were extremely disgusted with this decree. During the rebellion, one of the emperors was killed and the other lost the throne. The reason for the rebellion was that the king of the other country Blefuscu was instigated. After the rebellion subsided, he fled. This empire refuge. It is estimated that more than 11,000 people have been willing to die several times and will not break the egg to eat eggs. This is actually a satire on the ongoing conflict between Britain and France. Danny Cohen, a pioneer of network protocols, first used these two terms to refer to the byte order, which was widely accepted.

Second, what is big endian and little endian

Big-Endian and Little-Endian are defined as follows: 1) Little-Endian means that the low-order byte is discharged at the low address end of the memory, and the high-order byte is discharged at the high address end of the memory. 2) Big-Endian is that the high byte is discharged at the low address end of the memory, and the low byte is discharged at the high address end of the memory. As an example, the representation of the number 0x12 34 56 78 in memory is:

1) Big endian mode:

Low Address-----------------> High Address 0x12 | 0x34 | 0x56 | 0x78

2) Little endian mode:

Low address ------------------> High address 0x78 | 0x56 | 0x34 | 0x12

It can be seen that the big endian mode and the string storage mode are similar.

3) Here are two specific examples:

The 16-bit wide number 0x1234 is stored in the CPU memory of Little-endian mode (and Big-endian mode) (assuming storage from address 0x4000):

Memory address Little endian mode to store content Big endian storage
0x4000 0x34 0x12
0x4001 0x12 0x34

The 32-bit wide number 0x12345678 is stored in the CPU memory in Little-endian mode and Big-endian mode (assuming storage from address 0x4000):

Memory address Little endian mode to store content Big endian storage
0x4000 0x78 0x12
0x4001 0x56 0x34
0x4002 0x34 0x56
0x4003 0x12 0x78

4) There is no one in the big end and the little end is bad. The respective advantages are the disadvantages of the other side:

Little-endian mode: There is no need to adjust the byte content for forcing data, and the storage of 1, 2, and 4 bytes is the same. Big endian mode: The decision of the sign bit is fixed to the first byte, which is easy to judge positive or negative.

Third, the storage of the array in the case of big endian small end:

Take unsigned int value = 0x12345678 as an example. Let's look at the storage in two byte order. We can use unsigned char buf[4] to represent the value: Big-Endian: The low address stores the high bit, as follows: high address - -------------- buf[3] (0x78) -- low buf[2] (0x56) buf[1] (0x34) buf[0] (0x12) -- high -- ------------- Low address Little-Endian: The lower address stores the lower bits as follows: high address --------------- buf[3] (0x12) -- High buf[2] (0x34) buf[1] (0x56) buf[0] (0x78) -- low -------------- low address

Fourth, why is there a big and small end mode?

This is because in a computer system, we are in bytes, each address unit corresponds to one byte, and one byte is 8 bits. However, in addition to the 8-bit char in the C language, there are 16-bit short type, 32-bit long type (depending on the specific compiler), and, for the processor with more than 8 bits, for example, 16-bit or 32-bit. The processor, since the register width is larger than one byte, there must be a problem if multiple bytes are arranged. As a result, the big endian storage mode and the little endian storage mode are caused. For example, a 16-bit short type x, the address in memory is 0x0010, and the value of x is 0x1122, then 0x11 is the high byte and 0x22 is the low byte. For big endian mode, put 0x11 in the low address, ie 0x0010, and 0x22 in the high address, ie 0x0011. Little endian mode, just the opposite. Our commonly used X86 architecture is the little endian mode, while the KEIL C51 is the big endian mode. Many ARM and DSP are in little endian mode. Some ARM processors can also be selected by hardware to be big endian or little endian.

Five, how to determine the endianness of the machine

You can write a small test program to determine the endianness of the machine:

BOOL IsBigEndian()

{

Int a = 0x1234;

Char b = *(char *)&a; // Determine the starting storage location by forcing the int to a single byte of char. That is equal to taking the low address part where b is equal to a

If( b == 0x12)

{

Return TRUE;

}

Return FALSE;

}

The union order of the union is that all members are stored from the low address. With this feature, the CPU can easily obtain the memory to read and write in the Little-endian or Big-endian mode:

BOOL IsBigEndian()

{

Union NUM

{

Int a;

Char b;

}num;

Num.a = 0x1234;

If( num.b == 0x12 )

{

Return TRUE;

}

Return FALSE;

}

Six, common endian

The general operating system is small endian, and the communication protocol is big endian.

4.1 Endianness of Common CPUs

Big Endian: PowerPC, IBM, SunLittle Endian: x86, DECARM can work in both big endian mode and little endian mode.

4.2 Byte order of common files

Adobe PS – Big EndianBMP – Little EndianDXF (AutoCAD) – VariableGIF – Little EndianJPEG – Big EndianMacPaint – Big EndianRTF – Little Endian In addition, Java and all network communication protocols use Big-Endian encoding.

Seven, how to convert

For word data (16 bits):

#define BigtoLittle16(A) (( ((uint16)(A) & 0xff00) >> 8) | \

(( (uint16)(A) & 0x00ff) << 8))

For double word data (32 bit):

#define BigtoLittle32(A) ((( (uint32)(A) & 0xff000000) >> 24) | \

(( (uint32)(A) & 0x00ff0000) >> 8) | \

(( (uint32)(A) & 0x0000ff00) << 8) | \

(( (uint32)(A) & 0x000000ff) << 24))

Eight, understand the end mode from the perspective of software

From a software perspective, different end mode processors must consider the difference in end mode when transferring data. For network data transfer, you must consider the conversion of the end mode. In the Socket interface programming, the following functions are used for the conversion of the end-end byte order.

#define ntohs(n) //16-bit data type network byte order to host byte order conversion

#define htons(n) //16-bit data type host byte order to network byte order conversion

#define ntohl(n) //32-bit data type network byte order to host byte order conversion

#define htonl(n) //32-bit data type host byte order to network byte order conversion

The network byte order used by the Internet is addressed in big endian mode, and the host byte order varies according to the processor, such as the PowerPC processor using big endian mode and the Pentuim processor using little endian mode. The endian to network byte order of the big endian processor does not need to be converted, at which point ntohs(n)=n, ntohl = n; and the endian to network byte of the little endian processor must be converted, At this point ntohs(n) = __swab16(n), ntohl = __swab32(n). The __swab16 and __swab32 functions are defined as follows.

#define ___swab16(x)

{

__u16 __x = (x);

((__u16)(

(((__u16)(__x) & (__u16)0x00ffU) << 8) |

(((__u16)(__x) & (__u16)0xff00U) >> 8) ));

}

  

  

#define ___swab32(x)

{

__u32 __x = (x);

((__u32)(

(((__u32)(__x) & (__u32)0x000000ffUL) << 24) |

(((__u32)(__x) & (__u32)0x0000ff00UL) << 8) |

(((__u32)(__x) & (__u32)0x00ff0000UL) >> 8) |

(((__u32)(__x) & (__u32)0xff000000UL) >> 24) ));

}

The PowerPC processor provides four instructions, lwbrx, lhbrx, swtbrx, and sthbrx, for processing endian conversions to optimize functions such as __swab16 and __swap32. In addition, the rlwimi directive in the PowerPC processor can also be used to implement functions such as __swab16 and __swap32.

There is also a need to consider end-mode issues when dealing with ordinary files. The 32, 16-bit read and write operations on the file in the big endian processor are different from the little endian processor. From the perspective of software alone, we can't really understand the difference between big and small end mode. In fact, the real understanding of the difference between the big and small end modes must be understood from a system perspective, from the instruction set, registers and data bus, the difference between the big and small end modes.

Nine, understand the end mode from a system perspective

Add two keywords first, MSB and LSB: MSB: MoST Significant Bit ------- Most significant bit LSB: Least Significant Bit ------- Least Significant Bit

The processor is different in hardware design due to end mode issues. From a system perspective, the end-mode problem has different effects on the design of software and hardware. When the big-end mode exists in a processor system, special access to these different end modes must be handled. PowerPC processors dominate the network market. It can be said that most communication devices use PowerPC processors for protocol processing and other control information processing. This may also be the case that most protocols on the network use big endian addressing. the reason. Therefore, in the software design of the network protocol, the processor using the little endian needs to handle the transition of the end mode in the software. The Pentium dominates the personal computer market, so most of the peripherals used in personal computers use the small-end mode, including some PCI bus, Flash and other devices used in network devices, which also requires attention to the end mode conversion in the hardware design.

The small-end peripherals mentioned in this article refer to the registers in such peripherals stored in little endian, such as the configuration space of PCI devices, registers in NOR FLASH, and so on. For some devices, such as DDR granules, there are no registers stored in little endian, so it is logically not necessary to convert the peer mode. In the design, only one-to-one correspondence of the two data buses is required, and no conversion of the data bus is required.

From a practical point of view, processors using little endian mode need to handle end mode conversion in software, because processors with little endian mode do not require any conversion when interconnecting with small endian peripherals. Processors that use big endian mode need to handle end mode conversion during hardware design. The big-end mode processor needs to deal with multiple aspects such as registers, instruction set, data bus and data bus and small-end peripherals to solve the end-mode conversion problem when connecting with small-end peripherals. The processor based on the big and small mode differs in the bit sequence definition of the registers and data bus.

A 32-bit processor in big-end mode, such as the MPC8541 based on the E500 core, defines the most significant bit of its register as 0, and the least significant bit is defined as 31; The 32-bit processor defines the most significant bit of its register as 31 and the lower address as 0. Corresponding to this, the 32-bit processor data bus with big endian mode has the highest bit of 0 and the highest bit is 31; the 32-bit processor with little endian mode has the highest bit of the data bus of 31 and the lowest bit of 0.

The bit sequence of the external bus of the big-end mode processor also follows the same rule. According to the data bus used, 32-bit, 16-bit and 8-bit, the bit sequence of the external bus of the big-end processor is different. In big endian mode, msb of the 32-bit data bus is the 0th bit, MSB is the 0~7 field of the data bus; lsb is the 31st bit, and LSB is the 24th to 31st fields. In small endian mode, the msb of the 32-bit bus is the 31st bit, the MSB is the 31st to 24th bits of the data bus, the lsb is the 0th bit, and the LSB is the 7~0 field. In big endian mode, msb of the 16-bit data bus is the 0th bit, MSB is the 0~7 field of the data bus; lsb is the 15th bit, and LSB is the 8th-15th field. In small endian mode, the msb of the 16-bit bus is the 15th bit, the MSB is the 15th to 7th bits of the data bus, lsb is the 0th bit, and the LSB is the 7~0 field. In big endian mode, msb of the 8-bit data bus is the 0th bit, MSB is the 0~7 field of the data bus; lsb is the 7th bit, and LSB is the 0th-7th field. In the little endian mode, the msb of the 8-bit bus is the 7th bit, the MSB is the 7th to 0th bits of the data bus, the lsb is the 0th bit, and the LSB is the 7~0 field.

From the above analysis, we can know that for 8-bit, 16-bit and 32-bit wide data buses, the position of msb and MSB of the data bus does not change when using big-end mode, and the data bus is used in small-end mode. The lsb and LSB positions will not change.

To this end, the big endian processor typically has 8-bit, 16-bit, and 32-bit memory accesses (including access to peripherals) that contain fields 0-7, MSB. The little endian processor has 8th, 16th, and 32nd bit memory accesses including bits 7~0, and the 7th to 0th field of the little endian mode, LSB. Since the data bus of the large-end processor has different definitions of 8-bit, 16-bit and 32-bit wide data buses, it is necessary to separately discuss how to handle end-mode conversion at the system level. In a big-end processor system, you need to handle the access of the big-endian to small-end peripherals.

Ten, the actual example

Although many times, the work of endian is done by the compiler, but in some small details, you still need to carefully consider, especially in Ethernet communication, MODBUS communication, software portability. Here, give an example of MODBUS communication. In MODBUS, data needs to be organized into data packets. The data in the message is in big endian mode, that is, the low address stores the high bit and the high address stores the low bit. Suppose there is a 16-bit buffer m_RegMW[256], because it is on the x86 platform, so the data in memory is the little endian mode: m_RegMW[0].low, m_RegMW[0].high, m_RegMW[1].low, m_RegMW [1].high... For the sake of discussion, assume m_RegMW[0] = 0x3456; 0x56, 0x34 in memory. The data is now sent. If the data is not sent directly, the data sent at this time is 0x56, 0x34. Modbus is big endian, it will interpret the data as 0x5634 instead of the original data 0x3456, and a catastrophic error will occur. Therefore, before this, you need to convert the little endian data into big endian, that is, exchange high byte and low byte. In this case, you can call the function BigtoLittle16 (m_RegMW[0]) in step 5, and then send it. You can get the right data.


Hydrogel Cutting Machine

The Smart Screen Protector Cutting Machine can help stores reduce the inventory of Screen Protectors. It is mainly used to cut Screen Protectors such as Mobile Phone Screen Protectors, Watch Screen Protectors, Tablet Screen Protectors, Pad Screen Protectors, and personalized fashion Back Films. It is very suitable for personal business or shop drainage.

Universal Screen Protector Cutting Machine has 20000+ cloud data of different specifications and models, adopts massive cloud database, and all data is updated synchronously in the state of networking. You'll have a full range of screen protector models on one machine and cut any Screen Protector model as needed without having to stock up on Screen Protectors for various phone models. No more losing customers due to missing models.

Screen Protector Cutting Machine,Protective Film Cutting Machine,Back Sticker Cutting Machine,Phone Sticker Cutting Machine, Film Cutting Machine

Shenzhen Jianjiantong Technology Co., Ltd. , https://www.jjtscreenprotector.com

Posted on