将 ASM 转换为 C(不是逆向工程) [英] Convert ASM to C (not reverse engineer)

查看:51
本文介绍了将 ASM 转换为 C(不是逆向工程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌上搜索,发现大量轻率的回答基本上是在嘲笑提问者提出这样的问题.

I googled and I see a surprising amount of flippant responses basically laughing at the asker for asking such a question.

Microchip 免费提供了一些源代码(我不想在这里发布,以防万一.基本上,谷歌 AN937,点击第一个链接,有一个源代码"链接,它是压缩的文件).它在 ASM 中,当我看到它时,我开始对视.我想将其转换为类似于 c 类型语言的东西,以便我可以跟进.因为像这样的行:

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:

GLOBAL  _24_bit_sub
movf    BARGB2,w
subwf   AARGB2,f

可能很简单,但对我来说毫无意义.

are probably very simple but they mean nothing to me.

可能有一些自动化的 ASM 到 C 的翻译器,但我能找到的只是人们说这是不可能的.坦率地说,它不可能是不可能的.两种语言都有结构,而且这种结构肯定可以翻译.

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

推荐答案

你绝对可以用汇编程序制作一个c程序.问题是它可能看起来不像你想的那样,或者它可能会.我的 PIC 生锈了,但使用另一个汇编器,说你有

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

add r1,r2

在 C 中可以说变成了

In C lets say that becomes

r1 = r1 + r2;

可能更具可读性.您可能会失去对变量名称的任何感觉,因为值正在从内存跳转到寄存器并返回并且寄存器正在被重用.如果您谈论的是具有两个寄存器一个累加器和另一个寄存器的较旧图片,那么实际上可能会更容易,因为变量大部分都在内存中,您可以查看地址,例如

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

q = mem[0x12];
e = q;
q = mem[0x13];
e = e + q;
mem[0x12] = e;

很长,但很明显 mem[0x12] = mem[0x12] + mem[0x13];

Long and drawn out but it is clear that mem[0x12] = mem[0x12] + mem[0x13];

这些内存位置很可能是变量,它们不会像编译后的 C 代码那样用于带有一堆寄存器的处理器.图片可能更容易找出变量,然后进行搜索和替换以在文件中命名它们.

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.

您要查找的内容称为静态二进制翻译,不一定是从一个二进制文件到另一个(一个处理器到另一个)的翻译,但在这种情况下,是从 pic 二进制文件到 C 的翻译.理想情况下,您需要使用汇编程序在应用笔记中给出并使用微芯片工具将其组装成二进制文件,然后进行翻译.您也可以进行动态二进制翻译,但您更不可能找到其中之一,而且它通常不会导致 C 而是一个二进制到另一个.有没有想过沃尔玛 15 美元的操纵杆与 pac-man 和 galaga 是如何工作的?来自街机的 rom 使用静态二进制翻译进行转换、优化和清理,并为手持设备中的新目标处理器编译了 C 或任何中间语言.我想并不是所有的人都是这样做的,但我很确定有些人是这样做的.

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.

百万美元的问题,你能找到一个图片的静态二进制翻译器吗?谁知道呢,你可能必须自己写一个.猜猜这意味着什么,你写了一个反汇编器,而不是反汇编成本地汇编语法中的指令,比如 add r0,r1 你让你的反汇编器打印出 r0=r0+r1;当你完成这个反汇编程序时,虽然你会非常了解 pic 汇编语言,以至于你不需要 asm 到 C 的翻译器.你有鸡和蛋的问题.

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.

这篇关于将 ASM 转换为 C(不是逆向工程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆