<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>编译点滴 &#187; 小尾端</title>
	<atom:link href="http://www.lingcc.com/tag/%e5%b0%8f%e5%b0%be%e7%ab%af/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lingcc.com</link>
	<description>编译器、虚拟机、程序设计语言、体系结构、软件调试、操作系统等等</description>
	<lastBuildDate>Sat, 04 Feb 2012 06:56:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>大尾端和小尾端</title>
		<link>http://www.lingcc.com/2010/02/26/10701/</link>
		<comments>http://www.lingcc.com/2010/02/26/10701/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 13:52:22 +0000</pubDate>
		<dc:creator>erlv</dc:creator>
				<category><![CDATA[编译技术]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[MIPS]]></category>
		<category><![CDATA[x86]]></category>
		<category><![CDATA[大尾端]]></category>
		<category><![CDATA[小尾端]]></category>
		<category><![CDATA[虚拟机]]></category>
		<category><![CDATA[龙芯]]></category>

		<guid isPermaLink="false">http://www.lingcc.com/?p=10701</guid>
		<description><![CDATA[大尾端(big endian)和小尾端(little endian)的问题类似于写字时是从右往左写还是从左往右写的问题。说起来不难，但是在计算机世界中，我们必须要规定采用哪种形式，而并无优劣之分，所以演变到现在，就既有大尾又有小尾.X86就是小尾的，龙芯也用的小尾，早期的MIPS使用的是大尾端,这决定了访存的方向问题。 精确的讲，大尾端和小尾端的区别是系统里整数是从右往左表示还是从左往右表示。表示的不同就意味着重要性不同的位位置不同，大尾端从左往右表示，那么权重大的字节在左边即在低地址处，而小尾端相反。不管时处理器，虚拟机还是网路协议都需要规定大小尾端问题。 如图是X86的存储形式，若大小尾端混用，则会一团糟。 对于网络协议，大小尾端必须统一，我们的TCP/IP使用大尾端，也就是说所有电脑的网络端口都要是大尾端，对于电脑CPU是大尾端的，无须太多处理；但若是小尾端的机器，则需要在接受报文时做转换，幸好已经有比较高效的宏来实现，使得这种转换无须太多代价。宏定义如下，实现字节重拍序. #if defined(BIG_ENDIAN) &#038;&#038; !defined(LITTLE_ENDIAN) #define htons(A) (A) #define htonl(A) (A) #define ntohs(A) (A) #define ntohl(A) (A) #elif defined(LITTLE_ENDIAN) &#038;&#038; !defined(BIG_ENDIAN) #define htons(A) ((((uint16_t)(A) &#038; 0xff00) >> 8) &#124; \ (((uint16_t)(A) &#038; 0x00ff) > 24) &#124; \ (((uint32_t)(A) &#038; 0x00ff0000) >> 8) &#124; \ (((uint32_t)(A) &#038; 0x0000ff00) 相关文章： 小例子&#8211;回眸一笑百媚生 WebKit和Firefox的JavaScript性能对比 <a href='http://www.lingcc.com/2010/02/26/10701/'>[...]</a><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F07%2F09%2F11055%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F02%2F26%2F10701%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">龙芯中科公司招聘JAVA虚拟机开发工程师</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F02%2F25%2F10698%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F02%2F26%2F10701%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">好大一筐梅普斯(MIPS)</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F09%2F19%2F11208%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F02%2F26%2F10701%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">GCC即将支持龙芯3A</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F01%2F23%2F9890%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F02%2F26%2F10701%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">zzzz龙芯之火，可以燎原（下）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F01%2F23%2F9889%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F02%2F26%2F10701%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">zz 龙芯之火，可以燎原（上）</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>
<h3>相关文章：</h3><ul>
<li><a href='http://www.lingcc.com/2010/01/21/10663/' rel='bookmark' title='小例子&#8211;回眸一笑百媚生'>小例子&#8211;回眸一笑百媚生</a></li>
<li><a href='http://www.lingcc.com/2010/01/22/10668/' rel='bookmark' title='WebKit和Firefox的JavaScript性能对比'>WebKit和Firefox的JavaScript性能对比</a></li>
<li><a href='http://www.lingcc.com/2010/01/07/10594/' rel='bookmark' title='使用crossdev安装龙芯的O32 gnu交叉工具链'>使用crossdev安装龙芯的O32 gnu交叉工具链</a></li>
<li><a href='http://www.lingcc.com/2010/01/05/10588/' rel='bookmark' title='用crossdev装龙芯的n32 gnu交叉工具链'>用crossdev装龙芯的n32 gnu交叉工具链</a></li>
<li><a href='http://www.lingcc.com/2010/10/13/11318/' rel='bookmark' title='Linux内存的属性'>Linux内存的属性</a></li>
<li><a href='http://www.lingcc.com/2009/12/29/10503/' rel='bookmark' title='GCC初窥'>GCC初窥</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p>大尾端(big endian)和小尾端(little endian)的问题类似于写字时是从右往左写还是从左往右写的问题。说起来不难，但是在计算机世界中，我们必须要规定采用哪种形式，而并无优劣之分，所以演变到现在，就既有大尾又有小尾.X86就是小尾的，龙芯也用的小尾，早期的MIPS使用的是大尾端,这决定了访存的方向问题。</p>
<p>精确的讲，大尾端和小尾端的区别是系统里整数是从右往左表示还是从左往右表示。表示的不同就意味着重要性不同的位位置不同，大尾端从左往右表示，那么权重大的字节在左边即在低地址处，而小尾端相反。不管时处理器，虚拟机还是网路协议都需要规定大小尾端问题。<br />
如图是X86的存储形式，若大小尾端混用，则会一团糟。</p>
<p><a href="http://www.lingcc.com/wp-content/uploads/2010/02/L_EndiannessFigure1.gif"><img class="alignnone size-medium wp-image-10702" title="L_EndiannessFigure1" src="http://www.lingcc.com/wp-content/uploads/2010/02/L_EndiannessFigure1-300x45.gif" alt="" width="300" height="45" /></a><br />
<span id="more-10701"></span><br />
对于网络协议，大小尾端必须统一，我们的TCP/IP使用大尾端，也就是说所有电脑的网络端口都要是大尾端，对于电脑CPU是大尾端的，无须太多处理；但若是小尾端的机器，则需要在接受报文时做转换，幸好已经有比较高效的宏来实现，使得这种转换无须太多代价。宏定义如下，实现字节重拍序.</p>
<p><code><br />
#if defined(BIG_ENDIAN) &#038;&#038; !defined(LITTLE_ENDIAN)</p>
<p>  #define htons(A) (A)<br />
  #define htonl(A) (A)<br />
  #define ntohs(A) (A)<br />
  #define ntohl(A) (A)</p>
<p>#elif defined(LITTLE_ENDIAN) &#038;&#038; !defined(BIG_ENDIAN)</p>
<p>  #define htons(A) ((((uint16_t)(A) &#038; 0xff00) >> 8) | \<br />
                    (((uint16_t)(A) &#038; 0x00ff) << 8))<br />
  #define htonl(A) ((((uint32_t)(A) &#038; 0xff000000) >> 24) | \<br />
                    (((uint32_t)(A) &#038; 0x00ff0000) >> 8)  | \<br />
                    (((uint32_t)(A) &#038; 0x0000ff00) << 8)  | \<br />
                    (((uint32_t)(A) &#038; 0x000000ff) << 24))</p>
<p>  #define ntohs  htons<br />
  #define ntohl  htonl</p>
<p>#else</p>
<p>  #error "Must define one of BIG_ENDIAN or LITTLE_ENDIAN"</p>
<p>#endif</code></p>
<p>参考</p>
<p>http://www.netrino.com/Embedded-Systems/How-To/Big-Endian-Little-Endian</p>
<div style=float:left><!-- JiaThis Button BEGIN -->
<div id="jiathis_style_32x32">
	<a class="jiathis_button_qzone"></a>
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_tqq"></a>
	<a class="jiathis_button_renren"></a>
	<a class="jiathis_button_kaixin001"></a>
	<a href="http://www.jiathis.com/share/" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
	<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" src="http://v2.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END --></div><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F07%2F09%2F11055%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F02%2F26%2F10701%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">龙芯中科公司招聘JAVA虚拟机开发工程师</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F02%2F25%2F10698%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F02%2F26%2F10701%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">好大一筐梅普斯(MIPS)</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F09%2F19%2F11208%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F02%2F26%2F10701%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">GCC即将支持龙芯3A</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F01%2F23%2F9890%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F02%2F26%2F10701%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">zzzz龙芯之火，可以燎原（下）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F01%2F23%2F9889%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F02%2F26%2F10701%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">zz 龙芯之火，可以燎原（上）</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table><p><h3>相关文章：</h3><ul>
<li><a href='http://www.lingcc.com/2010/01/21/10663/' rel='bookmark' title='小例子&#8211;回眸一笑百媚生'>小例子&#8211;回眸一笑百媚生</a></li>
<li><a href='http://www.lingcc.com/2010/01/22/10668/' rel='bookmark' title='WebKit和Firefox的JavaScript性能对比'>WebKit和Firefox的JavaScript性能对比</a></li>
<li><a href='http://www.lingcc.com/2010/01/07/10594/' rel='bookmark' title='使用crossdev安装龙芯的O32 gnu交叉工具链'>使用crossdev安装龙芯的O32 gnu交叉工具链</a></li>
<li><a href='http://www.lingcc.com/2010/01/05/10588/' rel='bookmark' title='用crossdev装龙芯的n32 gnu交叉工具链'>用crossdev装龙芯的n32 gnu交叉工具链</a></li>
<li><a href='http://www.lingcc.com/2010/10/13/11318/' rel='bookmark' title='Linux内存的属性'>Linux内存的属性</a></li>
<li><a href='http://www.lingcc.com/2009/12/29/10503/' rel='bookmark' title='GCC初窥'>GCC初窥</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://www.lingcc.com/2010/02/26/10701/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

