Parsing the size-end mode from the memory stack allocation pattern

Source: Internet
Author: User


Today, I found a whole day of data and tried to understand the differences between the large and small end models, but I still don't understand it after a long time. Later, I tried to identify the differences from the memory allocation pattern.Memory allocation pattern in large and small-end ModeAfter comparison, you can understand it.

First post the basic knowledge:

The so-called Big-Endian mode isLow index data is stored in the memory's high address.The data is stored in a low address in the memory. This storage mode is similar to processing data as strings: The address increases from small to large, the data is placed from high to low;
The so-called little-Endian mode isLow index data is stored in the low memory address.And the data is stored in the high address of the memory. This storage mode effectively combines the high and low addresses with the data bit weights, the weight of the lower part is low, which is consistent with our logic method.

The following code can be used to test whether your compiler is a large-end mode or a small-end mode:
Short int X;
Char x0, x1;
X = 0x1122;
X0 = (char *) & X) [0]; // low address Unit
X1 = (char *) & X) [1]; // high address Unit
Cout If X0 = 0x11, it is a big end;

If X0 = 0x22, it is a small terminal;
The above program can also see that data addressing uses a low byte address.



Below we will post two images to differentiate the size-end mode from the memory allocation pattern

The first figure isSmall-end ModeMemory allocation pattern below:

 



The second figure isLargeTerminal ModeMemory allocation pattern below:


The memory allocation pattern in the size-end mode is:

Stack in high memory or low memory extended down(I hope you can understand it based on the picture)

If the High address is onSmall-end Mode(My computer is a small-end mode, so my next inference-generally, PC is a small-end mode, because PC is easy to process data in the small-end mode ).

If the low address is onBig end Mode.


Well, let's look at two examples with the above inference.


Example 1:

The number of 16bit widths is 0x1234.


In little-Endian mode, the storage method in CPU memory (assuming that the address is stored from 0x4000) is:

Memory Address

Zero X 4000

Zero X 4001

Store content

0x34

0x12



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

Memory Address

Zero X 4000

Zero X 4001

Store content

0x12

0x34


Resolution: in the small-end mode, because the high address is on, the stack is extended to the low address. Therefore 34(Coming first) Low addressPlace pressure on the stack, and then shift 12 (later) High addressPlace pressure Stack

In the big-end mode, because the low address is on, the stack is extended to the high address. So we should first forward 34 (first come)High addressPlace pressure on the stack, and then shift 12 (later)Low addressPlace pressure Stack

32-Bit Width 0x12345678


In little-Endian mode, the storage method in CPU memory (assuming that the address is stored from 0x4000) is:

Memory Address

Zero X 4000

Zero X 4001

Zero X 4002

Zero X 4003

Store content

0x78

0x56

0x34

0x12



In big-Endian 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


Resolution: I will not parse this example. I hope my friends who can understand my article can infer it by themselves.


Conclusion:

Big-Endian is a big-Endian mode that stores low data records in high memory addresses, while high data records are stored in low memory addresses.
The little-Endian mode stores low data points in the memory address while the data points in the memory address.


This article is written by cout_sev.

Some documents are taken from the Internet. If any errors occur, please correct them.

Reprinted please indicate the source: http://blog.csdn.net/cout_sev/

Thank you for your cooperation!





Parsing the size-end mode from the memory stack allocation pattern

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.