简单的C函数MIPS指令 [英] Simple C function to MIPS instructions

查看:596
本文介绍了简单的C函数MIPS指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要转换为MIPS指令的家庭作业一个简单的C函数。

I have a simple c function that I need to convert to MIPS instructions for a homework assignment.

功能是:

int load(int *ptr) {
     return *ptr;
}

我的MIPS指令,我想出是:

my MIPS instruction I've come up with is:

load:
     move $v0,$a0
     jr $ra

这是正确的?

推荐答案

让我们来分析函数的第二个。

Let's analyze the function for a second.

首先,什么都在这里涉及的类型?

First of all, what are the types of everything involved here?


  • PTR 是一个指向 INT

  • 的返回值的类型应该为 INT 的。

  • ptr is a pointer to an int.
  • the return value should be of type int.

接下来,什么功能怎样做呢?

Next, what does the function do with this?


  • 取消引用 INT 指针(即读取指针指向 INT 值) PTR 并返回该值。

  • dereferences the int pointer (i.e., reads the int value that the pointer is pointing to) ptr and returns that value.

下一步考虑你的code在做什么。

Next consider what your code is doing.


  • 您移动的参数,返回值。

  • 从函数返回。

这是正确的?

我说没有。你已经基本上返回的指针,而不是指针所指向的值。

I'd say no. You've essentially returned the pointer, not the value that the pointer was pointing to.

你能做些什么呢?

清楚地记得,我们正在处理在这里和你做了什么与它的类型。你有你的(类型为int * )的参数并返回该(类型 INT )。的类型不匹配。什么我们在C程序呢?我们取消引用的指针来获取值。换句话说,转换后的为int * INT 。你需要做的是一样的。

Well remember the types that we're dealing with here and what you did with it. You have your argument (of type int *) and you return that (of type int). The types do not match. What did we do in the C program? We dereferenced the pointer to get the value. In other words, converted the int * to an int. You need to do the same.

这篇关于简单的C函数MIPS指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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