2
\$\begingroup\$

I am confused regarding the (size/number of bytes) in a hex file.
Taking the case of A sample Blink Arduino Program - Blink.cpp.hex On linux terminal,

size command output:

beyond@beyond-HP-Pavilion:~/Documents$ size -f Blink.cpp.hex text data bss dec hex filename 0 1010 0 1010 3f2 Blink.cpp.hex

Arduino Ouput(after compilation):

Binary sketch size: 1010 bytes(of a 32256 byte maximum)

ls command output:

beyond@beyond-HP-Pavilion:~/Documents$ ls -l Blink.cpp.hex -rw-r--r-- 1 beyond beyond 2865 Nov 18 16:03 Blink.cpp.hex

XXD command output:

beyond@beyond-HP-Pavilion:~/Documents$ xxd Blink.cpp.hex

> 0000000: 3a31 3030 3030 3030 3030 4339 3436 3130  :100000000C94610
> 0000010: 3030 4339 3437 3330 3030 4339 3437 3330  00C9473000C94730
> 0000020: 3030 4339 3437 3330 3042 360d 0a3a 3130  00C947300B6..:10
> 0000030: 3030 3130 3030 3043 3934 3733 3030 3043  0010000C9473000C
> 0000040: 3934 3733 3030 3043 3934 3733 3030 3043  9473000C9473000C
> 0000050: 3934 3733 3030 3934 0d0a 3a31 3030 3032  94730094..:10002
> 0000060: 3030 3030 4339 3437 3330 3030 4339 3437  0000C9473000C947
> 0000070: 3330 3030 4339 3437 3330 3030 4339 3437  3000C9473000C947
> 0000080: 3330 3038 340d 0a3a 3130 3030 3330 3030  30084..:10003000
> 0000090: 3043 3934 3733 3030 3043 3934 3733 3030  0C9473000C947300
> 00000a0: 3043 3934 3733 3030 3043 3934 3733 3030  0C9473000C947300
> 00000b0: 3734 0d0a 3a31 3030 3034 3030 3030 4339  74..:100040000C9
> 00000c0: 3431 3830 3130 4339 3437 3330 3030 4339  418010C9473000C9
> 00000d0: 3437 3330 3030 4339 3437 3330 3042 450d  473000C947300BE.
> 00000e0: 0a3a 3130 3030 3530 3030 3043 3934 3733  .:100050000C9473
> 00000f0: 3030 3043 3934 3733 3030 3043 3934 3733  000C9473000C9473
> 0000100: 3030 3043 3934 3733 3030 3534 0d0a 3a31  000C94730054..:1
> 0000110: 3030 3036 3030 3030 4339 3437 3330 3030  00060000C9473000
> 0000120: 4339 3437 3330 3030 3030 3030 3030 3032  C947300000000002
> 0000130: 3430 3032 3730 3031 460d 0a3a 3130 3030  40027001F..:1000
> 0000140: 3730 3030 3241 3030 3030 3030 3030 3030  70002A0000000000
> 0000150: 3235 3030 3238 3030 3242 3030 3030 3030  250028002B000000
> 0000160: 3030 3030 4445 0d0a 3a31 3030 3038 3030  0000DE..:1000800
> 0000170: 3032 3330 3032 3630 3032 3930 3030 3430  0230026002900040
> 0000180: 3430 3430 3430 3430 3430 3430 3430 3230  4040404040404020
> 0000190: 3244 410d 0a3a 3130 3030 3930 3030 3032  2DA..:1000900002
> 00001a0: 3032 3032 3032 3033 3033 3033 3033 3033  0202020303030303
> 00001b0: 3033 3031 3032 3034 3038 3130 3230 3037  0301020408102007
> 00001c0: 0d0a 3a31 3030 3041 3030 3034 3038 3030  ..:1000A00040800
> 00001d0: 3130 3230 3430 3831 3032 3030 3130 3230  1020408102001020
> 00001e0: 3430 3831 3032 3030 3030 3031 320d 0a3a  4081020000012..:
> 00001f0: 3130 3030 4230 3030 3030 3037 3030 3032  1000B00000070002
> 0000200: 3031 3030 3030 3033 3034 3036 3030 3030  0100000304060000
> 0000210: 3030 3030 3030 3030 3239 0d0a 3a31 3030  0000000029..:100
> 0000220: 3043 3030 3030 3030 3031 3132 3431 4642  0C000000011241FB
> 0000230: 4543 4645 4644 3845 3044 4542 4643 4442  ECFEFD8E0DEBFCDB
> 0000240: 4631 3145 3038 450d 0a3a 3130 3030 4430  F11E08E..:1000D0
> 0000250: 3030 4130 4530 4231 4530 3031 4330 3144  00A0E0B1E001C01D
> 0000260: 3932 4139 3330 4231 3037 4531 4637 3045  92A930B107E1F70E
> 0000270: 3934 3934 0d0a 3a31 3030 3045 3030 3045  9494..:1000E000E
> 0000280: 3830 3130 4339 3446 3730 3130 4339 3430  8010C94F7010C940
> 0000290: 3030 3038 4445 3036 3145 3030 4539 3439  0008DE061E00E949
> 00002a0: 460d 0a3a 3130 3030 4630 3030 4243 3030  F..:1000F000BC00
> 00002b0: 3038 3935 3844 4530 3631 4530 3045 3934  08958DE061E00E94
> 00002c0: 4530 3030 3638 4545 3733 4530 4345 0d0a  E00068EE73E0CE..
> 00002d0: 3a31 3030 3130 3030 3038 3045 3039 3045  :1001000080E090E
> 00002e0: 3030 4539 3438 3630 3138 4445 3036 3045  00E9486018DE060E
> 00002f0: 3030 4539 3445 3030 3043 370d 0a3a 3130  00E94E000C7..:10
> 0000300: 3031 3130 3030 3638 4545 3733 4530 3830  01100068EE73E080
> 0000310: 4530 3930 4530 3045 3934 3836 3031 3038  E090E00E94860108
> 0000320: 3935 3833 3330 4544 0d0a 3a31 3030 3132  958330ED..:10012
> 0000330: 3030 3037 3146 3038 3433 3032 3846 3438  00071F0843028F48
> 0000340: 3133 3041 3146 3038 3233 3032 3146 3531  130A1F0823021F51
> 0000350: 3443 3043 300d 0a3a 3130 3031 3330 3030  4C0C0..:10013000
> 0000360: 3836 3330 4231 4630 3837 3330 4431 4630  8630B1F08730D1F0
> 0000370: 3834 3330 4539 4634 3034 4330 3830 3931  8430E9F404C08091
> 0000380: 3841 0d0a 3a31 3030 3134 3030 3038 3030  8A..:10014000800
> 0000390: 3038 4637 3730 3343 3038 3039 3138 3030  08F7703C08091800
> 00003a0: 3038 4637 4438 3039 3338 3030 3033 360d  08F7D8093800036.
> 00003b0: 0a3a 3130 3031 3530 3030 3038 3935 3834  .:10015000089584
> 00003c0: 4235 3846 3737 3032 4330 3834 4235 3846  B58F7702C084B58F
> 00003d0: 3744 3834 4244 3038 3935 4445 0d0a 3a31  7D84BD0895DE..:1
> 00003e0: 3030 3136 3030 3038 3039 3142 3030 3038  00160008091B0008
> 00003f0: 4637 3738 3039 3342 3030 3030 3839 3538  F778093B00008958
> 0000400: 3039 3142 3030 3041 370d 0a3a 3130 3031  091B000A7..:1001
> 0000410: 3730 3030 3846 3744 3830 3933 4230 3030  70008F7D8093B000
> 0000420: 3038 3935 3930 4530 4643 3031 4536 3536  089590E0FC01E656
> 0000430: 4646 3446 3143 0d0a 3a31 3030 3138 3030  FF4F1C..:1001800
> 0000440: 3032 3439 3146 4330 3145 4135 3746 4634  02491FC01EA57FF4
> 0000450: 4645 3439 3145 4532 3343 3146 3046 3045  FE491EE23C1F0F0E
> 0000460: 3032 370d 0a3a 3130 3031 3930 3030 4545  027..:10019000EE
> 0000470: 3046 4646 3146 4538 3539 4646 3446 3835  0FFF1FE859FF4F85
> 0000480: 3931 3934 3931 4443 3031 3636 3233 3134  919491DC01662314
> 0000490: 0d0a 3a31 3030 3141 3030 3034 3146 3439  ..:1001A00041F49
> 00004a0: 4642 3746 3839 3438 4339 3132 3039 3538  FB7F8948C9120958
> 00004b0: 3232 3338 4339 3339 4642 4634 340d 0a3a  2238C939FBF44..:
> 00004c0: 3130 3031 4230 3030 3038 3935 3946 4237  1001B00008959FB7
> 00004d0: 4638 3934 3843 3931 3832 3242 3843 3933  F8948C91822B8C93
> 00004e0: 3946 4246 3038 3935 4443 0d0a 3a31 3030  9FBF0895DC..:100
> 00004f0: 3143 3030 3030 4639 3331 4639 3344 4639  1C0000F931F93DF9
> 0000500: 3343 4639 3330 4639 3243 4442 3744 4542  3CF930F92CDB7DEB
> 0000510: 3732 3832 4646 360d 0a3a 3130 3031 4430  7282FF6..:1001D0
> 0000520: 3030 3330 4530 4639 3031 4532 3535 4646  0030E0F901E255FF
> 0000530: 3446 3834 3931 4639 3031 4536 3536 4646  4F8491F901E656FF
> 0000540: 3446 4637 0d0a 3a31 3030 3145 3030 3031  4FF7..:1001E0001
> 0000550: 3439 3132 4135 3733 4634 4646 3930 3130  4912A573F4FF9010
> 0000560: 3439 3130 3032 3344 3946 3038 3832 3333  4910023D9F088233
> 0000570: 350d 0a3a 3130 3031 4630 3030 3231 4630  5..:1001F00021F0
> 0000580: 3639 3833 3045 3934 3846 3030 3639 3831  69830E948F006981
> 0000590: 4530 3246 4630 4530 4545 3046 3042 0d0a  E02FF0E0EE0F0B..
> 00005a0: 3a31 3030 3230 3030 3046 4631 4645 4535  :10020000FF1FEE5
> 00005b0: 3846 4634 4638 3539 3139 3439 3144 4330  8FF4F85919491DC0
> 00005c0: 3139 4642 3746 3839 3434 320d 0a3a 3130  19FB7F89442..:10
> 00005d0: 3032 3130 3030 3636 3233 3231 4634 3843  021000662321F48C
> 00005e0: 3931 3130 3935 3831 3233 3032 4330 3843  911095812302C08C
> 00005f0: 3931 3831 3242 3446 0d0a 3a31 3030 3232  91812B4F..:10022
> 0000600: 3030 3038 4339 3339 4642 4630 4639 3043  0008C939FBF0F90C
> 0000610: 4639 3144 4639 3131 4639 3130 4639 3130  F91DF911F910F910
> 0000620: 3839 3546 350d 0a3a 3130 3032 3330 3030  895F5..:10023000
> 0000630: 3146 3932 3046 3932 3046 4236 3046 3932  1F920F920FB60F92
> 0000640: 3131 3234 3246 3933 3346 3933 3846 3933  11242F933F938F93
> 0000650: 3142 0d0a 3a31 3030 3234 3030 3039 4639  1B..:100240009F9
> 0000660: 3341 4639 3342 4639 3338 3039 3130 3430  3AF93BF938091040
> 0000670: 3139 3039 3130 3530 3141 3039 3137 410d  190910501A0917A.
> 0000680: 0a3a 3130 3032 3530 3030 3036 3031 4230  .:100250000601B0
> 0000690: 3931 3037 3031 3330 3931 3038 3031 3031  9107013091080101
> 00006a0: 3936 4131 3144 4231 3144 3631 0d0a 3a31  96A11DB11D61..:1
> 00006b0: 3030 3236 3030 3032 3332 4632 4435 4632  0026000232F2D5F2
> 00006c0: 4433 3732 3046 3032 4435 3730 3139 3641  D3720F02D570196A
> 00006d0: 3131 4442 3131 4439 350d 0a3a 3130 3032  11DB11D95..:1002
> 00006e0: 3730 3030 3230 3933 3038 3031 3830 3933  7000209308018093
> 00006f0: 3034 3031 3930 3933 3035 3031 4130 3933  040190930501A093
> 0000700: 3036 3031 3437 0d0a 3a31 3030 3238 3030  060147..:1002800
> 0000710: 3042 3039 3330 3730 3138 3039 3130 3030  0B09307018091000
> 0000720: 3139 3039 3130 3130 3141 3039 3130 3230  190910101A091020
> 0000730: 3142 410d 0a3a 3130 3032 3930 3030 4230  1BA..:10029000B0
> 0000740: 3931 3033 3031 3031 3936 4131 3144 4231  9103010196A11DB1
> 0000750: 3144 3830 3933 3030 3031 3930 3933 4246  1D809300019093BF
> 0000760: 0d0a 3a31 3030 3241 3030 3030 3130 3141  ..:1002A0000101A
> 0000770: 3039 3330 3230 3142 3039 3330 3330 3142  0930201B0930301B
> 0000780: 4639 3141 4639 3139 4639 3130 460d 0a3a  F91AF919F910F..:
> 0000790: 3130 3032 4230 3030 3846 3931 3346 3931  1002B0008F913F91
> 00007a0: 3246 3931 3046 3930 3046 4245 3046 3930  2F910F900FBE0F90
> 00007b0: 3146 3930 3138 3935 3237 0d0a 3a31 3030  1F90189527..:100
> 00007c0: 3243 3030 3039 4642 3746 3839 3432 3039  2C0009FB7F894209
> 00007d0: 3130 3030 3133 3039 3130 3130 3134 3039  1000130910101409
> 00007e0: 3130 3230 3130 330d 0a3a 3130 3032 4430  1020103..:1002D0
> 00007f0: 3030 3530 3931 3033 3031 3836 4235 4138  005091030186B5A8
> 0000800: 3942 3036 4330 3846 3346 3231 4630 3246  9B06C08F3F21F02F
> 0000810: 3546 3838 0d0a 3a31 3030 3245 3030 3033  5F88..:1002E0003
> 0000820: 4634 4634 4634 4635 4634 4639 4642 4635  F4F4F4F5F4F9FBF5
> 0000830: 3432 4634 3332 4633 3232 4632 3232 3733  42F432F322F22273
> 0000840: 370d 0a3a 3130 3032 4630 3030 3238 3046  7..:1002F000280F
> 0000850: 3331 3144 3431 3144 3531 3144 3832 4530  311D411D511D82E0
> 0000860: 3232 3046 3333 3146 3434 3146 3635 0d0a  220F331F441F65..
> 0000870: 3a31 3030 3330 3030 3035 3531 4638 4139  :10030000551F8A9
> 0000880: 3544 3146 3742 3930 3143 4130 3130 3839  5D1F7B901CA01089
> 0000890: 3545 4639 3246 4639 3235 450d 0a3a 3130  5EF92FF925E..:10
> 00008a0: 3033 3130 3030 3046 3933 3146 3933 4346  0310000F931F93CF
> 00008b0: 3933 4446 3933 3742 3031 3843 3031 3045  93DF937B018C010E
> 00008c0: 3934 3630 3031 4139 0d0a 3a31 3030 3332  946001A9..:10032
> 00008d0: 3030 3045 4230 3130 4643 3030 4539 3436  000EB010FC00E946
> 00008e0: 3030 3136 4331 4237 4430 4238 3345 3036  0016C1B7D0B83E06
> 00008f0: 3833 4546 370d 0a3a 3130 3033 3330 3030  83EF7..:10033000
> 0000900: 3738 3037 3338 4630 3038 3934 4531 3038  780738F00894E108
> 0000910: 4631 3038 3031 3039 3131 3039 4338 3531  F10801091109C851
> 0000920: 3542 0d0a 3a31 3030 3334 3030 3044 4334  5B..:10034000DC4
> 0000930: 4645 3131 3446 3130 3430 3130 3531 3130  FE114F1040105110
> 0000940: 3536 3146 3744 4639 3143 4639 3135 340d  561F7DF91CF9154.
> 0000950: 0a3a 3130 3033 3530 3030 3146 3931 3046  .:100350001F910F
> 0000960: 3931 4646 3930 4546 3930 3038 3935 3738  91FF90EF90089578
> 0000970: 3934 3834 4235 3832 3630 3742 0d0a 3a31  9484B582607B..:1
> 0000980: 3030 3336 3030 3038 3442 4438 3442 3538  003600084BD84B58
> 0000990: 3136 3038 3442 4438 3542 3538 3236 3038  16084BD85B582608
> 00009a0: 3542 4438 3542 3535 390d 0a3a 3130 3033  5BD85B559..:1003
> 00009b0: 3730 3030 3831 3630 3835 4244 4545 4536  7000816085BDEEE6
> 00009c0: 4630 4530 3830 3831 3831 3630 3830 3833  F0E0808181608083
> 00009d0: 4531 4538 3038 0d0a 3a31 3030 3338 3030  E1E808..:1003800
> 00009e0: 3046 3045 3031 3038 3238 3038 3138 3236  0F0E010828081826
> 00009f0: 3038 3038 3338 3038 3138 3136 3038 3038  0808380818160808
> 0000a00: 3334 300d 0a3a 3130 3033 3930 3030 4530  340..:10039000E0
> 0000a10: 4538 4630 4530 3830 3831 3831 3630 3830  E8F0E08081816080
> 0000a20: 3833 4531 4542 4630 4530 3830 3831 3433  83E1EBF0E0808143
> 0000a30: 0d0a 3a31 3030 3341 3030 3038 3436 3038  ..:1003A00084608
> 0000a40: 3038 3345 3045 4246 3045 3038 3038 3138  083E0EBF0E080818
> 0000a50: 3136 3038 3038 3345 4145 3731 350d 0a3a  1608083EAE715..:
> 0000a60: 3130 3033 4230 3030 4630 4530 3830 3831  1003B000F0E08081
> 0000a70: 3834 3630 3830 3833 3830 3831 3832 3630  8460808380818260
> 0000a80: 3830 3833 3830 3831 3945 0d0a 3a31 3030  808380819E..:100
> 0000a90: 3343 3030 3038 3136 3038 3038 3338 3038  3C00081608083808
> 0000aa0: 3138 3036 3838 3038 3331 3039 3243 3130  1806880831092C10
> 0000ab0: 3030 3839 3535 440d 0a3a 3130 3033 4430  008955D..:1003D0
> 0000ac0: 3030 4346 3933 4446 3933 3045 3934 4144  00CF93DF930E94AD
> 0000ad0: 3031 3045 3934 3735 3030 4330 4530 4430  010E947500C0E0D0
> 0000ae0: 4530 3932 0d0a 3a31 3030 3345 3030 3030  E092..:1003E0000
> 0000af0: 4539 3437 4130 3032 3039 3745 3146 3330  E947A002097E1F30
> 0000b00: 4539 3430 3030 3046 3943 4646 3839 3437  E940000F9CFF8947
> 0000b10: 300d 0a3a 3032 3033 4630 3030 4646 4346  0..:0203F000FFCF
> 0000b20: 3344 0d0a 3a30 3030 3030 3030 3146 460d  3D..:00000001FF.
> 0000**b30**: 0a                                       .

beyond@beyond-HP-Pavilion:~/Documents$

=> Total No. of bytes shown in Hexdump is b31 => in Decimal b31 is 2865

What is really meant by sizes here? size of different segments, size of file on disk and size of the hex file that will take up the space in AVR flash memory.

\$\endgroup\$
1
\$\begingroup\$

Have you tried looking at the content of a text file with just cat or more? It's an ASCII format that represents the binary data. The file is consequently bigger than the binary data it contains.

The clue is in the name: it's a hexadecimal (text) representation of the bytes.

The data it contains is precisely what will be written to the flash of the microcontroller.

When you compile (for the Arduino) the source is first compiled into ELF objects. Those are then linked into an ELF output file. ELF contains lots and lots of extra information for debugging and such.

The ELF is the distilled down into exactly what will be written into the flash as pure binary. That binary is then "wrapped" in a HEX format file.

More detail on it can be found here: http://en.wikipedia.org/wiki/Intel_HEX

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ yes, I have indeed used cat and more. In case of C executable, I do understand that the file size is bigger than the binary data it contains as it has a lot of sections and other related data written in it. but on this Hex file which is just about to be written on the AVR MCU, does this file also contains the "extra" weight other than binary stuff. I am under the assumption that the hex file is nothing other than the "just" needed machine code to run on the architecture. ex- "add r17,r18" as 0f12 as the machine equivalent. the assembly code to machine translated code. hence I asked for it \$\endgroup\$ – Saurabh Shandilya Nov 19 '14 at 12:52
  • \$\begingroup\$ @SaurabhShandilya a "byte" of binary data in "human readable" ASCII hex format requires two physical bytes of memory to store (i.e. "3C" is two bytes of information "3" and "C" , but can actually be stored in one byte in binary 0x3C = 0b00111100) and the Intel HEX format also has header and trailer information on every line which also adds some bloat to the file size. \$\endgroup\$ – vicatcu Nov 19 '14 at 14:50
  • \$\begingroup\$ okayy..so how about this, What actually is the difference between this "Blink.cpp.hex" file's hexdump(as we see above) and the one I will read after doing some --ABC-process-- from the flash of AVR microcontroller which is running this file once I upload it. {By ABC process, I mean externally reading the flash byte/page wise.} \$\endgroup\$ – Saurabh Shandilya Nov 19 '14 at 20:27
0
\$\begingroup\$

Intel HEX is a file format that conveys binary information in ASCII text form.

So, it's basically a container of your binary program but not the program itself. And the file size is not necessary equal to the size your program will occupy in flash memory.

  1. The "size" command on Unix like system, will show each 'secton''s size, according to your output, your .hex file will occuppy 1010 bytes on MCU's flash memory.
  2. Your Arduino compiler output shows the binary size.
  3. "ls" command show the file size.
  4. "XXD" command shows the file size.

Refs:

  1. http://en.wikipedia.org/wiki/Intel_HEX
  2. http://hex2bin.sourceforge.net/
  3. http://www.keil.com/support/docs/1584/
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ okay, I do get this thing, that Hex file is the container for my binary program and not the program itself. so rather than the "file size", the "size" command and the "arduino compiler" output shows me the real binary code (1), represented as "data" under the "size" command output. (1)..how do they actually get this "real binary size out of whole hex file. I get this thing for C executable where there are different sections for an elf file. \$\endgroup\$ – Saurabh Shandilya Nov 19 '14 at 12:57
  • \$\begingroup\$ Please see here: sourceforge.net/p/sdcc/mailman/message/24074307 \$\endgroup\$ – diverger Nov 19 '14 at 13:58
  • \$\begingroup\$ The compiler and linker will produce a binary program file. A bin2hex converter program will read that binary file and convert it to ASCII hex. Each line of the hex file starts with an address, then two characters for each byte in the binary file, so the hex file will be somewhat more than twice the size of the binary file. \$\endgroup\$ – Peter Bennett Nov 19 '14 at 16:54
  • \$\begingroup\$ yeah, exactly @PeterBennett , that exact protocol or the structural way I am asking of. So, do you mean that the "burner" or the "bootloader" actually strips off this unnecessary information and places the bytes at just where these are required in AVR MCU's flash OR the whole hex file is put as it is with all this extra stuff along? \$\endgroup\$ – Saurabh Shandilya Nov 19 '14 at 20:31
  • \$\begingroup\$ The programmer or bootloader will read the hex file, convert the hex data back to binary, and store the binary data in the specified memory addresses. The hex file is just a convenient way to transfer the binary data between programs or from computer to stand-alone programmer. \$\endgroup\$ – Peter Bennett Nov 19 '14 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.