Big-End mode & Small-terminal mode, host sequence & network sequence, and high and low address of the stack

Source: Internet
Author: User
Tags htons

first, big end mode & Small terminal mode

The so-called "big-end mode" refers to the low data (that is, the number of lower weights) stored in the high memory address , and the high level of data, stored in the low memory address , Such a storage pattern is somewhat similar to processing data as a string order: The address is increased from small to large, and data is placed from high to low;

The so-called "small-end mode", refers to the low data stored in the memory of the lower address , and the high level of data stored in the memory of the higher address , this storage mode of the address of the height and data rights effectively combined, The high address part weight value is high, the low address part weight value is low, and our logical method is consistent.

If a 32-bit integer 0x12345678 is stored in an integer variable (int), the integer variable is stored in memory in a big-end or small-ended mode as shown in the following table. For simplicity, this article uses OP0 to represent the highest byte MSB (Most significant byte) of a 32-bit data, using OP3 to represent a 32-bit data minimum byte LSB (least significant byte).

Address offset big End mode small port mode
0x00 (OP0) (OP3)
0x01 (OP1) (OP2)
0x02 (OP2) (OP1)
0X03 (OP3) (OP0)


Small ends: Higher valid bytes are stored at higher memory addresses, and lower valid bytes are stored in lower memory addresses.
Big-endian: The higher valid bytes are stored in the lower memory address, and the lower valid byte is stored in the higher memory address.

The main difference between storing the data in the size mode is the byte order in which it is stored, and the big end way will store the high position in the low address, and the small way will store the high position in the higher address. The data storage in the big end is in accordance with the normal thinking of human being, and the data stored in the small port is beneficial to computer processing. so far, the use of big or small end of data storage, which is not good or not conclusive.

The following code can be used to test whether your compiler is big-end or small-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 big-endian; If x0=0x22, it is small end ...
The above program also shows that when data is addressed, the address of the Low-order byte is used

Second, host Order & network Order

Different CPUs have different byte-order types These are the order in which integers are stored in memory. This is called the host order, the most common of which are two kinds:
1. Little endian: Storing the Low-order bytes at the starting address
2, big endian: high-order bytes stored in the starting address

network byte order is a set of data representation format in TCP/IP, which is independent of specific CPU type, operating system, etc., so that data can be interpreted correctly when transmitting between different hosts. The network byte order takes the big endian sort way.

The transformation of the BSD socket provides the following four functions for conversion:
Htons converts the unsigned short type from the host sequence to the network sequence
HTONL converts the unsigned long type from the host sequence to the network sequence
Ntohs converts the unsigned short type from the network sequence to the host sequence
Ntohl converts the unsigned long type from the network sequence to the host sequence


In systems using little endian, these functions convert the byte order
In systems that use the big endian type, these functions are defined as empty macros

Similarly, when developing a Web program, or Cross-platform development, you should also pay attention to ensuring that only one byte sequence is used, otherwise the interpretation of the two parties will produce a bug.

Note:
1, network and host byte conversion function: Htons ntohs htonl Ntohl (S is short L is long H is host n is network)
2, different CPUs run different operating systems, the byte sequence is also different , see the following table:


Processor OS byte sort
Alpha all Little endian
Hp-pa NT Little Endian
Hp-pa UNIX Big Endian
Intelx86 all Little Endian <-----x86 system is a small-end byte-order system
motorola680x () All big endian
MIPS NT Little Endian
MIPS UNIX Big Endian
PowerPC NT Little Endian
PowerPC non-NT big endian <-----PPC system is a big-endian byte-order system
rs/6000 UNIX Big Endian
SPARC UNIX Big Endian
IXP1200 Arm Core All Little endian

The following is an easy way to check native byte order:

To determine the byte order of this machine
Returns a small sequence of true tables. Returns false as a large segment sequence
BOOL Am_little_endian ()
{
unsigned short i=1;
return (int) * ((char *) (&i))? True:false;
}
int main ()
{
if (Am_little_endian ())
{
printf ("Native byte order is small De She!\n");
}
Else
{
printf ("Native byte sequence is a large sequence!\n");
}
return 0;
}

third, the high and low address of the stack

The

stack is a specific storage area specified in memory, where the address of the starting cell is called stack bottom , the current storage cell address is called stack top , and once the stack store is specified, the bottom of the stack is fixed, and the top of the stack is with the stack, The stack operation is dynamic. and different models of the stack design, there are two situations: one is a number of stacks, the top of the stack address plus 1, each stack a number, the top address of the stack minus 1, that is, the stack area is by the low address of memory to high address. The other is a number of each stack, the top address of the stack minus 1, each stack a number, the top of the stack address plus 1, that is, the stack area is the high address of memory to the low address . The high address, low address is relative, that is, the size of the corresponding address encoding .

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.