如何讀懂 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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM