Currently trying to understand how to convert some C into mips. Is a "return func" different from "return"? Both C codes are very similar yet im unsure how they will both look in code once completed. Are both of these codes the same? The second code is from another thread and I wanted to know if anyone could explain to me the differences when converted and why they are similar or different.

Here is the C Code:

``````int  f (int a, int b, int c, int d) {
If (a + b > c + d)
return (a + b);
return  (c + d);
}
``````

Here is a similar problem (from another thread):

``````int f(int a, int b, int c, ind d)
{
if (a+b > c+d)
return func(a + b, c + d);
return func(a + b, c + d);
}
``````

Thank you! :)
-Poppic

How well do you know C? How well do you know MIPS assembly? In order to understand how to convert from one language to another, you would need to understand, at least to some degree, both languages in question.

As for the two functions, they are not the same, and …

## All 3 Replies

How well do you know C? How well do you know MIPS assembly? In order to understand how to convert from one language to another, you would need to understand, at least to some degree, both languages in question.

As for the two functions, they are not the same, and the first is the simpler of the two, but it is still a little more complex than it may seem at first, because each of the individual lines of code are doing more than one thing, such that they would take at least thwo or more lines of assembly code. To show this, I'll put the C code into an idiom closer to that used by the assembly language:

``````int  f (int a, int b, int c, int d) {
int x, y;
x = a + b;
y = c + d;
if (x > y)
return x;
else
return y;
}
``````

What isn't evident from this is that returning a value in assembly language is itself a process of more than one step. The returned value has to be stored somewhere where the caller can retrieve it. In MIPS, the convention is to put the return value in the `\$v0` and `\$v1` registers (usually, just the `\$v0` value is used, unless the value is larger than 32 bits). Similar,y the first four arguments of the function are passed in the `\$a0`, `\$a1`, `\$a2`, and `\$a3` registers. So, in MIPS, the code above would come out as something like this:

``````f:
add \$t0, \$a0, \$a1       # add the first two arguments and put the result in \$t0
ble \$t0, \$t1, f.false   # if t0 is not gt t1, skip to f.false
move \$v0, \$t0           # put t0 into v0
b f.exit

f.false:                    # else,
move \$v0, \$t1           # put t1 in v0

f.exit:
jr \$ra
``````

The second function is superficially similar, but has one major difference: in the return lines, it is calling another function, and returning the return value of that function as it's own.

Thank you! Your response was very helpful. I have a good understanding of MIPs Assembly, and plan to learn this more advanced conversion from C to MIPs.

register \$s0 holds the base address of list my_data, \$s1 holds the length of the list my_data,
register \$s2 holds the base address of list my_data2 and \$s3 holds the length of the list
my_data2.
def sum_to_x(x):
sum = 0
for i in range (x):
sum += i
return sum
my_data = [1, 3, 6, 7]
my_data2 = [2, 1, 5]
for i in range( len( my_data ) ):
my_data[ i ] = sum_to_x( my_data[ i ] )
for i in range( len( my_data2 ) ):
my_data2[ i ] = sum_to_x( my_data2[ i ] )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.