Details about the big-end mode and small-end mode (reprinted)

Source: Internet
Author: User
Details about the big-end mode and small-end Mode

Reprinted from: http://blog.csdn.net/ce123_zhouwei/article/details/6971544

I. Origins of big-end mode and small-end Mode

There is an interesting story about the origins of the big-end and small-end terms, from Jonathan Swift's "garifo Travelogue": Lilliput and blefuscu, which have been fighting hard for the past 36 months. The reason for the war: As we all know, when eating eggs, the original method was to break the big end of the eggs. At that time, the grandfather of the emperor had to break his fingers in this way because he had eaten eggs in small hours, therefore, his father ordered all people to break the small end of the eggs before they could eat them. Then the common people were very disgusted with this Decree. During this period, many rebels occurred. One of the emperors sent their lives and the other lost their throne. The reason for the rebellion was the instigation of the King of blefuscu from another country, after the rebellion subsided, he fled to this empire for refuge. It is estimated that more than 11000 people are willing to die several times and refuse to break the eggs with smaller ones. In fact, it is ironic that the conflict between Britain and France continued at that time. Danny Cohen, a pioneer in network protocols, used these two terms for the first time to refer to the byte sequence and was later widely accepted.
 

2. What are big ends and small ends?

The definitions of big-Endian and little-Endian are as follows:
1) Little-Endian is the low-byte emission at the low-address end of the memory, and the high-byte emission at the High-address end of the memory.
2) Big-Endian refers to the low address of high bytes discharged in the memory, and the low byte is discharged in the high address of the memory.
For example, the representation of the number 0x12 34 56 78 in the memory is as follows:

1) big end mode:

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

2) Small-end mode:

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

It can be seen that the large-end mode is similar to the string storage mode.

3) The following are two examples:

 

The 16-Bit Width 0x1234 is stored in the little-Endian mode (and big-Endian mode) CPU memory (assuming it starts from the address 0x4000) as follows:

 

Memory Address Content storage in Small-end Mode Content storage in big-end Mode
Zero X 4000 0x34 0x12
Zero X 4001 0x12 0x34

32bit-width 0x12345678 in little-Endian mode and big-Endian mode:

Memory Address Content storage in Small-end Mode Content storage in big-end Mode
Zero X 4000 0x78 0x12
Zero X 4001 0x56 0x34
Zero X 4002 0x34 0x56
Zero X 4003 0x12 0x78
 

4) no one is better than the other, and their respective advantages are the disadvantages of the other:

Small-end mode: you do not need to adjust the byte content for forced conversion of data. The storage methods of 1, 2, and 4 bytes are the same.
Large-end mode: the determination of the symbol bit is fixed to the first byte, and it is easy to judge positive and negative.

 

3. Storage of arrays in large-end and small-end scenarios:

Taking unsigned int value = 0x12345678 as an example, we can use unsigned char Buf [4] to show the storage conditions of the two types of bytes respectively:
Big-Endian: Low-address storage is high, as shown below:
High address
---------------
Buf [3] (0x78) -- low
Buf [2] (0x56)
Buf [1] (0x34)
Buf [0] (0x12) -- high
---------------
Low address
Little-Endian: Low address storage, as follows:
High address
---------------
Buf [3] (0x12) -- high
Buf [2] (0x34)
Buf [1] (0x56)
Buf [0] (0x78) -- low
--------------
Low address

 

 

4. Why are there differences between large and small models?

This is because in computer systems, we use bytes. Each address unit corresponds to one byte, and each byte is 8 bit. However, in C language, apart from 8-bit char, there are 16-bit short and 32-bit long (depending on the specific compiler). In addition, for a processor whose digits are greater than 8 bits, for example, for a 16-bit or 32-bit processor, because the register width is greater than one byte, there must be a problem if multiple bytes are arranged. Therefore, the large-end storage mode and the Small-end storage mode are created. For example, if the address of a 16-bit short X in the memory is 0x0010 and the value of X is 0x1122, 0x11 is a high byte and 0x22 is a low byte. In the big-end mode, place 0x11 in the low address, that is, 0x0010, and 0x22 in the high address, that is, 0x0011. The small-end mode is the opposite. The commonly used X86 architecture is the small-end mode, while the Keil C51 mode is the large-end mode. Many arm and DSP are in small-end mode. Some arm processors can also choose big-end mode or small-end mode from hardware.

 

5. How to Determine the machine's byte order

You can write a small test program to determine the machine's byte order:

[CPP]View plaincopyprint?
  1. Bool isbigendian ()
  2. {
  3. Int A = 0x1234;
  4. Char B = * (char *) & A; // convert the int type to a single char byte and determine the initial storage location. That is, take the low address part of B equal to.
  5. If (B = 0x12)
  6. {
  7. Return true;
  8. }
  9. Return false;
  10. } <Span style = "font-family: Arial, verdana, sans-serif; white-space: normal; Background-color: RGB (255,255,255);"> </span>

The storage order of union Union is that all members are stored from the low address. With this feature, the CPU can easily read and write the memory in the little-Endian or big-Endian mode:

[CPP]View plaincopyprint?
  1. Bool isbigendian ()
  2. {
  3. Union num
  4. {
  5. Int;
  6. Char B;
  7. } Num;
  8. Num. A = 0x1234;
  9. If (Num. B = 0x12)
  10. {
  11. Return true;
  12. }
  13. Return false;
  14. } <Span style = "font-family: Arial, verdana, sans-serif; white-space: normal; Background-color: RGB (255,255,255);"> </span>
6. Common byte order

Generally, the operating system is a small client, while the communication protocol is a large client.

4.1 bytes of common CPU

Big endian: PowerPC, IBM, sun
Little endian: x86, Dec
Arm can work in both the big-end mode and the Small-end mode.
 

4.2 byte order of common files

Adobe PS-big endian
BMP-little endian
DXF (AutoCAD)-Variable
GIF-little endian
JPEG-big endian
Macpaint-big endian
RTF-little endian
 
In addition, Java and all network communication protocols use big-Endian encoding.
 

7. How to convert

For word data (16 bits ):

[CPP]View plaincopyprint?
  1. # Define bigtolittle16 (A) (uint16) (a) & 0xff00)> 8) | \
  2. (Uint16) (a) & 0x00ff) <8 ))

For double-character data (32-bit ):

 

[CPP]View plaincopyprint?
  1. # Define bigtolittle32 (A) (uint32) (a) & 0xff000000)> 24) | \
  2. (Uint32) (a) & 0x00ff0000)> 8) | \
  3. (Uint32) (a) & 0x0000ff00) <8) | \
  4. (Uint32) (a) & 0x000000ff) <24 ))

 

8. Understanding the end mode from the software perspective

From the software perspective, different end-mode processors must consider different data transmission modes. For network data transmission, you must consider switching the terminal mode. In socket interface programming, the following functions are used to convert the size to the byte order.

[CPP]View plaincopyprint?
  1. # Define ntohs (n) // switch the byte sequence of the 16-bit data type network to the host byte sequence
  2. # Define htons (n) // converts the byte sequence of a 16-bit data type host to the byte sequence of the network.
  3. # Define ntohl (n) // 32-bit data type network byte order to host byte order conversion
  4. # Define htonl (n) // 32-bit data type host byte sequence to network byte sequence Conversion


The Network byte sequence used by the Internet uses the big-end mode for addressing, while the host byte sequence varies depending on the processor. For example, the PowerPC processor uses the big-end mode, while the pentuim processor uses the small-end mode.
There is no need to convert the byte sequence of the large-end mode processor to the byte sequence of the network. In this case, ntohs (n) = N, ntohl = N; however, the bytes in the small-end mode processor must be converted to network bytes. In this case, ntohs (n) = _ swab16 (N), ntohl = _ swab32 (n ). The _ swab16 and _ swab32 functions are defined as follows.

[CPP]View plaincopyprint?
  1. # Define ___ swab16 (X)
  2. {
  3. _ 2010__ x = (X );
  4. (_ 002 )(
  5. (_) (_ X) & (_) 0x00ffu) <8) |
  6. (_) (_ X) & (_) 0xff00u)> 8 )));
  7. }
  8. # Define ___ swab32 (X)
  9. {
  10. _ U32 _ x = (X );
  11. (_ U32 )(
  12. (_ U32) (_ x) & (_ u32) 0x000000fful) <24) |
  13. (_ U32) (_ x) & (_ u32) 0x0000ff00ul) <8) |
  14. (_ U32) (_ x) & (_ u32) 0x00ff0000ul)> 8) |
  15. (_ U32) (_ x) & (_ u32) 0xff000000ul)> 24 )));
  16. }


The PowerPC processor provides four commands, lwbrx, lhbrx, stwbrx, and sthbrx, to process the conversion of byte order to optimize functions such as _ swab16 and _ swap32. In addition, the rlwimi command in the PowerPC processor can be used to implement functions such as _ swab16 and _ swap32.

You also need to consider the terminal mode when processing common files. The results of 32-or 16-bit read/write operations on files under the large-end mode processor are different from those in the small-end mode. From the perspective of software, we cannot really understand the differences between large and small models. In fact, to really understand the differences between the big and small modes, you must thoroughly understand the differences between the big and small modes from the perspective of the system, from the instruction set, registers, and data bus.

 

9. From the system perspective, we need to add two keywords: MSB and LSB:
MSB: Most Significant Bit ------- maximum valid bit
LSB: least significant bit ------- minimum valid bit

The processor is different in the design due to the terminal mode issues on the hardware. From the perspective of the system, the end mode problem has different effects on the design of software and hardware. When a processor system has a large or small end mode at the same time, you must perform special processing on these accesses in different client modes.
PowerPC processors dominate the network market. It can be said that most communication devices use PowerPC processors to process protocols and other control information, this may also be the reason why most of the protocols on the network adopt the large-end addressing method. Therefore, in software design related to network protocols, a small-End Processor needs to change the processing mode in the software. Pentium leads the man-machine market, so most of the peripherals used for man-machine use the small-end mode, including some PCI bus, flash and other devices used in network equipment, this also requires that you pay attention to the terminal mode conversion in the hardware design.
The small-end peripherals mentioned in this Article refer to the memory of these peripherals in the small-end mode, such as the configuration space of the PCI device and the registers in the nor flash. Some devices, such as DDR particles, do not have registers stored in the small-end mode. Therefore, logically, the peer mode is not required to be converted. In the design, you only need to interconnect the data bus between the two parties one-to-one, instead of converting the data bus.
From the perspective of practical application, a processor using the small-end mode needs to process the switching of the terminal mode in the software, because when the processor using the small-end mode is interconnected with the small-end peripherals, no conversion is required. The processor using the big-end mode needs to process the switching of the terminal mode during hardware design. The large-end mode processor must process registers, instruction sets, data bus, and connection between the data bus and small-end peripherals to solve the terminal mode conversion problem when connecting to the small-end peripherals. In the definition of register and Data Bus order, the processor based on the size-end mode is different.
A 32-bit processor in the big-end mode, such as the mpc8541 Based on the e500 kernel, defines the highest bit MSB (most significant bit) of its register as 0, and the lowest Bit LSB (lease significant bit) the value is 31, while the 32-bit processor in the small-end mode defines the highest bit of its register as 31, and the low-end address as 0. Correspondingly, the maximum bit of the 32-bit processor data bus in the big-end mode is 0, the maximum bit is 31, and the maximum bit of the 32-bit processor data bus in the small-end mode is 31, the second bit is 0.
The level order of the external bus of the big-and-small-end mode processor follows the same rule. According to the data bus used, it is 32-bit, 16-bit, and 8-bit, the order of the size-end processor's external bus is different. In big-end mode, the MSB Of the 32-Bit Data Bus is 0th bits, and MSB is 0th ~ While LSB is a 31st-bit field and LSB is a 24th-bit field ~ 31. In the small-end mode, the MSB value of the 32-bit bus is 31st bits, and the MSB value is 31st ~ of the Data Bus ~ 24-bit, LSB is 0th-bit, and LSB is 7-bit ~ Field 0. In big-end mode, the MSB of the 16-Bit Data Bus is 0th bits, and MSB is 0th ~ While LSB is a 15th-bit field and LSB is a 8th-bit field ~ 15 fields. In the small-end mode, the MSB value of the 16-bit bus is 15th bits, and the MSB value is 15th ~ of the Data Bus ~ 7 bits, LSB is 0th bits, and LSB is 7 ~ Field 0. In big-end mode, the MSB of the 8-Bit Data Bus is 0th bits, and MSB is 0th ~ While LSB is a 7th-bit field and LSB is a 0th-bit field ~ Field 7. In the small-end mode, MSB is 7th bits and MSB is 7th ~ 0-bit, LSB is 0th-bit, and LSB is 7-bit ~ Field 0.
From the above analysis, we can know that for 8-bit, 16-bit, and 32-bit data bus, the location of the MSB and MSB of the data bus will not change when using the big-end mode, the location of the LSB and LSB of the Data Bus does not change when the data bus is used in the small-end mode.
Therefore, large-end processors generally have 0th to access to 8-bit, 16-bit, and 32-Bit Memory (including access to peripherals ~ Field 7, that is, MSB. The small-end processor supports 8-bit, 16-bit, and 32-Bit Memory Access by 7th ~ 0 bits, 7th ~ 0 field, that is, LSB. Because the 8-bit, 16-bit, and 32-bit data bus of the big-size processor have different definitions, we need to discuss how to handle the terminal mode conversion at the system level. In a large-end processor system, you need to process access from a large-end processor to small-end peripherals.

 

10. Actual examples

Although many times the work of the byte sequence has been completed by the compiler, but in some small details, you still need to carefully consider it, especially in Ethernet communication, Modbus communication, and software portability. Here is an example of Modbus communication. In Modbus, data needs to be organized into data packets. The data in the message is in the big-end mode, that is, low-address storage and high-address storage. Assume that there is a 16-bit buffer m_regmw [256]. Because it is on the X86 platform, the data in the memory is in the small-end mode: m_regmw [0]. low, m_regmw [0]. high, m_regmw [1]. low, m_regmw [1]. high ......
For convenience, assume m_regmw [0] = 0x3456; 0x56, 0x34 in memory.
The data is sent. If no data conversion is performed, the data sent is 0x56, 0x34. Modbus is a big end, and the data is interpreted as 0x5634 instead of 0x3456 of the original data. Then, a catastrophic error occurs. Therefore, before that, you need to convert the small-end data to a large-end, that is, exchange the High-byte and Low-byte data. At this time, you can call the function bigtolittle16 (m_regmw [0]) in step 5. then send the message to obtain the correct data.

Details about the big-end mode and small-end mode (reprinted)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.