Big-endian/Little-endian, LSB/MSB
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的更多相关文章
- MSB与LSB Big Endian Little Endian
Most Significant Bit, Last(Least) Significant Bit 最高有效位(MSB) 指二进制中最高值的比特.在16比特的数字音频中,其第1个比特便对16bit的字 ...
- 彻底搞懂字符编码(unicode,mbcs,utf-8,utf-16,utf-32,big endian,little endian...)[转]
最近有一些朋友常问我一些乱码的问题,和他们交流过程中,发现这个编码的相关知识还真是杂乱不堪,不少人对一些知识理解似乎也有些偏差,网上百度, google的内容,也有不少以讹传讹,根本就是错误的(例如说 ...
- LSB MSB
#LSB:(Least Significant,Bit) 最低有效位 :MSB(Most Significant Bit):最高有效位,若MSB=1,则表示数据为负值,若MSB=0则表示数据为正. 在 ...
- 低字节序和高字节序相互转换(Little Endian/Big Endian)
这个例子展示了如何转换整形数字的字节顺序,该方法可以用来在little-endian和big-endian之间转换. 说明:Windos(x86,x64)和Linux(x86,x64)都是little ...
- 关于Big Endian 和 Little Endian
Big Endian 和 Little Endian 一.字节序 来自:http://ayazh.gjjblog.com/archives/1058846/ 谈到字节序的问题,必然牵涉到两大CPU派系 ...
- 清晰讲解LSB、MSB和大小端模式及网络字节序
时隔一个月又回到了博客园写文章,很开心O(∩_∩)O~~ 今天在做需求的涉及到一个固件版本的概念,其中固件组的人谈到了版本号从MSB到LSB排列,检索查阅后将所得整理如下. MSB.LSB? MSB( ...
- 字符编码笔记:ASCII、Unicode、UTF-8、UTF-16、UCS、BOM、Endian
转载:http://witmax.cn/character-encoding-notes.html 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问 ...
- 06socket编程
socket可以看成是用户进程与内核网络协议栈的编程接口. socket不仅可以用于本机的进程间通信,还可以用于网络上不同主机的进程间通信. IPv4套接口地址结构通常也称为“网际套接字地址结构”,它 ...
- UNIX网络编程——socket概述和字节序、地址转换函数
一.什么是socket socket可以看成是用户进程与内核网络协议栈的编程接口.socket不仅可以用于本机的进程间通信,还可以用于网络上不同主机的进程间通信. socket API是一层抽象的网络 ...
随机推荐
- 十八. Python基础(18)常用模块
十八. Python基础(18)常用模块 1 ● 常用模块及其用途 collections模块: 一些扩展的数据类型→Counter, deque, defaultdict, namedtuple, ...
- 准备下上机考试,各种排序!!以后再添加和仿真像wiki上那样!
#include <stdio.h> #include <string.h> #define N 6 typedef struct { ]; int score; }stude ...
- jdk8-lanbda方法引用和构造引用
1.方法引用概念及实例 1.对象实例方法 语法格式: 对象::实例方法名称 注意点: 实例方法必须和被实现的接口中定义的方法的参数列表和返回值一致.一般适合于一个方法就实现了的. 2.类::静态方法 ...
- let var区别
function varTest() { var x = 1; if (true) { var x = 2; // 同样的变量! console.log(x); } console.log(x); } ...
- WebClient和WebRequest获取html代码
HTML: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.as ...
- <Yarn><Node Labels>
Go through official docs For the official docs of Yarn node label, plase see here. Overview Node lab ...
- 对象存储到session中
以前在使用java开发中,通常都是在session里面存放的对象.在使用php开发中,也打算在session中存入对象,确实能把对象放进去,也能把整个对象输出,但就是取不出对象里面的属性. 通过pri ...
- SQL语句报错,无法绑定由多个部分组成的标识符解决
无法绑定由多个部分组成的标识符, 表示在查询的时候使用了别名,并且查询的多个表中存在相同的字段,如果在使用该字段时不明确该字段的来源就会报这个错误. 举例: 我们有两张表,B1,B2,他们有一个共同的 ...
- 点击图片video全屏
<!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...
- caffe 中solver.prototxt
关于cifar-10和mnist的weight_decay和momentum也是相当的重要:就是出现一次把cifar-10的两个值直接用在mnist上,发现错误很大.