Question: Computing Logarithm with Recursion This problem focuses on writing a recursive function in assembly in order to practice correctly preserving the architectural state of the

Computing Logarithm with Recursion
This problem focuses on writing a recursive function in assembly in order to practice correctly preserving the architectural state of the system when calling procedures. As with previous assignments, in order for your solution to be graded, it must work with my MUnit tests.
Follow these rules:
Give your procedures the exact names indicated in this write up.
Your procedures must be declared .globl.
The method parameters will be placed in registers a0, a1, and a2.
The method return value must be placed in register v0.
Each method must end with the instruction jr $ra.
Use the literal value 1 for true and 0 for false.
The Help menu in MARS has a list of instructions, or you may find it helpful to find a set on Google, such as this MIPS instruction reference. MIPS Conventions
This problem will be impossible to do if you do not understand how to use functions and the stack. paying careful attention to the table of preserved and non-preserved registers. Remember, no registers are preserved automatically -- you have to be the one to store them! In particular, be sure to use the $s* and $t* registers correctly. Problem: we often work with powers of two. To answer a question such as "how many bits do I need to represent 32 registers?", we need to be able to take a base-two logarithm. Because 32=25, we know that log2(32)=5. You can find various reviews of how logarithms work online if you need a refresher, but understanding them will not be strictly necessary to complete this task.
For this assignment, you are going to implement a recursive version of log2(x). Because we work primarily with integers , your function will return log2(x) rounded down to the next integer. For example, log2(5)2.32192809489, but your function will return log_two(5)=2. Implement log_two(int x). The formula for log_two(x)(rounded down) is
log_two(x)=1+ log_two(x/2)
where the division is also rounded down to the next integer. We also know that
log_two(1)=0
Write a recursive function named log_two to implement this formula. Note: You must implement the function using this recursive formula. If your log_two function does not call log_two inside itself, you have implemented an iterative solution. There should be no loops in your code.
log2(x) is defined only for positive x. For invalid agruments (i.e., arguments less than one), return -1.
Recall that when we want to multiply or divide by powers of two, we do not actually call mult or div. Instead, we implement this using shifts.
Here is a test class import org.junit.*; import static edu.gvsu.mipsunit.munit.MUnit.Register.*; import static edu.gvsu.mipsunit.munit.MUnit.*; public class LogTwoTest { @Before public void before(){ set(v0,1337); }/******************************************************************** Test log_two ******************************************************************/ @Test public void log_two_one(){ run("log_two", 1); Assert.assertEquals(0, get(v0)); } @Test public void log_two_zero(){ run("log_two", 0); Assert.assertEquals(-1, get(v0)); }/******************************************************************** Write many more tests! * Test Edge Cases *******************************************************************/}// end class to use as a starting point for your MUnit tests. Make sure to add more test cases! log_two.asm?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!