MPASM decompiler

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply
Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

MPASM decompiler

Post by Spanish_dude »

Hey guys, not the usual flowcode program today !

I have been searching for quite some time for a MPASM decompiler, because I had a project in mind where I needed to decompile hex files, but I couldn't find any, so I decided to make my own :D.

For the past couple of days I've been working on a decompiler for various microcontrollers.
Unfortunately I have no or little experience with any other microcontroller than microchip PIC microcontrollers and thus I based the decompiler on PIC's midrange mcu.

The program seems to work fairly well. It's called MPASM decompiler v1.0, couldn't find anything better :roll: .
Because I wanted to have a wide range of microcontrollers, I started using CSV files where I put the instruction set, common SFRs and specific SFRs for each microcontroller.
As of now, there's only the PIC midrange mcu supported and I only tried with the 12F675.
By this I mean there's only the midrange mcu instruction set and common SFRs that have been copied into a CSV file. (see readme for where to find those common SFRs).

Here's a download link for MPASM decompiler: http://www.mediafire.com/download.php?osid343jucchxbo

Everything you need to know is explained in the readme.txt file, so read it :P.

I explained how to add PIC midrange microcontrollers to the decompiler and how to add 'other'-range microcontrollers to it. As I said, only the 12F675 has been tested, so you'll need to make the CSV files for other microcontrollers (everything is explained in the readme).
For the 'other'-range controllers you'll probably have to modify the code. I don't know how the instructions are compiled to hex so I'm not sure it will be compatible with the current decompiler.
If it's the same as the midrange one then it should work fine. (By same I mean it uses f, d, b and k and uses less than 16 bits to encode the isntruction)

The decompiler is open source!
I'm not a professional C programmer so there's probably a lot that can be improved.
I left the codeblocks project file so you can open all files in codeblocks. It's also easier if you would like to compile it yourself.

I only used standard functions (stdio, string, stdlib, stdint) so I think you can compile it on linux.
I'm not sure for MAC users, but who cares, right ? :P

If you would like to give it a quick test, here's a link to a 12F675 blink led : http://www.ddg69.demon.co.uk/pic/LED1.zip
From this site : http://www.ddg69.demon.co.uk/pic/pic-led.html

After you extracted everything, copy LED1.hex from LED1.zip into the 'decompile' folder from MPASM decompiler.zip
On windows : Open a cmd prompt, go to the folder where you extracted the MPASM decompiler.zip file with the 'cd' command.
Type in : "MPASM decompiler.exe" -dev PIC -mcu 12F675 -bin LED1.hex

This will decode LED1.hex and display the decompiled MPASM code in the DOS window.
Now open LED1.asm and check the results :).
Note : LED1.asm has 3 constants. From those constants, two are decimal values but the guy who programmed this didn't put a dot in front of the decimal value so the compiler saw those as hex values. That's why you should see 0x15 and 0x31 in the decompiler, instead of the hex value for 15 and 131.
(8 bit mcu can't save 0x131 in one register hence the 0x31)

There's also a help command : -h or -help
This will give you a quick look at the commands that can be used, with explanation and example of use.

I have also added a command to write the decompiled code to a file : -wfile <filename> (without <>)

I had to 'emulate' the STATUS register so the decompiler would know in which bank to get it's registers.
It's a very simplistic function, but it only works with BCF and BSF instructions.
If you change the STATUS value through the WREG or other, then it won't apply the changes to the STATUS register and won't decode the hex properly.

Another thing to point out is the intel hex parser. It's a very basic parser that only reads data bytes. There's no extended addressing and stuff like that.
For the midrange mcus there's no need for extended addressing, because I think the memory only goes up to 0x1FFF.

I think I said everything I needed to say.
Hope you enjoy the program and the source files.

I'm open to any suggestions, criticism, ...
If you have any questions feel free to ask.

Nicolas

PS:
MPASM decompiler v1.0 beta source files and program are licensed under a Creative Commons Attribution 3.0 Unported License (CC BY).
more info : http://creativecommons.org/licenses/by/3.0/

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: MPASM decompiler

Post by medelec35 »

Hi Nicolas,
Thanks for sharing. Very clever!

There is something else for you to try.
If you download MPLAB from:
http://www.microchip.com/stellent/idcpl ... t=SW007002

Load MPLAB IDE
You can select target device (Configure menu) for any Microchip microcontroller.
Use File, Import and import a hex file for selected microcontroller
Disassembly can be viewed via View, Program Memory window.
If you right click on the menu of Program Memory window, you can deselect Line numbering and opcodes.
Mplab1.png
(246.83 KiB) Downloaded 4980 times
MPLAB also has a built in simulator.

Martin
Martin

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: MPASM decompiler

Post by Spanish_dude »

Awesome, I didn't know you could do that with MPLAB.
Hope this feature is still available in MPLAB X.

Anyways, I needed an open source decompiler as I want to do something like this with MPASM instructions:
http://buddhahacks.files.wordpress.com/ ... apro52.png

But couldn't find any so needed to do my own :P
Thanks for the tip though !

Nicolas

Post Reply