Tell you a computer story from 'Gulliver's Travels.

 


Today's article aims to share fundamental knowledge in computer science (PLC programming): the issue of data storage sequence (byte order) in memory. Not clear? Let me explain in detail: We know that a "bit" is the smallest unit of data storage in a computer, and eight "bits" make up a byte. Two bytes form a "word," with one byte being the low byte and the other byte being the high byte of that "word."

Now, here's the question: When storing an integer (e.g., hexadecimal number: 0x0384) in a "word," there are two possible methods. One method stores "0x03" in the low byte and "0x84" in the high byte, while the other method does the opposite: storing "0x03" in the high byte and "0x84" in the low byte.

Which storage method should be used? This question once sparked a significant debate in the field of computer science. In 1980, British computer scientist Danny Cohen published a paper titled "On Holy Wars and a Plea for Peace." In this paper, Cohen referenced a story from Jonathan Swift's 1726 satirical novel "Gulliver's Travels." The story features a dispute among the subjects of Lilliput over whether to crack open boiled eggs from the big end (Big-Endian) or the small end (Little-Endian). Those advocating for cracking open eggs from the big end were called Big-Endian, while those favoring the small end were called Little-Endian.

In his paper, Danny Cohen used this story as an analogy and introduced the concepts of the Most Significant Bit (MSB) and Least Significant Bit (LSB).

The term "Most Significant Bit (MSB)" refers to the bit with the highest positional value in the binary number system. Conversely, the "Least Significant Bit (LSB)" is the bit with the smallest positional value in the binary number system. For example, in the binary number 1101, the leftmost "1" represents "2" to the power of 3, the second leftmost "1" represents "2" to the power of 2, and the rightmost "1" represents "2" to the power of 0. It is evident that the leftmost "1" has the highest positional value, making it the "Most Significant Bit (MSB)," while the rightmost "1" has the lowest positional value, making it the "Least Significant Bit (LSB)."

When the "Most Significant Bit (MSB)" is stored in the low byte, and the "Least Significant Bit (LSB)" is stored in the high byte, this byte order is called "Big-Endian" byte order. Conversely, when the "Least Significant Bit (LSB)" is stored in the low byte, and the "Most Significant Bit (MSB)" is stored in the high byte, this byte order is called "Little-Endian" byte order.

For example, to store the hexadecimal number 0x01020304 at the starting address 0x100, following the "Big-Endian" byte order would store 0x01 at address 0x100 and 0x04 at address 0x103. On the other hand, following the "Little-Endian" byte order would store 0x01 at address 0x103 and 0x04 at address 0x100, as shown in the diagram below:


Intel x86 processors use the Little-Endian byte order, while ARM series microcontrollers use the Big-Endian byte order. Siemens S7 series PLCs use the Big-Endian storage method. For example, assigning the value W#16#0384 (i.e., 0x0384) to DB801.DBW510, it is observed in online monitoring that the value of DB801.DBB510 is 0x03, and DB801.DBB511 is 0x84. This indicates that the storage method is in Big-Endian byte order.