Question

In: Computer Science

Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...

Consider the following C code that outlines Fibonacci function

int fib (int n) {

if (n == 0) return 0;

else if (n==1) return 1;

else return fib(n-1) + fib (n-2);

}

For this programming assignment, write and test an ARMv8 program to find Fibonacci (n).

You need to write a main function that calls the recursive fib function and passes an argument n.

The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to fib

(or n) is always passed in X1. The function fib always returns its result in register X2.

You need to save the return address (contained in X30) and input n (contained in X1) on stack.

When you call fib recursively, you are changing the value of n and thus modifying X1. You need

the old value when you return from a recursive call. After calling fib(n-1) and before calling

fib(n-2), you must also save X2 which contains the result returned by fib(n-1) on stack. This is

because fib(n-2) will also return its result in X2. After returning from fib (n-2), you need to

retrieve previously saved result from fib(n-1), add it to the result returned by fib(n-2) and return

the sum again in X2. If you are careful in how you are using other registers, you do not need to

save any other values on stack. Also remember you need to increment or decrement stack pointer

in increments of 16. If you are saving 3 values (X1, X2 and X30) each time you enter fib, you

need to decrement and increment SP by 32.

Here is a pseudo assembly code you can use to write fib. I am hoping you already know how to

declare data section, initialize variable n and main program which gets the address of n, reads the

value from memory and calls fib by storing n in X1.

fib:

check if n is zero, if yes, set X2 =0 and return

check if n is one, if yes, set X2 = 1 and return

//note we did not have to save anything on stack if either of these two conditions

// are true, since we will not be calling fib recursively

if n is greater than 1

decrement stack pointer

save n (X1) and return address (X30) on stack

set X1 = X1-1 , that is n = n-1

call fib (this time with n-1)

upon return from fib (n-1), save X2 on Stack

restore n (X1) from stack

set X1=X1-2

call fib (that is fib(n-2))

upon return

restore previously saved value (X2) from stack

you can load this into X3

set X2 = X2+X3 (new value to be returned)

restore return address (X30)

increment stack pointer

return

Test input: n= 7

Submit: A file containing your ARMv8 code, a read me file (if needed), a snapshot of stack after

main calls fib (first call) and a snapshot of stack when n becomes 1 (just before the recursion

starts unwinding). Also show a snapshot of register contents at the time main calls fib (showing

that X1 contains n), and a snapshot showing the final values in registers (to show that X2

contains the final result).

Solutions

Expert Solution

HELLO I AM ADDING ARMv8 CODE FOR FIBONACCI

PLEASE GO THROUGH IT ONCE
THANK YOU

.syntax unified

.equ maxfib,4000000

previous .req r4
current  .req r5
next     .req r6
sum      .req r7
max      .req r8
tmp      .req r9

.section .rodata
 .align 2
fibstring:
 .asciz "fib is %d\n"
sumstring:
 .asciz "%d\n"

.text
 .align 2
 .global main
 .type main, %function
main:
 stmfd sp!, {r4-r9, lr}
 ldr max,   =maxfib
 mov previous, 1 
 mov current, 1
 mov sum, 0

loop:
 cmp current, max
 bgt last

 add next, current, previous

 movs tmp, current, lsr 1      @ setting the carry flag from lsr 
                               @ this will update the status register
 addcc sum, sum, current       @ we add current to the sum ONLY if carry clear is true 


 mov previous, current
 mov current, next
 b loop

last:
 mov r1, sum  
 ldr r0, =sumstring            @ storing address ofstarting of the string to R0
 bl printf

 mov r0, 0
 ldmfd sp!, {r4-r9, pc}
 mov r7, 1                     
 swi 0                         

Related Solutions

Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
How to code the following function in C? (NOT C++) int vehicleInsert(HashFile *pHashFile, Vehicle *pVehicle) This...
How to code the following function in C? (NOT C++) int vehicleInsert(HashFile *pHashFile, Vehicle *pVehicle) This function inserts a vehicle into the specified file. • Determine the RBN using the driver's hash function. • Use readRec to read the record at that RBN. • If that location doesn't exist or the record at that location has a szVehicleId[0] == '\0': o Write this new vehicle record at that location using writeRec. • If that record exists and that vehicle's szVehicleId...
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)       {        for(int k=0;k<j;k++)        cout<<A[k]<<" + ";        cout<<rem<<"\n";        return;       }     for(int i=0;i<=rem;i++)    {          if(i<=rem)          A[j]=i;          printperm(A,n-1,rem-i,j+1);    } }
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return } Selection_Sort(int a[], int z) { …..// do the selection sort } main() {     Selection_Sort(array, size);     Binary_Search(array, item); }
Consider the following program written in C syntax: void swap(int a, int b) { int temp;...
Consider the following program written in C syntax: void swap(int a, int b) { int temp; temp = a; a = b; b = temp;} void main() { int value = 2, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]); swap(list[0], list[1]); swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap? Passed by value Passed by reference Passed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT