# Will my HEX file fit in my microcontroller?

I have a hex file with a size of 42 kB. Does the size matter while loading it in a microcontroller? Can I load 42 kB hex file in a ATMEGA32 chip which has a memory of 32 kB?

• Normally a hex file is compiled for a specific processor and should be used only in that processor. At least for the uc I have used. – russ_hensel Mar 26 '11 at 20:47

You can use avr-size to check the real size of your program:

[jpc@jpc sepack] avr-size sepack.elf
text    data     bss     dec     hex filename
4396       6     277    4679    1247 sepack.elf
[jpc@jpc sepack] avr-size sepack.hex
text    data     bss     dec     hex filename
0    4402       0    4402    1132 sepack.hex


As you can see it works better with .elf files since it can also show you both how much ram you need (data+bss) and how much flash will be used (text+data). With the .hex file only shows you the second figure (labeling it data)

• So whats the size of my hex file ? I get the following details while i check my file.. text - 13858, data - 926, bss - 1043 dec - 15927, hex - 3dd3 – Sakti Apr 1 '11 at 4:56
• You will need 13858+926=14784 bytes of Flash and 1969 bytes of RAM (+ some space for stack space). I guess that means a part with at least 16kB and 4kB RAM (3 would be enough but that may be hard to find). – jpc Apr 1 '11 at 15:04
• You may have trouble fitting into the RAM of the ATmega32. You should either lower the RAM usage, carefully check stack space usage or switch to a part with 4k RAM. – jpc Apr 1 '11 at 15:07

The hex file generated is often somewhat human-readable and includes checksum data, both of which inflate the size a few times. I believe that AVRs use the Intel HEX format, while other micro toolchains use other formats; e.g. Codewarrior for Freescale HCS08 provides Motorola S-records, which is extremely similar.

If you open the .hex file with a text editor (Notepad), it should look similar to this, though much longer:

:10010000214601360121470136007EFE09D2190140
:100110002146017EB7C20001FF5F16002148011988


where the various fields are described in the example on Wikipedia. There are only 16 bytes in each of those lines that denote data that will be stored in the micro, the rest is checksum and control data that is just used by the flasher program.

• Hex files also allow for gaps in the data (the first field is the load address). So, potentially, a small non-contiguous hex file could have a large footprint in RAM/flash – Toby Jaffey Mar 26 '11 at 21:06

Each byte in a hex file takes two hex digits, plus a few characters per record. Your hex file should fit in the device's flash memory.