机器字节存储顺序

 

 

[zt]

(1)http://hi.baidu.com/cppyun/blog/item/9625c8396d5ff7f33b87ce33.html

8086机器都是使用little endian, 而摩托罗拉的power pc使用big endian
对于一个数0x1122
产用little endian方式时  低字节存储0x22,高字节存储0x11.
而使用big endian方式时, 低字节存储0x11, 高字节存储0x22
在这俩种字节方式间转换可以使用汇编指令 BSWAP

测试函数

int IsMyMachineBigEndian()
{
       unsigned short test = 0x1122;
       unsigned char  *cp = (unsigned char *)&test;
       /*printf("/n %x",*cp++);
       printf("/n %x",*cp++);*/

       return (*cp == 0x11);
}


若返回真值,则说明是big endian

PC结果:

     22

     11

(2)Which is Better?

You may see a lot of discussion about the relative merits of the two formats, mostly religious arguments based on the relative merits of the PC versus the Mac. Both formats have their advantages and disadvantages.

In "Little Endian" form, assembly language instructions for picking up a 1, 2, 4, or longer byte number proceed in exactly the same way for all formats: first pick up the lowest order byte at offset 0. Also, because of the 1:1 relationship between address offset and byte number (offset 0 is byte 0), multiple precision math routines are correspondingly easy to write.

In "Big Endian" form, by having the high-order byte come first, you can always test whether the number is positive or negative by looking at the byte at offset zero. You don't have to know how long the number is, nor do you have to skip over any bytes to find the byte containing the sign information. The numbers are also stored in the order in which they are printed out, so binary to decimal routines are particularly efficient.

int IsMyMachineBigEndian()
{
       unsigned short test = 0x1122;
       unsigned char  *cp = (unsigned char *)&test;
       /*printf("/n %x",*cp++);
       printf("/n %x",*cp++);*/

       return (*cp == 0x11);
}

你可能感兴趣的:(Math,汇编,assembly,存储,byte,Numbers)