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

Consider the following function: int counter( int n) { int s = 0;    for ( int...
Consider the following function: int counter( int n) { int s = 0;    for ( int i = 0; i < n; i++)             for ( int j = i; j > 0; j--)                        s = s + 1;   return s; } Part (a): How many times "s=s+1;" will be executed? Determine a precise count.  Show all your work. Part (b): What is the time complexity of the "counter" function in terms of Big-O notation? Justify and show all your work.
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...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as a parameter, put all prime numbers less than n into an array, and print out the array and the sum of the numbers in that array. You can use the isPrime function above to save time.
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.
Find the runtime of this function, where n is an integer. int function(int n) { int...
Find the runtime of this function, where n is an integer. int function(int n) { int a = 0; for (int i = n; i > 0; i--) { for (int j = 0; j < i; j++) { a = a + i + j; } } return a; } Find the runtime of this function, where m and n are two integers. int f(int m, int n) { if (m==1 || n==1) { return 1; } return f(m,...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
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...
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next;...
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next; }; ListNode *head; // List head pointer Assume that a linked list has been created and head points to the first node. Write code that traverses the list displaying the contents of each node’s value member Now write the code that destroys the linked list
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {  ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {    cout<<"? ";    if (n > 0)      mystery (n - 1); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT