Help Required - How to convert bin file to source code - Page 1
Forum section: electronics / repair

author: daisizhou
author_rank: Super Contributor
timestamp: April 17, 2021, 11:48:59 pm
content: I use a programmer to read the information in the flash memory of the motherboard,But there are incomprehensible garbled characters.How to convert BIN files (ie garbled files) into codes?

author: IanB
author_rank: Super Contributor
timestamp: April 17, 2021, 11:53:51 pm
content: Quote from: daisizhou on April 17, 2021, 11:48:59 pmYou will have to use a disassembler. The resulting output will not have any comments or symbolic names to help you interpret it, but it will be better than nothing.You can, if you are patient, read the datasheet of the microcontroller and manually interpret the codes in the raw hex as CPU instructions. But this is only for small amounts of code, and if you have no other option. Otherwise the disassembler is best.In some cases it might be possible to "decompile" the machine language back into some semblance of a language like C, but it is not easy or straightforward to do. And the result will never be the same as the original source code.

author: daisizhou
author_rank: Super Contributor
timestamp: April 18, 2021, 12:00:22 am
content: How do I choose a disassembler?Because I don’t know which language the source code is written in,I guess it may be C languageCan you recommend me a better decompiler software?My MCU is Freescale MC9S12XDP512MAG QFP144

author: dc101
author_rank: Regular Contributor
timestamp: April 18, 2021, 01:00:31 am
content: If you post the bin file, I can take a look at it. There are commercial tools like IDA Pro and Binary Ninja, as well as the opensource Ghidra that can disassemble and decompile binary files.To do proper disassembly you need to know the architecture, the endianness and the base-address of the code. ARM 32 is probably the easiest to determine by looking at the binary file which usually consists of a pattern where every forth byte is eX.For endianness, usually MIPS is big-endian but there are some cores that are little. ARM almost always little, x86 is little and PowerPC is big.

author: IanB
author_rank: Super Contributor
timestamp: April 18, 2021, 01:48:23 am
content: Quote from: daisizhou on April 18, 2021, 12:00:22 amYou are seeing garbled characters because you are trying to view the binary file as if it were a text file. To view a binary file you need to use a hex editor, which shows you each byte in the file as a hex code.In case you don't realize it, an MCU does not execute a language like C, it executes machine code. Machine code consists of a sequence of binary values that represent machine instructions. To interpret those instructions you need to look at the datasheet for the MCU. Disassemblers translate the machine code instructions into text that is easier for humans to read, but you will still need the datasheet to properly understand it.Once a C program has been compiled into binary code, the original C language has been lost. You cannot get it back again. Compilation is a one-way trip. The only thing you can do is guess what the original C code might have looked like, which is what decompilers try to do. However, decompiling will never be 100% successful.

author: daisizhou
author_rank: Super Contributor
timestamp: April 18, 2021, 11:36:09 am
content: I uploaded the hex file, which software is the best decompiler for me

author: daisizhou
author_rank: Super Contributor
timestamp: April 19, 2021, 01:29:25 pm
content: Quote from: daisizhou on April 18, 2021, 12:00:22 amI hope to learn the source code,Does anyone have a better suggestion?

author: IanB
author_rank: Super Contributor
timestamp: April 19, 2021, 02:00:23 pm
content: Quote from: daisizhou on April 19, 2021, 01:29:25 pmQuote from: IanB on April 18, 2021, 01:48:23 amIs that clear enough for you?