For my current Computer Science class, our teacher gave us the Java algorithm fib(int n)
and told us to replicate it in MIPS assembly.
I thought I could do a better algorithm, and thus, I created ofib(int n)
in MIPS assembly and created a Java version as well.
If my calculations are correct, the provided fib
takes n*7 instructions to calculate the nth Fibonacci number (with my MIPS translation anyway: I tried my best to optimize the assembly within the algorithm), where my ofib
takes n*5 instructions (and I'm pretty sure that this is the best implementation of this algorithm as I optimized it for the assembly level directly).
I would not have been able to write ofib
without the insight optimizing fib
gave me. As such I understand why fib
was given as the example, as its algorithm is easier to follow. However, I wanted to go deeper optization.
This was done as a fun exercise. I don't know if this is the best algorithm, or even if it has any gain in a high-level world, but I had fun seeing how small/quick I could get the Fibbonacci generator to be.
In fact, I know I could get at least marginally more efficiency by treating everything as unsigned 32-bit integers. However, sticking to the prompt I'm sticking to signed. (At that point overflow would be happening a long time ago anyway.)