您能给我一些有关如何有效设计汇编语言程序的指导吗?

发布于 2025-02-14 01:01:06 字数 224 浏览 1 评论 0原文

我是大学的CS学生,我应该为考试制定MIPS Assembly语言程序,但是我确实很难设计它,尤其是在分配登记册,呼叫惯例之后,保存登记册Stack ...这一切似乎都变得非常混乱和压倒性,我失去了轨道!

至少从概念上讲,我没有问题来解决特定问题。它与更大的图景,项目的整体结构和设计有关。

鉴于此,您能指出一种模式,提供提示或最佳实践以在实际编写代码之前对所有内容进行整理,以便我不会在此过程中失去自己?

I'm a CS student at university, and I'm supposed to develop a MIPS assembly language program for an exam, but I do have a hard time designing it, especially when it comes to assigning registers, following calling conventions, saving registers on stack... It all seems to get pretty confusing and overwhelming rather fast, I lose track!

I don't have a problem figuring out, at least conceptually, an algorithm to solve a specific problem. It has more to do with the bigger picture, the overall structure and design of the project.

In light of this, could you point out a pattern, give tips or best practices to follow to sort everything out prior to actually writing code, so I don't lose myself in the process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花坊 2025-02-21 01:01:06

一般来说,我建议对汇编语言代码的有效设计建议:

在C中写出算法并运行它,并 对其进行测试以确保其工作 。 然后将其转换为汇编语言,而无需免费更改。

直接在汇编中开发算法很难,并且在汇编中修复算法错误并不是很有趣,部分原因是算法中可能出现的很小变化可能会对汇编代码产生巨大的影响。 /em>

许多人有渴望在给定的组装算法中进行改进 - 抵制 - 返回并在C版本中进行相同的改进,然后测试以确保它仍然工作。

您的C代码可能包含具有语句和表达式的数据结构,全局数据和功能。

对于语句,将控制流结构转换为if-Goto-Label形式。 C支持If-Goto-Label。 您可以将每个结构化语句(如果,如果)代替使用If-Goto-Label。 您也可以以任何顺序执行此操作,一次将一个结构化语句转换为IF-Goto-Label,并仍测试结果C代码以确保其继续工作。

另外,简化所有if-goto语句,例如,您已删除&& s and || s。 这意味着将联想和析取成多个if-Goto语句。

将复杂表达式转换为通过短寿命变量连接的作业,以揭示和显式这些短期变量。例如,在返回FIB(N-2) + FIB(N-1)中,我们可以介绍临时变量以保持函数调用结果:int t1 = fib(n-2) ; int t2 = fib(n-1);返回T1+T2。 这更清楚地表明我们需要一些临时变量,并且其中一个是t1是在函数调用中实时的,因此比t2 ,只需要短期斯托尔。

一旦拥有,就可以开始将C转换为组装。

在将C代码转换为组装过程中,首先翻译数据结构(如果需要),然后翻译全局数据,然后转换功能。

对于函数,转换参数&所有局部变量(将变量分配给寄存器或内存)。 为变量分配存储需要分析函数实现方式如何使用变量。

正式参数相对容易,它们遵循呼叫约定。 但是,您需要对函数本身内形式参数和局部变量的使用进行分析。 跨函数调用中实时的任何变量都需要内存的帮助才能在这些函数调用中生存。 如果正式参数,该功能输入中的该变量在一个函数调用中实时,则必须将其移至安全的位置。 在函数调用中未使用的任何变量都可以在参数和/或使用临时寄存器中保留。

然后翻译语句,然后翻译这些语句中的表达式(例如,分配,有条件测试,函数调用)。 保留退出的C代码的顺序,并以与C代码中的组件相同的方向转换零件部分。 您的代码已经以if-Goto-Label形式为单位,因此只需将其直接使用到汇编语言。


这些步骤中的每个步骤都相当小,可以遵循适当的规则和等价模式来掌握。

Generally speaking, what I recommend for the efficient design of assembly language code is the following steps:

Write out the algorithm in C and run it and test it to make sure it works.  Then translate that to assembly language rather mechanically and without gratuitous changes.

Developing an algorithm directly in assembly is hard, and, fixing algorithm bugs in assembly is not a lot of fun, in part because what might appear as a small change in the algorithm can have a dramatic effect on the assembly code.

Many have the urge to make improvements in a given algorithm for assembly — resist that — go back and make those same improvements in the C version, then test to make sure it still works.

Your C code may contain data structures, global data, and functions having statements and expressions.

For statements, convert control flow structures to if-goto-label form.  C supports if-goto-label.  You can substitute each structured statement (if,while,for) for something using if-goto-label.  You can do this in any order, too, one structured statement converted at a time to if-goto-label, and still test the resulting C code to make sure it continues working.

Also, simplify all if-goto statements, e.g. such that you have removed &&s and ||s.  This means splitting conjuctions and disjunctions into multiple if-goto statements.

Translate complex expressions into assignments connected by short-lived variables, so as to expose and make explicit these short-lived variables.  For example, in return fib(n-2) + fib(n-1), we can introduce temporary variables to hold the function call result: int t1 = fib(n-2); int t2 = fib(n-1); return t1+t2.  This makes clearer that we need some temporary variables, and that one of these, t1, is live across a function call so needs different physical storage (of call-surviving duration) than t2, which only needs short-term storge.

Once you have that, you can begin translation of the C to assembly.

During translation of C code to assembly, first translate data structures (if needed), then translate global data, then translate functions.

For functions, translate parameters & all local variables (assign variables to registers or memory).  Assigning storage for variables requires an analysis of how the variables are used by the function implementation.

Formal parameters are relatively easy, they follow the calling convention.  However, you need to perform an analysis on the usage of formal parameters and local variables within the function itself.  Any variable that is live across a function call will need some help from memory to survive those function calls.  If a formal parameter, that on function entry is in a parameter register and this variable is live across a function call, it will have to be moved to a safe place.  Any variables that are not live across a function call can be left in parameter and/or use temporary registers.

Then translate statements, then translate the expressions in those statements (e.g. assignments, conditional test, function calls).  Keep the order of the exiting C code and translate piece parts in the same orientation in assembly as they are in the C code.  Your code will already be in if-goto-label form so just take that rather directly to assembly language.


Each of these steps are, individually, fairly small, and can be mastered by following the proper rules and equivalence patterns.

坐在坟头思考人生 2025-02-21 01:01:06

与Java或Python(例如Java或Python)相比,MIPS是一种非常独特的语言,因此需要一些时间才能习惯它。我推荐的关于MIPS计划开发的方式是进行以下操作:

  1. 确保您理解并记住您的教授已通知您的每种MIPS指令,或者您知道您将接受测试。了解MIPS没有IF语句,因此您必须从头开始创建它们。
  2. 确保您了解每个寄存器可以加载哪些数据类型,$ t$ s
  3. 等在MIPS中创建有效的程序。因此,下一步是获取一些废纸或考试的一部分,您可以在其中写入并开始记下您想要的程序要做的事情,请考虑所有您拥有的MIPS指令,并尝试意识到哪种指令最适合每个任务。考虑是否需要创建任何if语句或循环,并使用适当的分支或跳跃指令。我真的认为这取决于记忆和真正理解每个说明的作用,以及要伴随它的登记册以制定良好的MIPS代码。

MIPS is a very unique language compared to other high level languages like Java OR Python, so it takes some time to get used to it. The way I would recommend about going with program development in MIPS is by doing the following:

  1. Make sure you understand and memorize each MIPS instruction that your professor has informed you about or that you know you will be tested on. Understand that MIPS has no if statements so you must create them all from scratch.
  2. Make sure you understand what data types each register can be loaded with, $t, $s etc.
  3. If you cannot do the first 2 steps, it will be near impossible to create an effective program in MIPS. So the next step is getting some scrap paper or a part of the exam where you can write on and start jotting down what you want your program to do, consider all the MIPS instructions you have and attempt to realize which instruction suits best for each task. Consider if you need to create any if statements or loops and use the appropriate branch or jump instruction. I truly believe it comes down to memorizing and truly understanding what each instruction does and which registers to accompany it with to develop good MIPS code.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文