Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hex Decompilers for PIC

I've faced to a problem with a PIC Micro controller.

I have a micro-controller programmed by me long time ago and I lost the relevant source code and the schematic diagrams. Now I need to invert the value of a port. I can do this using some NOT gates but it is a big hassle to do so. or alternatively I will need to write the whole program back.

I don't expect to see the code back in PIC C or MikroC. Having an understandable assembly code would be sufficient.

So do anyone has any experience on a good HEX decompiler that I can use for this purpose? Any comments based on your experience? :)

EDIT : Device PIC 16F84A

like image 822
Chathuranga Chandrasekara Avatar asked Jun 03 '10 10:06

Chathuranga Chandrasekara


4 Answers

Decompilation is unlikely to be a practical solution, and it is even less likely that a tool for your specific compiler and instruction set combination even exists.

Disassembly however is straightforward, though whether you will be able to make sense of the resulting code is a different matter since no comments or symbols are preserved in the HEX file; if you have the original object code it may render the disassembly more readable. There are many PIC disassemblers available, just Google it; I can't direct you at any specific one because there are a number of PIC families with different instruction sets, and you did not specify.

A simple approach to disassembly would be to simply load your HEX file into MPLAB and select View->Disassembly Listing, then right-click the windows and select "Output to File". This output may need some massaging for it to be suitable for input to an assembler.

like image 56
Clifford Avatar answered Nov 03 '22 12:11

Clifford


I know this is an old post, but I have recently encountered a similar problem and didn't find a very complete answer online. I lost my MPLAB X IDE project due to hard drive failure, luckily I had already programmed a device with a working version of the code.

Recover the .hex
Follow the steps below to recover the .hex information from a programmed device:

Use MPLAB X IDE and your PIC programmer (I used PICkit3) to read the .hex file from the programmed device:

  • Start a new project for your device.
  • In "Project Properties" select your programmer.
  • Right click on the project folder and select "Set as Main Project".
  • Click on the arrow next to the "Read Device Memory Main Project" and select "Read Device Memory to File". Reading device memory to .hex file

Disassemble the .hex
You can view the disassembly in MPLAB X IDE, but you cannot edit or save it (or at least I couldn't figure out how to) and it is very cryptic. I found the easiest, no strings attached, disassembler to be the one packaged with gputils, it is called gpdasm. To download and install, visit the gputils page here:

https://gputils.sourceforge.io/

Now open a command prompt and navigate to the folder where your .hex file is located. Generate an assembly source file from the .hex with the following command:

gpdasm -p p16f84a -csno hexfile.hex > asmfile.dis

With the -c -s -n and -o options, this generates quite a good listing which is very near to being able to be assembled as is. Obviously the variable names and labels cannot be recovered, but at least subroutines are identified which makes things a lot easier. Hope this helps someone in the future.

like image 45
bjoubert89 Avatar answered Nov 03 '22 10:11

bjoubert89


There is a list of PIC disassemblers at the official PICList technical reference.

Many people never use a stand-alone disassembler, but prefer to use the disassembler inside their favorite PIC simulator.

http://piclist.com/techref/microchip/dissassemblers.htm

http://piclist.com/techref/microchip/simulators.htm

like image 35
2 revs Avatar answered Nov 03 '22 11:11

2 revs


You should be able to load the hex file into the MPLAB IDE and view the assembly code from there as well as run it and step through the code in the simulater if I am not mistaken.

You can also use it to read the code from a device if it is not code protected.

like image 26
Cobusve Avatar answered Nov 03 '22 12:11

Cobusve