计算机组织和体系结构中的 c 到 mips 代码

发布于 2024-08-31 19:56:25 字数 286 浏览 2 评论 0原文

int i ;

void set_array(int num){
  int array[10];
  for(i=0;i<10;i++){
    array[i]=compare(num,i){
  }
}

int compare(int a ,int b){
  if(sub(a,b)>=0)
    return 1;
  else
    return 0;
}

int sub(int a,int b){
  return a-b;
}

有人知道如何转换为 mips 代码吗

int i ;

void set_array(int num){
  int array[10];
  for(i=0;i<10;i++){
    array[i]=compare(num,i){
  }
}

int compare(int a ,int b){
  if(sub(a,b)>=0)
    return 1;
  else
    return 0;
}

int sub(int a,int b){
  return a-b;
}

anybody know how to convert to mips code

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

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

发布评论

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

评论(1

野侃 2024-09-07 19:56:25

首先,您需要一张备忘单:MIPS 指令参考< /a>

在为任何体系结构编写程序集时,一份好的参考表是最有用的。

其次,开始在高层分解该计划。您知道这些函数及其名称,所以从这里开始。首先定义函数并为每个函数创建一个非常基本的定义,以便它实际上是可调用的。

接下来,编写代码,从每个函数各自的位置提取函数参数。你很幸运,因为每个函数只接受一个基本整数。在调用函数之前,您需要将所有函数参数移至 $a0 - $a3 中。然后,您可以编写代码,在返回之前将返回值移至 $v0$v1 中。确保在您编写的每个函数中,将正在使用的寄存器保存到堆栈中,然后在 jal $ra 之前恢复它们。现在您应该有一些可调用并返回值的函数,但实际上不执行任何操作。

最后,实际编写函数所做的垃圾。再说一遍,你很幸运,因为你只使用 int...所以每个数字都将匹配一个寄存器,并且在引用堆栈时只需要偏移 4 个字节...让事情变得更容易。我会给你一个关于 int sub(int a, int b) 的提示...它看起来像这样:

sub  $t2, $t1, $t0

哈哈,看到了吗?编写汇编并不难:D

当然,您必须将结果移到函数末尾的 $v0' 或$v1` 中,然后跳回被调用方。

无论如何,希望这能让你开始。

First of all, you need a cheat sheet: MIPS Instruction Reference

A good reference sheet is the most useful thing possible when writing assembly for any architecture.

Secondly, start breaking the program down on the high level. You know the functions and their names, so start there. Start by defining the functions and creating a very basic definition for each function so that it is actually callable.

Next, write the code that will pull the function parameters from their respective places for each function. You are lucky here as each function just accepts a basic integer. You will need to move any function parameters into $a0 - $a3 before calling the function. You can then write the code that moves your return value into $v0 or $v1 before returning. Make sure in each function you write, you save off the registers you are using onto the stack and then restore them before your jal $ra. Now you should have some functions that are callable and return values, but don't really do anything.

Lastly, actually write the junk that your functions do. Again, you are luck as you are only using int... so each number will match to one register and you will only need to offset by 4 bytes when referencing the stack... making things easier. I'll give you a hint for int sub(int a, int b)... it'll look something like this:

sub  $t2, $t1, $t0

Haha, see? It's not so hard to write assembly :D

Of course you will then have to move your result into $v0' or$v1` at the end of the function and jump back to the callee.

Anyways, hope that gets you started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文