Intel HEX格式

From Wikipedia, the free encyclopedia

Intel HEX is a file format that conveys binary information in ASCII text form. It is commonly used for programming microcontrollersEPROMs, and other types of programmable logic devices. In a typical application, a compiler or assembler converts a program's source code (such as in C or assembly language) to machine code and outputs it into a HEX file. The HEX file is then imported by a programmer to "burn" the machine code into a ROM, or is transferred to the target system for loading and execution.[1]

Format[edit]

Intel HEX consists of lines of ASCII text that are separated by line feed or carriage return characters or both. Each text line contains hexadecimal characters that encode multiple binary numbers. The binary numbers may represent data, memory addresses, or other values, depending on their position in the line and the type and length of the line. Each text line is called a record.

Record structure[edit]

record (line of text) consists of six fields (parts) that appear in order from left to right:

  1. Start code, one character, an ASCII colon ':'.
  2. Byte count, two hex digits, indicating the number of bytes (hex digit pairs) in the data field. The maximum byte count is 255 (0xFF). 16 (0x10) and 32 (0x20) are commonly used byte counts.
  3. Address, four hex digits, representing the 16-bit beginning memory address offset of the data. The physical address of the data is computed by adding this offset to a previously established base address, thus allowing memory addressing beyond the 64 kilobyte limit of 16-bit addresses. The base address, which defaults to zero, can be changed by various types of records. Base addresses and address offsets are always expressed as big endian values.
  4. Record type (see record types below), two hex digits, 00 to 05, defining the meaning of the data field.
  5. Data, a sequence of n bytes of data, represented by 2n hex digits. Some records omit this field (n equals zero). The meaning and interpretation of data bytes depends on the application.
  6. Checksum, two hex digits, a computed value that can be used to verify the record has no errors.
Color legend[edit]

As a visual aid, the fields of Intel HEX records are colored throughout this article as follows:

  Start code   Byte count   Address   Record type   Data   Checksum

Checksum calculation[edit]

A record's checksum byte is the two's complement (negative) of the least significant byte (LSB) of the sum of all decoded byte values in the record preceding the checksum. It is computed by summing the decoded byte values and extracting the LSB of the sum (i.e., the data checksum), and then calculating the two's complement of the LSB (e.g., by inverting its bits and adding one).

For example, in the case of the record :0300300002337A1E, the sum of the decoded byte values is 03 + 00 + 30 + 00 + 02 + 33 + 7A = E2. The two's complement of E2 is 1E, which is the checksum byte appearing at the end of the record.

The validity of a record can be checked by computing its checksum and verifying that the computed checksum equals the checksum appearing in the record; an error is indicated if the checksums differ. Since the record's checksum byte is the negative of the data checksum, this process can be reduced to summing all decoded byte values — including the record's checksum — and verifying that the LSB of the sum is zero.

Text line terminators[edit]

Intel HEX records are separated by one or more ASCII line termination characters so that each record appears alone on a text line. This enhances legibility by visually delimiting the records and it also provides padding between records that can be used to improve machine parsing efficiency.

Programs that create HEX records typically use line termination characters that conform to the conventions of their operating systems. For example, Linux programs use a single LF (line feed, hex value 0A) character to terminate lines, whereas Windows programs use a CR (carriage return, hex value 0D) followed by a LF.

Record types[edit]

Intel HEX has six standard record types:

Hex code Record type Description Example
00DataContains data and a 16-bit starting address for the data. The byte count specifies number of data bytes in the record. The example shown to the right has 0B (decimal 11) data bytes (6164647265737320676170) located at consecutive addresses beginning at address 0010.:0B0010006164647265737320676170A7
01End Of FileMust occur exactly once per file in the last line of the file. The data field is empty (thus byte count is 00) and the address field is typically 0000.:00000001FF
02Extended Segment AddressThe data field contains a 16-bit segment base address (thus byte count is 02) compatible with 80x86 real mode addressing. The address field (typically 0000) is ignored. The segment address from the most recent 02 record is multiplied by 16 and added to each subsequent data record address to form the physical starting address for the data. This allows addressing up to one megabyte of address space.:020000021200EA
03Start Segment AddressFor 80x86 processors, 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 IPvalue.:0400000300003800C1
04Extended Linear AddressAllows for 32 bit addressing (up to 4GiB). The address field is ignored (typically 0000) and the byte count is always 02. The two encoded, big endian data bytes specify the upper 16 bits of the 32 bit absolute address for all subsequent type 00 records; these upper address bits apply until the next 04 record. If no type 04 record precedes a 00 record, the upper 16 address bits default to 0000. The absolute address for a type 00 record is formed by combining the upper 16 address bits of the most recent 04 record with the low 16 address bits of the 00 record.:02000004FFFFFC
05Start Linear AddressThe address field is 0000 (not used) and the byte count is 04. The four data bytes represent the 32-bit value loaded into the EIP register of the 80386 and higher CPU.:04000005000000CD2A

Named formats[edit]

Special names are sometimes used to denote the formats of HEX files that employ specific subsets of record types. For example:

  • I8HEX files use only record types 00 and 01 (16 bit addresses)
  • I16HEX files use only record types 00 through 03 (20 bit addresses)
  • I32HEX files use only record types 000104, and 05 (32 bit addresses)

File example[edit]

This example shows a file that has four data records followed by an end-of-file record:

:10010000214601360121470136007EFE09D2190140
:100110002146017E17C20001FF5F16002148011928
:10012000194E79234623965778239EDA3F01B2CAA7
:100130003F0156702B5E712B722B732146013421C7
:00000001FF

See also[edit]

Articles
Other

References[edit]

  1. Jump up^ Hexadecimal Object File Format Specification (PDF) (Revision A ed.). Intel. 1988-01-06. Archived (PDF) from the original on 2016-06-07. Retrieved 2016-06-07.

External links[edit]

Documentation
Software Programs
  • binex - a converter between Intel HEX and binary for Windows.
  • SRecord, a converter between Intel HEX and binary for Linux (usage), C++ source code.
Software Source Code
  • libgis, open source C library that converts Intel HEX, Motorola S-Record, Atmel Generic files.
v0.8.0: 去掉“直接Hex文件编辑”功能的128K字节限制(其他功能仍然有128KB限制) v0.7.0: 一些小的调整 V0.6.1: 调整另存对话框,去掉无效选项 V0.6.0: 加入文件拖放功能。 V0.5.5:修正建立新的配置文件时,保存失败的Bug。 V0.5.4:修正数制计算器中输入A-F时的bug,增加源数据存储类型选择。 0.5.3:修改图标,部分界面。 0.5.2: About 界面修改,添加更新网址。 0.5.1:修正块另存时新建文件保存失败的Bug。 0.5:新增文件内、文件间块移动、复制、交换、另存等功能 0.4:新增转换为二进制后比较功能 本人继MCUTool以后的另外一个单片机开发实用工具软件,用于单片机调试过程中简单的参量修改,省略修改源代码、再编译的过程,直接修改目标文件。尤其是对eeprom数据的修改、调整更为方便实用。 本软件编写的目的是用来编辑存放于单片机 Flash 或 EEPROM 中的数据,支持 Intel Hex 格式以及 Motorola S 格式的数据文件,也可以编辑二进制的内存映射文件。数据的编辑可以通过常量定义的方式,也可以通过内存映射的方式进行。 同时可以在两种16进制格式文件之间相互转换。格式转换以行数据为单位,以确保转换前后数据不会改变。 另外提供十进制、十六进制转换器,转换结果可以作为普通显示或作为内存映射,作内存映射时可以选择多字节数据的存储方式。 二进制比较功能,通过不同工具生成的Hex文件可能因为格式而无法进行文本方式比较,本工具可以首先转换为二进制数据再进行比较,免去自行转换的麻烦。 单文件内/双文件间的块操作,包括复制、移动、交换、另存等等,方便实验数据提取、复制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值