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?
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.