I googled and I see a suprising amount of flippant responses basically laughing at the asker for asking such a question.
Microchip provides some source code for free (I don't want to post it here in case that's a no-no. Basically, google AN937, click the first link and there's a link for 'source code' and its a zipped file). Its in ASM and when I look at it I start to go cross-eyed. I'd like to convert it to something resembling a c type language so that I can follow along. Because lines such as:
are probably very simple but they mean nothing to me.
There must be some automated ASM to C translator out there but all I can find are people saying its impossible. Frankly, its impossible for it to be impossible. Both languages have structure and that structure surely can be translated.
thank you.
starblue46k1111 gold badges7878 silver badges138138 bronze badges
StevenSteven24311 gold badge33 silver badges1212 bronze badges
13 Answers
You can absolutely make a c program from assembler. The problem is it may not look like what you are thinking, or maybe it will. My PIC is rusty but using another assembler, say you had
In C lets say that becomes
Possibly more readable. You lose any sense of variable names perhaps as values are jumping from memory to registers and back and the registers are being reused. If you are talking about the older pics that had what two registers an accumulator and another, well it actually might be easier because variables were in memory for the most part, you look at the address, something like
Long and drawn out but it is clear that mem[0x12] = mem[0x12] + mem[0x13];
These memory locations are likely variables that will not jump around like compiled C code for a processor with a bunch of registers. The pic might make it easier to figure out the variables and then do a search and replace to name them across the file.
What you are looking for is called a static binary translation, not necessarily a translation from one binary to another (one processor to another) but in this case a translation from pic binary to C. Ideally you would want to take the assembler given in the app note and assemble it to a binary using the microchip tools, then do the translation. You can do dynamic binary translation as well but you are even less likely to find one of those and it doesnt normally result in C but one binary to another. Ever wonder how those $15 joysticks at wal-mart with pac-man and galaga work? The rom from the arcade was converted using static binary translation, optimized and cleaned up and the C or whatever intermediate language compiled for the new target processor in the handheld box. I imagine not all of them were done this way but am pretty sure some were.
The million dollar question, can you find a static binary translator for a pic? Who knows, you probably have to write one yourself. And guess what that means, you write a disassembler, and instead of disassembling to an instruction in the native assembler syntax like add r0,r1 you have your disassembler print out r0=r0+r1; By the time you finish this disassembler though you will know the pic assembly language so well that you wont need the asm to C translator. You have a chicken and egg problem.
Oded420k7777 gold badges771771 silver badges930930 bronze badges
old_timerold_timer50.3k77 gold badges6464 silver badges126126 bronze badges
Getting the exact same source code back from a compiled program is basically impossible. But decompilers have been an area of research in computer science (e.g. the dcc decompiler, which was a PhD project).
There are various algorithms that can be used to do pattern matching on assembly code and generate equivalent C code, but it is very hard to do this in a general way that works well for all inputs.
You might want to check out Boomerang for a semi-recent open source effort at a generalized decompiler.
bobbymcrbobbymcr20.8k22 gold badges4343 silver badges6161 bronze badges
You can'tdeterministically convert assembly code to C. Interrupts, self modifying code, and other low level things have no representation other than inline assembly in C. There is only some extent to which an assembly to C process can work. Not to mention the resultant C code will probably be harder to understand than actually reading the assembly code.. unless you are using this as a basis to begin reimplementation of the assembly code in C, then it is somewhat useful. Check out the Hex-Rays plugin for IDA.
L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳10.1k33 gold badges4242 silver badges5252 bronze badges
I once worked a project where a significant part of the intellectual property was some serious algorithms coded up in x86 assembly code. To port the code to an embedded system, the developer of that code (not me) used a tool from an outfit called MicroAPL (if I recall correctly): Windows 7 loader activator.
I was very, very surprised at how well the tool did.
Fate stay night realta nua full save. On the other hand, I think it's one of those 'if you have to ask, you can't afford it' type of things (their price ranges for a one-off conversion of a project work out to around 4 lines of assembly processed for a dollar).
But, often the assembly routines you get from a vendor are packaged as functions that can be called from C - so as long as the routines do what you want (on the processor you want to use), you might just need to assemble them and more or less forget about them - they're just library functions you call from C.
Michael BurrMichael Burr288k4141 gold badges447447 silver badges682682 bronze badges
Yes, it's very possible to reverse-engineer assembler code to good quality C.
I work for a MicroAPL, a company which produces a tool called Relogix to convert assembler code to C. It was mentioned in one of the other posts.
Please take a look at the examples on our web site:
Simon MarsdenSimon Marsden
There must be some automated ASM to C translator out there but all I can find are people saying its impossible. Frankly, its impossible for it to be impossible.
No, it's not. Compilation loses information: there is less information in the final object code than in the C source code. A decompiler cannot magically create that information from nothing, and so true decompilation is impossible.
kquinnkquinn8,36322 gold badges3131 silver badges3434 bronze badges
It isn't impossible, just very hard. A skilled assembly and C programmer could probably do it, or you could look at using a Decompiler. Some of these do quite a good job of converting the asm to C, although you will probably have to rename some variables and methods.
Check out this site for a list of decompilers available for the x86 architecture.
a_m0da_m0d7,5351313 gold badges5151 silver badges7373 bronze badges
Check out this: decompiler
A decompiler is the name given to a computer program that performs the reverse operation to that of a compiler. That is, it translates a file containing information at a relatively low level of abstraction (usually designed to be computer readable rather than human readable) into a form having a higher level of abstraction (usually designed to be human readable).
Nick DandoulakisNick Dandoulakis
36.8k1414 gold badges8888 silver badges131131 bronze badges
Not easily possible.
One of the great advantages of C over ASM apart from readability was that it prevented 'clever' programing tricks.
There are numerous things you can do in assembler that have no direct C equivalent,or involve tortuous syntax in C.
The other problem is datatypes most assemblers essentialy have only two interchangeable datatypes: bytes and words. There may be some language constructs to define ints and floatsetc. but there is no attempt to check that the memory is used as defined. So its very difficult to map ASM storage to C data types.
In addition all assembler storage is essentially a 'struct'; storage is layed out in the order it is defined (unlike C where storage is ordered at the whim of the runtime). Many ASM programs depend on the exact storage layout - to acheive the same effect in C you would need to define all storage as part of a single struct.
Also there are a lot of absused instructions ( on olde worldy IBM manframes the LA, load address, instruction was regulary used to perform simple arithimatic as it was faster and didnt need an overflow register )
While it may be technically possible to translate to C the resulting C code would be less readable than the ASM code that was transalated.
James AndersonJames Anderson24.8k66 gold badges4141 silver badges7070 bronze badges
I can say with 99% guarantee, there is no ready converter for this assembly language, so you need to write one. You can simply implement it replacing ASM command with C function:
This part is easy :)Then you need to implement each function. You can declare registers as globals to make things easy. Also you can use not functions, but #defines, calling functions if needed. This will help with arguments/results processing.
Assembly To C Converter
Special case is ASM directives/labels, I think it can be converted with #defines only.
The fun starts when you'll reach some CPU-specific features. This can be simple function calls with stack operations, some specific IO/Memory operations. More fun are operations with Program Counter register, used for calculations, or using/counting ticks/latencies.
But there is another way, if this hardcore happens. It's hardcore too :)There is a technique named dynamic recompilation exists. It's used in many emulators.
You don't need recompile your ASM, but the idea is almost the same. You can use all your #defines from first step, but add support of needed functionality to them (incrementing PC/Ticks). Also you need to add some virtual environment for your code, such as Memory/IO managers, etc.
Download sniper elite 1. Good luck :)
zxcatAssembly Code To C Converter
zxcat1,34433 gold badges2020 silver badges3838 bronze badges
I think it is easier to pick up a book on PIC assembly and learn to read it. Assembler is generally quite simple to learn, as it is so low level.
jakobengblom2jakobengblom24,02722 gold badges2121 silver badges2929 bronze badges
It is difficult to convert a function from asm to C but doable by hand. Converting an entire program with a decompiler will give you code that can be impossible to understand since to much of the structure was lost during compilation. Without meaningful variable and function names the resultant C code is still very difficult to understand.
GerhardGerhard4,84444 gold badges4040 silver badges7171 bronze badges
Check out asm2c
Swift tool to transform DOS/PMODEW 386 TASM Assembly code to C code
franckfranck2,38311 gold badge1313 silver badges2626 bronze badges
Not the answer you're looking for? Browse other questions tagged cassemblyembeddedreverse-engineeringmicrochip or ask your own question.
I have this quite complicated algorithm written in C# and what I need now is to write the same thing in Assembler, I know the basics, but I would really appreciate some clues about app's structure and how to handle matrix in asm. (It's going to be .dll I assume)
Here is the algorithm:http://pastebin.com/iGKcK1rF
Thanks in advance
nrz8,84544 gold badges3030 silver badges6565 bronze badges
ZikerZiker
2 Answers
The quickest way is to use your C compiler to generate assembly code from your C code. Then you could study this generated code and improve it. If you are using
Mikhail VladimirovMikhail Vladimirovgcc
compiler, launch is with -S
option to generate assembly code. If you are using compiler from MS Visual Studio named cl
, pass /Fa
option to it.11.4k11 gold badge2626 silver badges3131 bronze badges
To add to Mikhail's answer above:
- Profile before you optimize! Look up 'gprof'. Here's a useful link:http://www.cs.utah.edu/dept/old/texinfo/as/gprof.html#SEC2
- Using the results of profiling, partition your code into compute-intensive inner loops and 'outer code'.
- Convert the inner loops into asm (using Mikhail's suggestion, or some other method) and then call them from your program.
Rahul BanerjeeRahul Banerjee