1

I working on translating some C code to MIPS and got stuck on this one line.

int f(int n, int m)

I know this is supposed to be for initializing the variables but how would it look like in MIPS? I'm having n=$a0 and m=$a1.

For Context here is the whole code:

int f(int n, int m) {    
   if (n ≤ 0)
      return m;
   else
      return f(n-1, n+m);
 }

I already understand how the loop works just for some reason I'm stuck on this.

iray1995
  • 11
  • 2
  • 1
    It's a function definition — a function called `f` that takes two `int` arguments `n` and `m` and returns an `int` value. The implementation uses recursion. You need to reread your textbook on functions in C. Also, the `≤` symbol is not valid in C; you use `<=` (two symbols). – Jonathan Leffler Mar 02 '19 at 18:08

2 Answers2

2

it is the recursive function but any optimizing compiler will actually remove the recursion.

int f(int n, int m) {    
   if (n <= 0)
      return m;
   else
      return f(n-1, n+m);
 }

f:
.L3:
        MOV.B   #0, R14
        CMP.W   R12, R14 { JGE        .L1
        ADD.W   R12, R13
        ADD.W   #-1, R12
        BR      #.L3
.L1:
        MOV.W   R13, R12
        RET
0___________
  • 60,014
  • 4
  • 34
  • 74
1

Its a simple c recursive function. function name is f and it takes two arguments m and n and simply adds a series of n to 1 in m.

for example if you pass f(3,5) it will return 3+2+1 + 5 = 11 or f(4,5) will return 4+3+2+1 + 5 = 15

Rajnesh
  • 798
  • 5
  • 22