简体   繁体   English

简单的C函数转换为MIPS指令

[英]Simple C function to MIPS instructions

I have a simple c function that I need to convert to MIPS instructions for a homework assignment. 我有一个简单的c函数,需要将其转换为MIPS指令以进行作业。

The function is: 该函数是:

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

my MIPS instruction I've come up with is: 我想出的MIPS指令是:

load:
     move $v0,$a0
     jr $ra

Is this correct? 这个对吗?

Let's analyze the function for a second. 让我们分析一下功能。

First of all, what are the types of everything involved here? 首先,这里涉及的所有类型是什么?

  • ptr is a pointer to an int . ptr是一个指向int的指针。
  • the return value should be of type int . 返回值应为int类型。

Next, what does the function do with this? 接下来,此功能有什么作用?

  • dereferences the int pointer (ie, reads the int value that the pointer is pointing to) ptr and returns that value. 取消引用int指针(即,读出int ,该指针指向值) ptr并返回该值。

Next consider what your code is doing. 接下来考虑您的代码在做什么。

  • you moved the argument to the return value. 您将参数移至返回值。
  • return from the function. 从函数返回。

Is this correct? 这个对吗?

I'd say no. 我会说不。 You've essentially returned the pointer, not the value that the pointer was pointing to. 实际上,您已经返回了指针,而不是指针所指向的值。

What can you do about it? 你能为这个做什么?

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 ). 您有参数(类型为int * ),然后返回该参数(类型为int )。 The types do not match. 类型不匹配。 What did we do in the C program? 我们在C程序中做了什么? We dereferenced the pointer to get the value. 我们取消引用指针以获取值。 In other words, converted the int * to an int . 换句话说,将int *转换为int You need to do the same. 您需要做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM