Least significant byte (LSB) Most significant byte (MSB)

Big-endian machines store the most-significant byte at the lowest-numbered address, with the other bytes following in decreasing order of significance. Little-endian machines store multi-byte numbers in the opposite order: from least-significant to most-significant.

Internet diagram is big-endian.  STM32 ARM-CM3 is little-endian.

这是从wiki的Example

如何用简单的方法检查是大端还是小端

uint8_t IsBigEndian(void)
{
volatile uint32_t temp = 0x0a0b0c0d;
return (*((uint8_t*)(&temp))) == 0x0a;
} void EndianTest(void)
{
if (IsBigEndian() == )
{
std::cout << "it is a big-endian" << std::endl;
}
else
{
std::cout << "it is a little-endian" << std::endl; }
}

从调试也可以看出内存地址和值, 注意0x0a是most-significant byte。

字节顺序其实就是说在计算机内存中怎么存储,是说从左到右,还是从右到左。对于在一个机器内的数据交互,那种字节顺序都没有关系。主要的问题是在不同机器上交互,比如你设计一个总线系统,那么你就要充分考虑你的通讯协议使用大端还是小端。比如说TCP/IP协议它就是是按照big-endian来设计的,因为你不知道网络系统的电脑,路由器,等等的字节顺序,所以在设计通讯协议时就要充分考虑到这一点。

在TCP/IP里有一系列的函数用于字节的转换如htons (covert host byte to network byte short 16bit), htonl (convert host byte to network byte long 32bit), ntohs (network byte to host byte short 16bit), ntohl (convert network byte to host byte long 32bit). 当你设计通讯协议时,这些函数你就要自己实现了。

说到TCP/IP,让我想起一个我们以前遇到的一个问题,我们的panel在客户那里每个星期二凌晨都会自动重启。后来分析是由于客户每个星期二IT部门会用nmap进行扫描,而TCP/IP字节数的问题造成系统死了,后由Watchdog重启。我把以前分析的一些简要内容放在这里(只有英语版)。

Problem Description

The field reports show that panel will lockup and then reboot while doing security port scan by nmap.

Root Cause

After analysis, this problem happens at all panel versions, standalone/networking panel, and happens at other scanning tools (e.g. Tenable Nessus).

The investigation shows that, the problem was caused by unaligned data access during TCP timestamps option parsing in Linux kernel. The reason can be summarized below:

1)      The ARMv5 or earlier MCU (likes panel MCU S3C44B0X ARM7TDMI is belong to ARMv4T), who had limited abilities to access memory that was not aligned on a word (four byte) boundary. According to S3C44B0 datasheet (see page 89 Address Alignment), the MCU is going to ABORT mode which cause the system crash.

2)      In Linux TCP option parsing, the handling did not consider the data unaligned in timestamp option, but TCP options are not guaranteed to be aligned at all.

According to the Linux debug output information when TCP package with timestamps options: The timestamps hold address (register: R8) is 0x0DB074E2, which is not multiple 4(word), the Linux kernel error information is: Unhandled fault: alignment exception (13).

1)      TCP package by nmap

2)      Linux debug output

Solution

The solution is get word value byte one byte when the address is unaligned, which is come from official Linux kernel patch (see appendix). The change is only applied for TCP/IP option parsing and without side effect. The solution had been successfully verified by prototype.

Purpose: for field panels, firmware upgraded is the recommended solution if the panel was be connected to internet or corporate network, and it is not need to upgrade firmware if the panel was not be connected to network.

Appendix

The patch is come from Linux official, the link is https://archive.org/details/git-history-of-linux.

 

Big-endian/Little-endian, LSB/MSB的更多相关文章

  1. MSB与LSB Big Endian Little Endian

    Most Significant Bit, Last(Least) Significant Bit 最高有效位(MSB) 指二进制中最高值的比特.在16比特的数字音频中,其第1个比特便对16bit的字 ...

  2. 彻底搞懂字符编码(unicode,mbcs,utf-8,utf-16,utf-32,big endian,little endian...)[转]

    最近有一些朋友常问我一些乱码的问题,和他们交流过程中,发现这个编码的相关知识还真是杂乱不堪,不少人对一些知识理解似乎也有些偏差,网上百度, google的内容,也有不少以讹传讹,根本就是错误的(例如说 ...

  3. LSB MSB

    #LSB:(Least Significant,Bit) 最低有效位 :MSB(Most Significant Bit):最高有效位,若MSB=1,则表示数据为负值,若MSB=0则表示数据为正. 在 ...

  4. 低字节序和高字节序相互转换(Little Endian/Big Endian)

    这个例子展示了如何转换整形数字的字节顺序,该方法可以用来在little-endian和big-endian之间转换. 说明:Windos(x86,x64)和Linux(x86,x64)都是little ...

  5. 关于Big Endian 和 Little Endian

    Big Endian 和 Little Endian 一.字节序 来自:http://ayazh.gjjblog.com/archives/1058846/ 谈到字节序的问题,必然牵涉到两大CPU派系 ...

  6. 清晰讲解LSB、MSB和大小端模式及网络字节序

    时隔一个月又回到了博客园写文章,很开心O(∩_∩)O~~ 今天在做需求的涉及到一个固件版本的概念,其中固件组的人谈到了版本号从MSB到LSB排列,检索查阅后将所得整理如下. MSB.LSB? MSB( ...

  7. 字符编码笔记:ASCII、Unicode、UTF-8、UTF-16、UCS、BOM、Endian

    转载:http://witmax.cn/character-encoding-notes.html 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问 ...

  8. 06socket编程

    socket可以看成是用户进程与内核网络协议栈的编程接口. socket不仅可以用于本机的进程间通信,还可以用于网络上不同主机的进程间通信. IPv4套接口地址结构通常也称为“网际套接字地址结构”,它 ...

  9. UNIX网络编程——socket概述和字节序、地址转换函数

    一.什么是socket socket可以看成是用户进程与内核网络协议栈的编程接口.socket不仅可以用于本机的进程间通信,还可以用于网络上不同主机的进程间通信. socket API是一层抽象的网络 ...

随机推荐

  1. C/C++知识补充 (1)

    ● C++的圆括号运算符() 下列关于圆括号运算符的功能说法不正确的是(C) . A. 可用于强制类型转换 B 可用于类型构造 C 可用于类型声明 D 可用于函数调用 对大部分可重载的运算符来说,它既 ...

  2. synchronized(七)

    package com.bjsxt.base.sync006; /** * 死锁问题,在设计程序时就应该避免双方相互持有对方的锁的情况 * @author alienware * */public c ...

  3. Java 内存监控(一)之 jps命令

    今天看一下Java命令行工具 jps的使用 一.命令简介 jps [ options ] [ hostid ] 不输入 [ hostid ] 内容,则默认是本机. 二.options选项的内容 -q ...

  4. Linux平台 获取程序依赖文件

    创建sh脚本文件 =========================================================== #!/bin/sh exe=$1 #发布的程序名称des=$2 ...

  5. ios scrollView代理的用法

    // // ZQRViewController.m // 03-图片缩放 // // Created by apple on 17-08-25. // #import "ZQRViewCon ...

  6. http 请求头部解析

    作者:知乎用户链接:https://www.zhihu.com/question/42696895/answer/109035792来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  7. jq设置控件可用不可用

    $("#tj").attr("disabled", true); //不可用 $("#tj").removeAttr("disab ...

  8. shell 中变获取值及运算的几种方法

    num=$(tail ./image/1.txt -n 1) num=$(($num+1))

  9. Selenium之ActionChains (二)

    今天,为大家介绍的是标题中的三个新方法,以及一个老方法 以下方法都需要操作一个名为Keys的包,先来简单认识下 Keys key_down(value,element),key_up(value,el ...

  10. MySQL添加可重复执行列

    一.information_schema数据库表说明: SCHEMATA表:提供了当前mysql实例中所有数据库的信息.是show databases的结果取之此表. TABLES表:提供了关于数据库 ...