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

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 ] )

#include <stdio.h> //header file

int size = 100; //global variable for size
int stack[100]; //stack declaration
int top = -1; //iterator for stack

int isempty() { //checking the stack is empty or not
if(top == -1)
return 1;
else
return 0;


int isfull() { //checking the stack is full or not
if(top == size)
return 1;
else
return 0;


int pop() { //pop the stack
int poped,i;
if(!isempty()) { //checking the stack is empty or not
poped = stack[top]; //last Element stored
top = top - 1; //deleting
printf("Stack = ");
for(i=0;i<=top;i++) //printing stack
printf("%d ",stack[i]);
printf("\n");
return poped; //returning poped Element

else
printf("Stack is empty.\n");


int push(int data) { //appending the Element to stack
if(!isfull()) { //checking stack is full or not
top = top + 1;   
stack[top] = data; //appending

else
printf("Could not insert data, Stack is full.\n");
int i;
printf("Stack = "); //printing Stack
for(i=0;i<=top;i++){
printf("%d ",stack[i]);

printf("\n");


int maxi(){ //printing maximum Element in stack
if(!isempty()){
int i,max=-1;
for(i=0;i<top+1;i++){
if(max<stack[i]){
max = stack[i];


printf("Max is %d\n",max); //printing max Element

else
printf("Stack is empty!\n");


int rotate(){ //for rotating the stack
if(!isempty()){
int arr[top+1],c=0,i;
for(i=top;i>-1;i--)
arr[c++] = pop(); //storing stack Element in an array from last
for(i=0;i<c;i++)
push(arr[i]); //appending Element to Stack from starting of array
printf("Stack = ");
for(i=0;i<=top;i++)
printf("%d ",stack[i]);
printf("\n");

else
printf("Stack is empty!\n");


int main(){
int choice;
do{
printf("\nEnter 1 to push, 2 to pop, 3 to find max, 4 to rotate, 0 to exit\n");
scanf("%d",&choice);
if(choice==1){
int ele;
printf("Enter Element : ");
scanf("%d",&ele); //taking the element which to be pushed
push(ele); // calling the push function

else if(choice==2){
pop(); //calling the pop function

else if(choice==3){
maxi(); //calling the maxi function

else if(choice==4){
rotate(); //calling the rotate function

else if(choice!=0){
printf("\n------Invalid Option------\n");
while(choice!=0);
commented: 6 years later? Be timely. -3

Just for fun I put something similar in to the online compiler at https://godbolt.org
I set the code type to C and the output to MIPS. Example follows:

// Note that If is now if.
int  f (int a, int b, int c, int d) {
    if ((a + b) > (c + d))
        return (a + b);
    return  (c + d);
}

int main() {
    int i;
    i = f (1,2,3,4);
}

And the resulting MIPS assembler output is:

f(int, int, int, int):
        daddiu  $sp,$sp,-32
        sd      $fp,24($sp)
        move    $fp,$sp
        move    $8,$4
        move    $4,$5
        move    $3,$6
        move    $2,$7
        sll     $5,$8,0
        sw      $5,0($fp)
        sll     $4,$4,0
        sw      $4,4($fp)
        sll     $3,$3,0
        sw      $3,8($fp)
        sll     $2,$2,0
        sw      $2,12($fp)
        lw      $3,0($fp)
        lw      $2,4($fp)
        addu    $2,$3,$2
        move    $3,$2
        lw      $4,8($fp)
        lw      $2,12($fp)
        addu    $2,$4,$2
        slt     $2,$2,$3
        beq     $2,$0,.L2
        nop

        lw      $3,0($fp)
        lw      $2,4($fp)
        addu    $2,$3,$2
        b       .L3
        nop

.L2:
        lw      $3,8($fp)
        lw      $2,12($fp)
        addu    $2,$3,$2
.L3:
        move    $sp,$fp
        ld      $fp,24($sp)
        daddiu  $sp,$sp,32
        j       $31
        nop

main:
        daddiu  $sp,$sp,-48
        sd      $31,40($sp)
        sd      $fp,32($sp)
        sd      $28,24($sp)
        move    $fp,$sp
        lui     $28,%hi(%neg(%gp_rel(main)))
        daddu   $28,$28,$25
        daddiu  $28,$28,%lo(%neg(%gp_rel(main)))
        li      $7,4                        # 0x4
        li      $6,3                        # 0x3
        li      $5,2                        # 0x2
        li      $4,1                        # 0x1
        ld      $2,%got_disp(f(int, int, int, int))($28)
        move    $25,$2
        nop

        sw      $2,0($fp)
        move    $2,$0
        move    $sp,$fp
        ld      $31,40($sp)
        ld      $fp,32($sp)
        ld      $28,24($sp)
        daddiu  $sp,$sp,48
        j       $31
        nop
Be a part of the DaniWeb community

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