如何使用c#将c代码文件转换为汇编代码? [英] How to convert a c code file to assembly code using c#?

查看:191
本文介绍了如何使用c#将c代码文件转换为汇编代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发ac#.net应用程序,它将代码转换为汇编代码。

请帮我看看如何将ac程序代码文件转换为

对应汇编代码使用c#代码。

I am developing a c#.net application which convert codes to assembly code.
Please help me to find out how to convert a c program code file to its
corresponding assembly code using c# code.

推荐答案

这并不像你想象的那么容易,你必须能够获取库,更改特殊构造(汇编没有一个FOR循环)等等。



为了做这样的事情,你首先必须将C文件解析为语法树 [ ^ ],你可以在google上搜索AST工具来帮助你做到这一点。这将让您验证C程序语法是否正确,因为您不想编译为错误的文件。



接下来,您需要开始更改部件将AST转换为等效的汇编,也不是一件容易的事,因为你需要跟踪堆栈和寄存器之类的事情。例如,在方法调用之前,您必须负责在堆栈中推送所有正确的东西。



虽然这里的步骤不是很好,但是是一种更简单的方法。您可以获取C文件并将其传递给实际的C编译器,在完成时保存中间输出(汇编文件)。你应该利用那里的工具而不是尝试重新发明轮子,特别是因为这个轮子就像你能得到的那么复杂。
This is not as easy as you may think, you have to be able to take libraries, change special constructs (assembly does not have a FOR loop), etc.

In order to do something like this you first have to parse the C file into its syntax tree[^], you can search google for AST tools to help you do this. This will let you verify that the C program is syntax correct, since you don't want to compile to a bad file.

Next, you need to start changing parts of the AST into equivalent assembly, also not an easy task since you need to keep track of things like stacks and registers. You will have to be responsible for push-ing all the right things on a stack before a method call for example.

While the steps here aren't great, there is a somewhat easier way. You could take the C file and pass it to an actual C compiler, saving the intermediate output (assembly files) when its done. You should leverage tools that are out there and work than try to reinvent the wheel, especially since this wheel is about as complicated as you can get.


你的工作将是一个非常复杂的任务,基本上你必须写一个 C 编译器。

使用工具可以加快这个过程。看看,例如在 ANTLR4 [ ^ ]。
Yours would be a very complex task, basically you have to write a C compiler.
Using a tool would speed-up the process. Have a look, for instance at ANTLR4[^].


这篇关于如何使用c#将c代码文件转换为汇编代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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