I want to convert ASM file to C program. is there any tools available for this. My intention is to convert assembly program of PIC 16F877A program to C program...
-
1I am somewhat puzzled by the tags on this question. Why tag it 8051? Are you going to recompile the C code for this other processor? Why RTOS? Surely not some attempt to port an OS between processor families? – uɐɪ May 20 '10 at 13:42
-
I was also going to ask why the hell this had an RTOS tag. Thankfully some kind soul removed it. – Dan May 20 '10 at 16:35
1 Answers
A tool that automatically converts assembly to C is a kind of decompiler. There are several decompilers for x86 assembly language. While I haven't used it, I hear that IDA, the Interactive Disassembler, has a decompile-to-C option, and supports Microchip PIC assembly language.
People who know PIC assembly can do a pretty good job manually porting code from PIC assembly to C. The first step is copying the contents of the ".asm" file to a ".c" file and wrapping it with the "asm" keyword. With SDCC syntax, the resulting file looks something like:
// the_program.c
__asm
; some assembler code
goto $0056
__endasm;
You should be able to compile that ".c" file and use "diff" to confirm that it compiles to an identical executable. A person can sometimes translate pieces of assembler into C functions that compile to identical executable (using "diff" to compare). The next step often looks something like
// the_program.c
__asm
; some assembler code
__endasm;
int main(void){
for(;;){ // forever
__asm
; more assembler code
__endasm;
};
}
__asm
; yet more assembler code
goto $0056
__endasm;
From here, there are two paths you can take:
- If you want to add some C code to this pre-existing assembly application, and run it on the same 16F877A processor, don't bother trying to translate the big blobs of assembly language -- stick your new code in normal C subroutines and call it from the main for() loop. As time goes on, in order to fix bugs and add features, you may find it convenient to translate a few lines at a time from asm to C, and perhaps someday it will be entirely converted to pure C. But there's no reason to do it all at once.
- If you want to port this application to some completely different processor, it's probably best to comment out all the assembly language in the ".c" file, and add a simple blinky-light main loop program. Use the assembly language as general guidance to write one or two C functions at a time, running and testing the program on the new processor after each addition, until you have adequate functionality.

- 5,250
- 6
- 53
- 66
-
This question is very similar to http://stackoverflow.com/questions/1376856/convert-asm-to-c-not-reverse-engineer – David Cary Jun 09 '10 at 23:17