Read the HEX file from a PIC and convert to assembly language?

Thread Starter

Longymod

Joined Jan 29, 2018
13
Am looking for a little bit of help, I am very new to microcontrollers so my terminology might be wrong.

I need to read the code from an already programmed microcontroller (PIC16F873) and make some variations. I am using a PIC KIT 2 to export the HEX file but I then would like this in assembly language.

I have MPLAB IDE v8.60 installed on my PC. What is the easiest way to do this?

Thanks in advance
 

AlbertHall

Joined Jun 4, 2014
12,345
There are dissassemblers available, including for free, which will do that job but the result isn't easy to work with.
There will be no comments, no meaningful labels, no useful variable names, etc.
See the attached disassembled program.
 

Attachments

Thread Starter

Longymod

Joined Jan 29, 2018
13
Cheers AlbertHall, I will try to hunt down the original code from the archives then as it will probably save me time in the long run

Appreciate the reply , thanks mate
 

jpanhalt

Joined Jan 18, 2008
11,087
In my very limited experience (maybe a half-dozen times), I have found working from a disassembly listing challenging. Sure, the listing shows the instructions, but it will lack important structural elements like labels and comments. If I knew something about the code and what I was looking for (e.g., changing a constant, resetting a date, changing a specific register setting, or trying to identify which version of code was on a chip), then I had success. Otherwise, it is a forest.

Here's an example of disassembled code done with MPLab 8.92:

Code:
Line  Address   Opcode                     Disassembly                
    28   01B      1D0B    BTFSS 0xb, 0x2                              
    29   01C      282B    GOTO 0x2b                                    
    30   01D      0AAD    INCF 0x2d, F                                
    31   01E      082D    MOVF 0x2d, W                                
    32   01F      3A1E    XORLW 0x1e                                  
    33   020      1D03    BTFSS 0x3, 0x2                              
    34   021      2826    GOTO 0x26                                    
    35   022      01AD    CLRF 0x2d                                    
    36   023      08AE    MOVF 0x2e, F                                
    37   024      1D03    BTFSS 0x3, 0x2                              
    38   025      1205    BCF 0x5, 0x4                                
    39   026      082D    MOVF 0x2d, W                                
    40   027      062E    XORWF 0x2e, W                                
    41   028      1903    BTFSC 0x3, 0x2
As to your specific question, you can read a chip and disassemble it just using MPLab 8.92 ( and maybe later versions of MPLab too) if the code is not read protected .
 
Last edited:

Papabravo

Joined Feb 24, 2006
21,159
Once upon a time I did this for DDT, a debugger for the 8080 processor running on a Z80 system. The program was about 5K bytes when I started and about 8K bytes when I finished, but it now understood and displayed z80 opcodes. Took me about 4 months, but it was a labor of love and necessity.
 

MrChips

Joined Oct 2, 2009
30,708
Also, if you are new to microcontrollers staring at dissassembled output would be like staring at hieroglyphics.
You need to have ASM programming under your belt before you attempt to go this route.
 

Papabravo

Joined Feb 24, 2006
21,159
To write a disassembler? This one took me a few hours, IIRC.
No, not a dissassembler. You can be pretty thick and very condescending sometimes. DDT was the 8080 based command line debugger that came with CP/M. It was based on a similarly named debugger for the PDP series of machines from DEC (Digital Equipment Corporation). I disassembled it, and use the source to rewrite it for the Z80. Primarily to understand and display all the Z-80 instructions that were not part of the 8080 architecture. That is why it got bigger. I also had a day job at the time which is why it took 4 months of evenings and weekends.
 

LesJones

Joined Jan 8, 2017
4,174
I have used MPLAB V8.0 to disassemble a PIC16F876. These are the notes I made on how to do it.

MPLAB disassembly

1 Create a new project

2 Import hex file to be disassembled.

3 Select program memory view.

4 Right click inside this display and select "Output to file"

I started by looking for insrtuctions inputting and outputting to ports. I did have the advantage of having the schematic of the unit and knowing what it did. (It was a Schumatech DRO350 digital readout.) It did take me a few months to disassemble it and modify the code to run on a PIC18F2525. The reason for doing this was all the available memory was used on the PIC16F876 so the only way to get more space was to move to a PIC18 chip.

Les.
 
Top