简单的 C 函数到 MIPS 指令
我有一个简单的 c 函数,需要将其转换为 MIPS 指令以完成家庭作业。
功能是:
int load(int *ptr) {
return *ptr;
}
我想出的MIPS指令是:
load:
move $v0,$a0
jr $ra
这是正确的吗?
I have a simple c function that I need to convert to MIPS instructions for a homework assignment.
The function is:
int load(int *ptr) {
return *ptr;
}
my MIPS instruction I've come up with is:
load:
move $v0,$a0
jr $ra
Is this correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们分析一下这个函数。
首先,这里涉及到的一切都是什么类型?
ptr
是一个指向int
的指针。int
类型。接下来,该函数用它做什么?
int
指针(即读取指针指向的int
值)ptr
并返回该值。接下来考虑您的代码在做什么。
这是正确的吗?
我会说不。您实际上返回了指针,而不是指针指向的值。
你能做些什么呢?
请记住我们在这里处理的类型以及您对其所做的操作。您有参数(
int *
类型),并返回该参数(int
类型)。类型不匹配。我们在C程序中做了什么?我们取消引用指针来获取值。换句话说,将int *
转换为int
。你也需要这样做。Let's analyze the function for a second.
First of all, what are the types of everything involved here?
ptr
is a pointer to anint
.int
.Next, what does the function do with this?
int
pointer (i.e., reads theint
value that the pointer is pointing to)ptr
and returns that value.Next consider what your code is doing.
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 typeint
). 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 theint *
to anint
. You need to do the same.