yxr注:由颜色标注,深动形象

http://www.cnblogs.com/imapla/archive/2013/03/16/2926133.html

另外也可以参考,中文更具体,比如04-扩展线性地址记录的举例

http://heyunhuan513.blog.163.com/blog/static/1602042200910805711589/

如何读懂 Intel HEX 文件

 

什么是 Intel HEX 文件格式
Intel HEX 文件是遵循 Intel HEX 文件格式的 ASCII 文本文件。在 Intel HEX 文件的每一行都包含了一个 HEX 记录。这些记录是由一些代表机器语言代码和常量的16进制数据组成的。Intel HEX 文件常用来传输要存储在 ROM 或者 EPROM 中的程序和数据。大部分的 EPROM 编程器能使用 Intel HEX 文件。

Intel HEX由任意数量的十六进制记录组成。每个记录包含5个域,它们按以下格式排列:

Start Code  每个 Intel HEX 记录都由冒号开头
Byte count 是数据长度域,它代表记录当中数据字节的数量
Address 是地址域,它代表记录当中数据的起始地址
Record type 是代表HEX记录类型的域,它可能是以下数据当中的一个:
  00-数据记录
  01-文件结束记录
  02-扩展段地址记录
  03-开始段地址记录
  04-扩展线性地址记录
  05-开始线性地址记录
Data 是数据域,一个记录可以有许多数据字节.记录当中数据字节的数量必须和数据长度域中指定的数字相符
Checksum 是校验和域,它表示这个记录的校验和.校验和的计算是通过将记录当中所有十六进制编码数字对的值相加,以256为模进行以下补足。

详见:http://en.wikipedia.org/wiki/Intel_HEX

The format is a text file, with each line containing hexadecimal values encoding a sequence of data and their starting offset or absolute address.

Each line of Intel HEX file consists of six parts:

    1. Start code, one character, an ASCII colon ':'.
    2. Byte count, two hex digits, a number of bytes (hex digit pairs) in the data field. 16 (0x10) or 32 (0x20) bytes of data are the usual compromise values between line length and address overhead.
    3. Address, four hex digits, a 16-bit address of the beginning of the memory position for the data. Limited to 64 kilobytes, the limit is worked around by specifying higher bits via additional record types. This address is big endian.
    4. Record type, two hex digits, 00 to 05, defining the type of the data field.
    5. Data, a sequence of n bytes of the data themselves, represented by 2n hex digits.
    6. Checksum, two hex digits - the least significant byte of the two's complement of the sum of the values of all fields except fields 1 and 6 (Start code ":" byte and two hex digits of the Checksum). It is calculated by adding together the hex-encoded bytes (hex digit pairs), then leaving only the least significant byte of the result, and making a 2's complement (either by subtracting the byte from 0x100, or inverting it by XOR-ing with 0xFF and adding 0x01). If you are not working with 8-bit variables, you must suppress the overflow by AND-ing the result with 0xFF. The overflow may occur since both 0x100-0 and (0x00 XOR 0xFF)+1 equal 0x100. If the checksum is correctly calculated, adding all the bytes (the Byte count, both bytes in Address, the Record type, each Data byte and the Checksum) together will always result in a value wherein the least significant byte is zero (0x00).
      For example, on :0300300002337A1E
      03 + 00 + 30 + 00 + 02 + 33 + 7A = E2, 2's complement is 1E

There are six record types:

  • 00data record, contains data and 16-bit address. The format described above.
  • 01End Of File record. Must occur exactly once per file in the last line of the file. The byte count is 00 and the data field is empty. Usually the address field is also 0000, in which case the complete line is ':00000001FF'. Originally the End Of File record could contain a start address for the program being loaded, e.g. :00AB2F0125 would cause a jump to address AB2F. This was convenient when programs were loaded from punched paper tape.
  • 02Extended Segment Address Record, segment-base address (two hex digit pairs in big endian order). Used when 16 bits are not enough, identical to 80x86 real mode addressing. The address specified by the data field of the most recent 02 record is multiplied by 16 (shifted 4 bits left) and added to the subsequent data record addresses. This allows addressing of up to a megabyte of address space. The address field of this record has to be 0000, the byte count is 02 (the segment is 16-bit). The least significant hex digit of the segment address is always 0.
  • 03Start Segment Address Record. For 80x86 processors, it specifies the initial content of the CS:IP registers. The address field is 0000, the byte count is 04, the first two bytes are the CS value, the latter two are the IP value.
  • 04Extended Linear Address Record, allowing for fully 32 bit addressing (up to 4GiB). The address field is 0000, the byte count is 02. The two data bytes (two hex digit pairs in big endian order) represent the upper 16 bits of the 32 bit address for all subsequent 00 type records until the next 04 type record comes. If there is not a 04 type record, the upper 16 bits default to 0000. To get the absolute address for subsequent 00 type records, the address specified by the data field of the most recent 04 record is added to the 00 record addresses.
  • 05Start Linear Address Record. The address field is 0000, the byte count is 04. The 4 data bytes represent the 32-bit value loaded into the EIP register of the 80386 and higher CPU.

【转】Intel Hex文件格式说明  

http://heyunhuan513.blog.163.com/blog/static/1602042200910805711589/

问题:

什么是Intel HEX格式?

回答:

Intel HEX文件是记录文本行的ASCII文本文件,在Intel HEX文件中,每一行是一个HEX记录
由十六进制数组成的机器码或者数据常量,Intel HEX文件经常被用于将程序或数据传输
存储到ROM.EPROM,大多数编程器和模拟器使用Intel HEX文件.

记录格式

一个Intel HEX文件可以包含任意多的十六进制记录,每条记录有五个域,下面是一个记录的格式.

:llaaaatt[dd...]cc

每一组字母是独立的一域,每一个字母是一个十六进制数字,每一域至少由两个十六进制数字组成,下面是字节的描述.

:冒号     是每一条Intel HEX记录的开始

ll 是这条记录的长度域,他表示数据(dd)的字节数目.

aaaa 是地址域,他表示数据的起始地址
<如果是数据记录,这表示将要烧录的这条记录中的数据在EPROM中的偏移地址,
对于不支持扩展段地址和扩展线性地址的,如89C51,这就是此条记录的起始地址>

tt 这个域表示这条HEX记录的类型,他有可能是下面这几种类型
00 ----数据记录      
01 ----文件结束记录
02 ----扩展段地址记录
04 ----扩展线性地址记录

dd   是数据域,表示一个字节的数据,一个记录可能有多个数据字节,字节数目可以
查看ll域的说明

cc   是效验和域,表示记录的效验和,计算方法是将本条记录冒号开始的所有字母对

<不包括本效验字和冒号> 所表示的十六进制数字
<一对字母表示一个十六进制数,这样的一个十六进制数为一个字节>

都加起来然后模除256得到的余数最后求出余数的补码即是本效验字节cc.
<例如:
:0300000002005E9D
cc=0x01+NOT((0x03+0x00+0x00+0x00+0x02+0x00+0x5E)%0x100)=0x01+0x9C=0x9D

C语言描述:
UCHAR cc;
cc=(UCHAR)~(0x03+0x00+0x00+0x00+0x02+0x00+0x5E);
cc++;
>
数据记录

Intel HEX文件由若干个数据记录组成,一个数据记录以一个回车和一个换行结束
                                          <回车为0x0d换行为0x0a>

比如下面的一条数据记录
:10246200464C5549442050524F46494C4500464C33

10   是此行记录数据的字节数目
2462   是数据在内存<将要烧写的eprom地址>中的起始地址
00     是记录类型00(是一个数据记录)
464C 到 464C 是数据
33   是此行记录的效验和

扩展线性地址记录(HEX386)
扩展线性地址记录也可称为 32位地址记录   和   HEX386记录,这个纪录包含高16(16-31位)位数据地址,这种扩展的线性记录总是有两个字节数据,像下面这样:

:02000004FFFFFC
02     是记录的数据字节数目
0000   是地址域这在扩展地址记录中总是0000
04     是记录类型04(扩展地址记录)
FFFF   是高16位地址
FC     是记录效验和,计算方法如下:
        01h + NOT(02h + 00h + 00h + 04h + FFh + FFh)

当一个扩展线性地址记录被读到后,扩展线性地址记录的数据区域将被保存
并应用到后面从Intel HEX文件中读出的记录,这个扩展线性记录一直有效,
直到读到下一个扩展线性记录.


绝对内存地址 = 数据记录中的地址 + 移位后的扩展线性地址


下面举例说明这个过程

从数据记录的地址域得到地址           2462
从扩展线性地址记录的地址域得到地址   FFFF
绝对内存地址                           FFFF2462

扩展段地址记录 (HEX86)

扩展段地址记录也被称为 HEX86记录,   包含 4-19位的数据地址段,
这个扩展段地址记录总是有两字节数据,如下:

:020000021200EA
02 是     记录中的数据字节数目
0000           是地址域,在扩展段地址记录中,这个域总是0000
02           是记录类型02(扩展段地址的标示)
1200     是该段的地址
EA     是效验和
计算如下:
01h + NOT(02h + 00h + 00h + 02h + 12h + 00h).

当扩展段地址记录被读后,扩展段地址将被存储并应用到以后从Intel HEX文件读出的记录,这个段地址一直有效直到读到下一个扩展段地址记录

绝对内存地址 = 数据记录中的地址 + 移位后的扩展段地址


数据记录中的地址域       移位后扩展段地址记录中的地址域

下面举例说明这个过程

从数据记录的地址域得到地址                   2 4 6 2
从扩展段地址记录的地址域得到地址           1 2 0 0
绝对内存地址                         0 0 0 1 4 4 6 2

文件结束记录(EOF)
一个Intel HEX文件必须有一个文件结束记录,这个记录的类型域必须是01,
一个EOF记录总是这样:
:00000001FF
00是记录中数据字节的数目
0000这个地址对于EOF记录来说无任何意义
01记录类型是01(文件结束记录标示)
FF是效验和计算如下
01h + NOT(00h + 00h + 00h + 01h).
========================

总结


形如
:BBAAAATTHHHH...HHHHCC

BB: Byte
AAAA:数据记录的开始地址,高位在前,地位在后
      因为这个格式只支持8bits,地址被倍乘
      所以,为了得到实际的PIC的地址,需要将 地址除以2
TT: Type
        00 数据记录
        01 记录结束
        04 扩展地址记录(表示32位地址的前缀,当然这种只能在 INHX32)
HHHH:一个字(Word)的数据记录,高Byte在前,低Byte在后
      TT之后,总共有 BB/2 个字 的数据
CC: 一个Byte的CheckSum


因为PIC16F873A只有4K的程序空间

所以,不会有 TT=04的 Linear Address Record

posted on 2016-09-27 09:27  e_shannon  阅读(727)  评论(0)    收藏  举报