MIPS computer structure

Thread Starter

FreddoAvishi

Joined Mar 8, 2019
36
Hello fellas !

I've to convert this code of recursion to Code MIPS assembly language, and I'm finding it hard at all to interact with recursion in terms of MIPS language;
may please I get help how to convert this code to MIPS with illustration of steps while converting it? specifically the step of converting recursion to MIPS language isn't understandable for me at all, so it would be much appreciated if you explain to me.

here's the code
Code:
int f(int n) 
{
 if (n < 5) 
         return n; 
return 1 + f(f(n – 1)); 
}

thanks
 

djsfantasi

Joined Apr 11, 2010
9,163
So, let’s set a level. I almost didn’t post, because I don’t know MIPS. But the process should be code agnostic.

The major impediment is a lack of understanding of what recursion is. What’s your understanding of recursion? Can you define it here to help us understand your skill or educational level is for this subject!
 

Thread Starter

FreddoAvishi

Joined Mar 8, 2019
36
So, let’s set a level. I almost didn’t post, because I don’t know MIPS. But the process should be code agnostic.

The major impediment is a lack of understanding of what recursion is. What’s your understanding of recursion? Can you define it here to help us understand your skill or educational level is for this subject!
recursion is something like opening new stack at every call of recursion with its own parameters, and once one of the calls finish its operation it closes its stack space but before closing it, the CPU updates the returned parameters upon $va and then it closes the certain stack's space.
 

WBahn

Joined Mar 31, 2012
30,072
recursion is something like opening new stack at every call of recursion with its own parameters, and once one of the calls finish its operation it closes its stack space but before closing it, the CPU updates the returned parameters upon $va and then it closes the certain stack's space.
That's good enough.

So you have two options -- the first is to write some assembly code that implements this concept of a stack frame for each pass (which is doable, but a pain) and the second is to convert the recursive algorithm into an interative algorithm, since implementing iterative algorithms in assembly is usually much simpler.
 

WBahn

Joined Mar 31, 2012
30,072
But what'ever it's already in code C, and on exams there's no compiler, there's your mind to do recursion by MIPS language :)
Notice that he didn't say to convert it to C, he said to convert it to "C without recursion". Huge difference.

For instance, let's say that you had the C code

C:
int f(int n)
{
   if (n < 1)
      return 1;

   return n * f(n-1);
}
You could convert that to

C:
int f(int n)
{
   int r = 1;

   for (int i = 1; i <= n; i++)
      r *= i;

   return r;
}
 

Thread Starter

FreddoAvishi

Joined Mar 8, 2019
36
Notice that he didn't say to convert it to C, he said to convert it to "C without recursion". Huge difference.

For instance, let's say that you had the C code

C:
int f(int n)
{
   if (n < 1)
      return 1;

   return n * f(n-1);
}
You could convert that to

C:
int f(int n)
{
   int r = 1;

   for (int i = 1; i <= n; i++)
      r *= i;

   return r;
}
You made it more than hard for me :)
 

Papabravo

Joined Feb 24, 2006
21,226
Hard because you don't understand the C-language or because you still cannot fathom the difference between a recursive and a non-recursive approach?
 

Thread Starter

FreddoAvishi

Joined Mar 8, 2019
36
Hard because you don't understand the C-language or because you still cannot fathom the difference between a recursive and a non-recursive approach?
Not C language at all, I'm totally skilled .. .I still can't fathom the difference ...

I mean the difference on recursion in aspect of MIPS assembly, not totally understand how recursion goes syntaxed(when we do save register ra and a0 and when we not ... .. )
 

Papabravo

Joined Feb 24, 2006
21,226
Not C language at all, I'm totally skilled .. .I still can't fathom the difference ...

I mean the difference on recursion in aspect of MIPS assembly, not totally understand how recursion goes syntaxed(when we do save register ra and a0 and when we not ... .. )
Look at it like this:
  1. In a non-recursive function, there is a loop which performs some number of operations where n > 0, and the called function NEVER calls itself.
  2. In a recursive function, there are a series of calls to the function you are currently executing which performs the same operations.
The tipoff is the ability of the compiler/assembler to implement a function that calls itself. The results should be identical although there may be large difference in performance and memory utilization. For an example of a totally pathological recursive function, try the Ackermann function. On small machines of the pre-IBM PC era (prior to 1982) this function would crash most computer systems at about A(5,4)

https://en.wikipedia.org/wiki/Ackermann_function for basic discussion
https://rosettacode.org/wiki/Ackermann_function for more code examples than you can shake a stick at
 
Top