MPASM decompiler - Page 1
Forum section: electronics / projects

author: pyroesp
author_rank: Regular Contributor
timestamp: July 31, 2012, 08:51:16 pm
content: Don't know if this is the right place to make this topic, sorry if it isn't :/.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.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 calledMPASM decompiler v1.0, couldn't find anything better.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?osid343jucchxboEverything you need to know is explained in the readme.txt file, so read it.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 isopen 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 ?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.zipFrom this site :http://www.ddg69.demon.co.uk/pic/pic-led.htmlAfter you extracted everything, copy LED1.hex from LED1.zip into the 'decompile' folder from MPASM decompiler.zipOn 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.hexThis 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 -helpThis 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.pyroespPS: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/

author: daedalus
author_rank: Regular Contributor
timestamp: August 01, 2012, 10:43:30 am
content: I don't do RE work any more, but if you want something commercial to benchmark against, might be worth checking out IDA pro, IIRC they had some IDC scripts to parse microchip pic hex files with, and its pretty much the gold standard for reverse engineering tools.

author: pyroesp
author_rank: Regular Contributor
timestamp: August 01, 2012, 02:15:10 pm
content: @daedalus: I just wanted an open source decompiler, that's why I did it. I know IDA pro looks awesome, but it's so overpriced.Also, I'm more a C guy than using scriptsMy hex parser does work good enough, no problems so far with it.