| |
Mixer
Registered: Apr 2008 Posts: 452 |
ASM to C
Are there tools to convert a) disassembler output to some sort of C? b) macro assembler listings to C?
Such a thing can be done by substituting opcodes with their C-statement equals. Has anyone written more sophisticated tools for this? |
|
| |
JackAsser
Registered: Jun 2002 Posts: 2014 |
Quote: Are there tools to convert a) disassembler output to some sort of C? b) macro assembler listings to C?
Such a thing can be done by substituting opcodes with their C-statement equals. Has anyone written more sophisticated tools for this?
It's called a decompiler. IDA Pro disassembler contains a decompiler. However, compilation is a destructive process. Syntax is lost.
Me experience is that decompiling manually using your human mind is much more effective. |
| |
Mixer
Registered: Apr 2008 Posts: 452 |
My human mind is lazy. Any other 6502 decompilers in addition to IDA? |
| |
iAN CooG
Registered: May 2002 Posts: 3193 |
by googling i found this, commercial
http://microapl.com/asm2c/index.html
it does 6502 to C
http://microapl.com/asm2c/sample6502.html |
| |
chatGPZ
Registered: Dec 2001 Posts: 11386 |
decompiling is a quite complex tasks - and most decompilers dont produce useful output. hexrays (IDA) is probably the best BY FAR and even its output is often quite a mess :)
that said, hexrays can do x86 and ARM - not 6502 :)
curious though what you'd want to use it for (that has 6502) - its not like such decompilers would magically produce C code from arbitrary assembler programs, it can only work *somewhat* when the assembler program is a compiled c program. |
| |
Mixer
Registered: Apr 2008 Posts: 452 |
Groepaz, this is scene. Magical and impossible are our daily business :) Time to write a better tool. IAN's find is interesting and does seem to do pretty good job. |
| |
chatGPZ
Registered: Dec 2001 Posts: 11386 |
Quote:Time to write a better tool.
some things just cant be done automatically - no matter how good the tool is.
some years ago i had the idiotic idea to make a basic v2 to C compiler... guess what, it cant be done either. you cant even convert the basic shit from the test/demo disk automatically :) with asm it will be even more impossible =P |
| |
Mixer
Registered: Apr 2008 Posts: 452 |
You are probably correct, but I am not convinced yet. It does not have to be perfect. I am interested in the call structure of some complicated asm code, would like to see some asm math as c math. |
| |
chatGPZ
Registered: Dec 2001 Posts: 11386 |
for the call structure - use IDA. it can display assembly as flow-chart. THAT works pretty decent at least :) (you will need a "PRO" version, the normal one doesnt have 6502) |
| |
oziphantom
Registered: Oct 2014 Posts: 490 |
I build this, which kind of does a thing. Needs more work, only tested on a couple of vsfs etc. I built it after I did most of the hard work on HM, but it helped. I will be visiting it before starting the next one.
https://github.com/oziphantom/CodeTree
It takes guesses at code structure and will tag, if/else, if chains etc.
I want it to be able to modify the regenerator config and add code-data blocks as it finds them, to remove the tediousness of it ;) I also want to be able to get a point where I can see what memory locations are read/written by functions. PRs welcome ;)
Note I don't really know Python so the code is not "pythony" |
| |
oziphantom
Registered: Oct 2014 Posts: 490 |
Quote: for the call structure - use IDA. it can display assembly as flow-chart. THAT works pretty decent at least :) (you will need a "PRO" version, the normal one doesnt have 6502)
yeah but Maths is the neigh on impossible part
lda thing
clc
adc thing
sta thing
is trivial and a simple script will give you thing += thing its the
lda table,x
clc
adc otherTable,y
sta thing
you want converted and that is really hard to do, as you need to look at the tables and work out what the magic values are.
thing = x * 0.58 + y * 2.3 good luck.. |
| |
oziphantom
Registered: Oct 2014 Posts: 490 |
Quote: Quote:Time to write a better tool.
some things just cant be done automatically - no matter how good the tool is.
some years ago i had the idiotic idea to make a basic v2 to C compiler... guess what, it cant be done either. you cant even convert the basic shit from the test/demo disk automatically :) with asm it will be even more impossible =P
What was the major blocker on basic to C? I would think it mostly has a 1:1 relationship. READ would be the tricky part. Making custom functions would probably need to be beyond scope, but what else? |
| |
Mixer
Registered: Apr 2008 Posts: 452 |
@Ozipanthom, true that the decompiler itself does not know that the lookup is a multiplication, and does not know what the multiplier is.
But this is already pretty good for my uses.
thing=mem[table[x]]+mem[otherTable[y]] |
| |
chatGPZ
Registered: Dec 2001 Posts: 11386 |
Quote:What was the major blocker on basic to C? I would think it mostly has a 1:1 relationship.
not at all. you can do terrible things in basic v2 which just wont translate to C at all. like GOSUB somewhere, but never RETURN. or reuse parts of one subfunction from another. even after building a control flow graph and duplicating the things that would collide, you'd still end up with dead ends. not worth the trouble, easier to just convert by hand :)
Quote:READ would be the tricky part.
no not really, thats one of the easier parts infact :=P |
| |
DanPhillips
Registered: Jan 2003 Posts: 39 |
Yay back on the forum :)
We once used a bunch of macros to convert 65816 to c.
All the object handling/ai was converted when going from Snes to PC dos.
I don't have a copy of the macros, but I do remember they were "horrendous" :)
Cheers
Dan |
| |
chatGPZ
Registered: Dec 2001 Posts: 11386 |
ah i did this once when i converted 6502 code to gameboy :)
however - this doesnt make the code more readable at all =D |
| |
Remdy
Registered: Feb 2019 Posts: 26 |
I did it for (a subset) of DOS/PMODEW 386 TASM assembly code: https://github.com/frranck/asm2c |