59.2k views
5 votes
Write an LEGv8 assembly program to compute the sum of the generated Fibonacci series. Do not use recursion. Assume the user input n is a 64-bit non-zero positive integer which is stored in X19 and n is the range of the series. Compute the sum of the generated Fibonacci series and store the sum in X20. For example, a Fibonacci series with range of 5 will have 7 terms (0, 1, 1, 2, 3, 5, 8) and the sum is 20. The first two terms (0 and 1) are considered as default terms and is not included in the range.

2 Answers

4 votes

Final answer:

To compute the sum of a generated Fibonacci series in LEGv8 assembly without using recursion, use an iterative approach. Initialize X20 to 0 and X21 and X22 to represent the first two terms. Use a loop to generate the next term, update the sum, and update the values of X21 and X22. Repeat until the desired number of terms is reached. The final sum will be stored in X20.

Step-by-step explanation:

Solution:

To compute the sum of a generated Fibonacci series in LEGv8 assembly without using recursion, you can use an iterative approach. Here's how you can do it:

  1. Initialize X20 to 0 to store the sum.
  2. Initialize X21 and X22 to 0 and 1, respectively, to represent the first two terms of the series.
  3. Use a loop to generate the next term of the series.
  4. Inside the loop, add the current term (X22) to X20 to update the sum.
  5. Update the values of X21 and X22 by shifting X22 to X21 and adding X21 and X22 to get the next term.
  6. Repeat steps 4 and 5 until the desired number of terms (n-2) is reached.
  7. The final sum will be stored in X20.

Here's an example implementation:

.section .data

.section .text
.align 2
.globl _start

_start:
movi x20, 0 // Initialize sum to 0
movi x21, 0 // Initialize term 1 to 0
movi x22, 1 // Initialize term 2 to 1

loop:
add x20, x20, x22 // Add current term to sum
add x21, x22, x21 // Update term 1
add x22, x21, x22 // Update term 2
subi x19, x19, 1 // Decrement n
bnez x19, loop // Loop until n=0

// Sum of Fibonacci series is stored in x20

// Exit
mov x0, x20
movi x1, 93 // Exit syscall number
syscall
User POV
by
8.2k points
5 votes

The program assumes that the user input n is a 64-bit non-zero positive integer stored in X19 and computes the sum, storing it in X20.

.data

sum: .quad 0 // Initialize sum to 0

.text

.global _start

_start:

LDR X20, sum // Load initial sum value

LDR X21, #0 // Initialize current Fibonacci term

LDR X22, #1 // Initialize next Fibonacci term

LDR X19, [X19] // Load the value of n

CMP X19, #0 // Check if n is 0

BEQ end_program

CMP X19, #1 // Check if n is 1

BEQ end_program

ADD X19, X19, #-2 // Adjust n to get the correct number of terms

fibonacci_loop:

ADD X23, X21, X22 // Calculate the next Fibonacci term

ADD X20, X20, X23 // Add the term to the sum

MOV X21, X22 // Update the current term

MOV X22, X23 // Update the next term

ADD X19, X19, #-1 // Decrement the counter

CBZ X19, end_program // Check if we have reached the end of the series

B fibonacci_loop // Repeat the loop

end_program:

STR X20, sum // Store the final sum

// Exit the program (you may need to use appropriate system call)

User Sebastian Zarnekow
by
8.5k points