Operating System big-end mode and small-end Mode

Source: Internet
Author: User

  



In C language, besides the 8-bit char type, there are also 16-bit short type, 32-bit long type (depending on the specific compiler), 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 of how to arrange multiple bytes. Therefore, the large-end storage mode and the Small-end storage mode are created.

Big end mode:

The high bytes of the word data are stored in the low address, while the low bytes of the word data are stored in the high address.

Small-end mode:

In contrast to the big-end storage mode, in the small-end storage mode, the low address stores the low byte of word data, and the high address stores the high byte of word data.

For example, the 16-Bit Width 0x1234 is stored in the CPU memory in the small-end mode (assuming it is stored from the address 0x4000:

Memory Address

Zero X 4000

Zero X 4001

Store content

0x34

0x12

In the big-end mode, the CPU memory is stored as follows:

Memory Address

Zero X 4000

Zero X 4001

Store content

0x12

0x34

The 32-Bit Width 0x12345678 is stored in the CPU memory in the small-end mode (assuming it is stored from the address 0x4000) as follows:

Memory Address

Zero X 4000

Zero X 4001

Zero X 4002

Zero X 4003

Store content

0x78

0x56

0x34

0x12

In the big-end mode, the CPU memory is stored as follows:

Memory Address

Zero X 4000

Zero X 4001

Zero X 4002

Zero X 4003

Store content

0x12

0x34

0x56

0x78


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. Note: data is stored in large-end mode in line with human normal thinking, while data is stored in Small-end mode in favor of computer processing. (In my understanding, the small-end mode places a low-byte value in the low-byte mode)

The following code can be used to test whether your compiler is a large-end mode or a small-end mode: int main ()
{
Short int X;
Char x0, x1;
X = 0x1122;

X0 = * (char *) & X); // low address unit, or (char *) & X) [0];
X1 = * (char *) & x + 1); // high address unit, or (char *) & X) [1];

Printf ("X0 = % x \ Nx1 = % x \ n", x0, X1 );
}
If X0 = 0x11, it is a large end. If X0 = 0x22, it is a small end ......


Reference announcement address: http://xfjane.spaces.eepw.com.cn/articles/trackback/item/70351

  The following is a profound introduction:
Big endian and little endian

[Introduction to big endian and little endian]
Byte endian refers to the organization of bytes in the memory, so it is also called byte ordering or byte order.
For an object that spans multiple bytes in data, we must establish such a convention for it:
(1) What is its address?
(2) How are its bytes organized in memory?
For the first question, we have the following explanation:
For objects that span multiple bytes, generally the bytes it occupies are continuous, and its address is equal to the lowest address it occupies. (The linked list may be an exception, but the address of the linked list can be seen as the address of the linked list header ).
For example, if int X is used, its address is 0 × 100. It occupies ox100, 0x101, 0x102, and 0x103 in the memory (32-bit system, so int occupies 4 bytes ).
The above is only the case of memory byte organization: There are two conventions for the organization of multi-byte objects in the memory. Consider a W-bit integer.
The expressions of it are as follows: [Xw-1, Xw-2,..., X1, x0], its
MSB (most significant byte, maximum valid bytes) is [Xw-1, Xw-2,... Xw-8];
LSB (least significant byte, the lowest valid byte) is [X7, X6,..., x0].
The remaining bytes are located between MSB and LSB.

Who is the lowest address in the memory of LSB and MSB, that is, who represents the address of the object?
This leads to the problems of big endian and little endian.
If the LSB is in front of msb and the lsb is a low address, the machine is a small end; otherwise, the machine is a large end.
Dec (Digital Equipment Corporation, now part of Compaq) and Intel machines (x86 platforms) generally use small terminals.
IBM, Motorola (Power PC), and Sun generally use big ends.
Of course, this does not represent all situations. Some CPUs can work on small ends and big ends, such as arm, Alpha, and Motorola PowerPC. For details, refer to the processor manual.
Whether the CPU is large or small depends on the specific settings.
(For example, Power PC supports little-Endian, but the default value is big-Endian)
Generally, most users' operating systems (such as Windows, FreeBSD, and Linux) are little endian. A small portion, such as Mac OS, is big endian.
Therefore, either little endian or big endian is related to the operating system and chip type.

In Linux, you can find the byte_order (or
_ Byte_order, _ byte_order) to determine its value. Byte_order is called byte order. This value can be found in the endian. h or machine/endian. h file. Sometimes in feature. H, different operating systems may be different.

For a value ranging from 0 to 1122
When using the little endian method, the low-byte storage is 0 × 22, and the high-byte storage is 0 × 11.
In the big endian mode, the low-byte storage is 0 × 11, and the high-byte storage is 0 × 22.

The above description was inaccurate only after a netizen corrected it.

After thinking about it, I think the following description may be more appropriate:

When using the little endian method to store data, the LSB of the data is the least significant data bit, which is stored in the low address location. Here, the LSB is 22. That is,

Low address storage 0 × 22, high address storage 0 × 11

When using the big endian method to store data, the most meaningful data bit of MSB is stored in the low address location. Here, MSB is 11, that is

Low address storage 0 × 11, high address storage 0 × 22

Note:

1) The so-called MSB (most significant byte) name is very complicated. I don't know if anyone understands it. I was confused when I started to see this word, but I didn't fully understand it. in fact, MSB is the most important person in a number,

For example, 12004, a Chinese reader, twelve thousand and four, that is, 1 of the highest digit represents 10 thousand. Here it is called MSB, the most meaningful bit.

2) Common Data Storage: When written in text, the content writing format is mostly from low address to high address (more in line with human thinking)

For example, a hexadecimal number is 0x11 22 33, and the storage location is

Address 0 × 3000 store 11

Address 0 × 3001 stores 22

Store 33 in address 0 × 3002

After the connection, the address 0x3000-0x3002 stores the DATA 0x112233.

This storage and Representation Method exactly matches the big end.

The explanation is a bit messy. I hope someone can understand it.

If there is any error, please correct it. Thank you.

[Use a function to determine whether the system is big endian or little endian]
Bool isbig_endian ()
// If the byte order is big-Endian, true is returned;
// If the value is little-Endian, false is returned.
{
Unsigned short test = 0 ×1122;
If (* (unsigned char *) & Test) = 0 × 11)
Return true;
Else
Return false;

} // Isbig_endian ()

From http://www.cnblogs.com/okaimee/archive/2010/07/19/1780609.html

========================================================== ======================================

========================================================== ======================================

Http://wxxweb.blog.163.com/blog/static/135126900201022133740759/

Big-end mode and small-end mode I. Concepts and details 

In computer systems of various systems, there are usually two types of byte storage mechanisms: Big-Endian and little-Endian, that is, big-end mode and small-end mode.

Let's review two keywords: MSB and LSB:

MSB: most significant bit --- maximum valid bit

LSB: least significant bit --- minimum valid bit

Big-edian big-Endian: MSB is stored on the lowest-end address.

For example, if the double byte count is 0 × 1234, the starting address 0 × 00002000 exists in the big-Endian mode:

| Data | <-address

| 0 × 12 | <-0 × 00002000

| 0 × 34 | <-0 × 00002001

In big-Endian, the sequence number arrangement in bit sequence is as follows (taking the number of double bytes as an example ):

Bit | 0 1 2 3 4 5 6 7 | 8 9 10 11 12 13 14 15

MSB------------LSB

Val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 |

+ --------------- +

= 0x8 B 8

Little-Endian: LSB is stored on the lowest-end address.

For example, if the number of double bytes is 0 × 1234, the starting address 0 × 00002000 exists in the form of little-Endian:

| Data | <-address

| 0 × 34 | <-0 × 00002000

| 0 × 12 | <-0 × 00002001

In little-Endian, the sequence number arrangement in the bit sequence is the opposite of the big-Endian sequence. The method is as follows (taking the number of Double Bytes 0 × 8b8a as an example ):

Bit | 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0

MSB------------LSB

Val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 |

+ --------------- +

= 0x8 B 8

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

Taking unsigned int value = 0 × 12345678 as an example, we can use unsigned char Buf [4] to represent the value: Big-Endian in two bytes order: low address storage is high, as shown below:

High address

-----

Buf [3] (0 × 78)-low position

Buf [2] (0 × 56)

Buf [1] (0 × 34)

Buf [0] (0 × 12)-high

-----

Low address

Little-Endian: Low address storage, as follows:

High address

-----

Buf [3] (0 × 12)-high

Buf [2] (0 × 34)

Buf [1] (0 × 56)

Buf [0] (0 × 78)-low position

-----

Low address

From: http://blog.163.com/?email protected]/blog/static/49955027201161210511469/

Operating System big-end mode and small-end Mode

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.