Small-end mode large-end Mode

Source: Internet
Author: User

Embedded system developers should be familiar with the little-Endian and big-Endian modes. For example, the storage method of 16-bit 0x1234 in little-Endian mode CPU memory (assuming that it starts from address 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

 

 

Sometimes you need to know Big end ModeOr small-end mode. The so-called Big end Mode,
It is the low position of the index data stored in the high address of the memory, and the high position of the data is stored in the low address of the memory; the so-called small-end mode, is the low position of the index data stored in the low address of the memory, and the high position of the data is stored in
. 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
C language in addition to 8-bit char, there are 16-bit short type, 32-bit long type (depending on the specific compiler), in addition, for a processor whose digits are greater than 8 bits, for example
For example, 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
A 16-bit short-Type X with a memory address of 0x0010 and a value of 0x1122. Therefore, 0x11 is a high byte and 0x22 is a low byte. For Big end ModePut 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 structure is the small-end mode, while the Keil C51 is Big end Mode. Many arm and DSP are in small-end mode. Some arm processors can also be selected by hardware Big end ModeOr small-end mode. The following code is used to test whether your compiler is Big end ModeOr 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

If X0 = 0x11, it is a large end. If X0 = 0x22, it is a small end ......

  Int main () <br/>{< br/> short int X; <br/> char x0, x1; <br/> x = 0 * (char *) & X); // low address unit, or (char *) & X) [0]; <br/> X1 = * (char *) & x + 1); // high address unit, or (char *) & X) [1]; <br/> printf ("% x/n % x/N", x0, X1); <br/>}  Int checkcpu () <br/>{< br/> Union W <br/> {int A; <br/> short B; <br/>} C; <br/> C. a = 0x12345678; <br/> return (C. B = 0x5678); <br/>}< br/> int main () <br/>{< br/> printf ("% d/N", checkcpu (); <br/>} 

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.