Chinaunix首页 | 论坛 | 博客
  • 博客访问: 818166
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

2008-10-13 16:14:29

再网上查阅了很多资料(包括中英版的MSDN),反复体会,才基本弄明白一些Big-Endian和Little-Endian的含义,先总结如下:

Big-Endian 和 Little-Endian 字节排序

字节排序 含义
Big-Endian 一个Word中的高位的Byte放在内存中这个Word区域的低地址处。
Little-Endian 一个Word中的低位的Byte放在内存中这个Word区域的低地址处。

必须注意的是:表中一个Word的长度是16位,一个Byte的长度是8位。如果一个数超过一个Word的长度,必须先按Word分成若干部分,然后每一部分(即每个Word内部)按Big-Endian或者Little-Endian的不同操作来处理字节。

一个例子:
如果我们将0x1234abcd写入到以0x0000开始的内存中,则结果为
                big-endian     little-endian
0x0000     0x12              0xcd
0x0001     0x34              0xab
0x0002     0xab              0x34
0x0003     0xcd              0x12
(注意:0xab换算成2进制是10101011,是个8位的数。我刚才居然当成4位了,自己把自己搞晕了,shit。)

-----------
疑问:为什么要以一个Word为基础单位来分割而不是一个DoubleWord或者Byte?究竟就是这么定义的还是跟具体的CPU有关,跟具体的模式(比如说16位模式、32位模式、64位模式的LE和BE定义会不同)有关?我实在是不清楚,望知道的大侠不吝指点,也希望有这方面资料(文章、代码)的朋友贴一些上来。
--------------------next---------------------
目前的存储器,多以byte为访问的最小单元,于是endian的问题应运而生了,当一个逻辑上的整理必须分割为物理上的若干单元时就存在了先放谁后放谁的问题

存在“如果说"跟word或者说字长根本就没关系",假设有一数据文件里面有N多数顺序排布,如果想以Little-Endian format读入内存某区域,那么应该怎么读?怎么排?”这样的问题是由于对于endian的实质理解的偏差,endian指的是当物理上的最小单元比逻辑上的最小单元小时,逻辑到物理的单元排布关系。这里的“有一数据文件里面有N多数顺序排布”,这个“有一数据”显然不是逻辑上的最小单元,而其中的“N多数”的一个才是逻辑最小单元,于是可应用楼主表格中的原则排列,而“N多数”之间的顺序则是由这“N多数”的宿主决定的,比如是你写的程序,这个顺序由你决定

刚才谈到了,endian指的是当物理上的最小单元比逻辑上的最小单元小时,逻辑到物理的单元排布关系。咱们接触到的物理单元最小都是byte,在通信领域中,这里往往是bit,不过原理也是类似的。

实践可以给你更多的经验,比如在一个嵌入式系统的通信协议中,从底层射频驱动到上层的协议栈全部需要实现,那么很可能遇到多个endian的问题,底层的bit序、协议层的byte序、应用层的byte序,这些都是不同的概念

絮絮叨叨,一些拙见
--------------------next---------------------
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.

--------------------next---------------------

阅读(567) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~