views:

257

answers:

1

Hi,

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...

+1  A: 

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.
David Cary
This question is very similar to http://stackoverflow.com/questions/1376856/convert-asm-to-c-not-reverse-engineer
David Cary