Modify the following ARM code so that it now has a “main” that simply calls a RE
ID: 3861869 • Letter: M
Question
Modify the following ARM code so that it now has a “main” that simply calls a RECURSIVE fibonacci function with a single parameter of 16 (that is retrieved from a memory labeled “fibinput”. This function will return the 16th fibonacci value and the main will then store only this one value back into memory in a memory location labeled “fibnumber”.
Here is the ARM lab That need to modify:
ldr R4, =copyPrms
MOV R0, #0
MOV R1, #24
MOV R2, #1
again ADD R3, R2, R0
SUBS R1, R1, #1
MOV R0, R2
MOV R2, R3
str R3, [R4], #4
BNE again
copyPrms DCD 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
END
Here is what the recursive Java version of the code might look like:
public static int fibonacci(int number) {
if (number == 1 || number == 2) {
return 1;
}
return fibonacci(number-1) + fibonacci(number -2);
}
Explanation / Answer
import java.util.Scanner;
public class Fibonacci {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
int num = input.nextInt();
long fibo = findsFibo(num); System.out.println(fibo);
num = input.nextInt();
fibo = findsFibo(num); System.out.println(fibo);
}
public static long findsFibo(int n) {
long fn_2 = 1;
long fn_1 = 0;
long fn = 0;
for (int i = 1; i <= n; i++) {
fn = fn_2 + fn_1;
fn_2 = fn_1;
fn_1 = fn;
}
return fn;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.