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! :)

3 Years
Discussion Span
Last Post by 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 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;
        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:

    add $t0, $a0, $a1       # add the first two arguments and put the result in $t0
    add $t1, $a2, $a3       # add the third and fourth args
    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

    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.

Edited by Schol-R-LEA


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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.