Question: Write an assembly program in NASM that fills an array of N quadwords with the first N values in the Fibonacci Sequence. The Fibonacci Sequence
Write an assembly program in NASM that fills an array of N quadwords with the first N values in the Fibonacci Sequence. The Fibonacci Sequence is a number sequence that begins with two 1's, then each subsequent number in the sequence is the sum of the previous two numbers. The starter code for this project includes a constant value and an array defined in the .bss section. Constant fib_size specifies the size of the array to receive the Fibonacci Sequence, and fib is the array to be filled with the sequence values. Your program should be dependent on the array size specified by fib_size. In other words, if the constant value given for fib_size is changed, the size of the array also changes, and your program should fill the entire array without other changes to your code. You may assume that fib_size is at least 3 (your code need not consider lesser values). Note that the Fibonacci Sequence always begins with two 1's. All values beginning with the third value are calculated. As such, your program should begin by simply moving 1 to the first two positions of the array, then use a loop to fill the remainder of the array. The loop should execute fib_size-2 times. Check your results using the debugger. For convenience in checking your results, the first 20 Fibonacci Sequence positions.
So far, my code is as follows: I get lost around line 30.....
global _start ; exposes program entry point to the linker
section .text ; start of code segment
; Define constants
EXIT_SUCCESS equ 0 ; successful operation SYS_exit equ 60 ; call code for termination
_start:
mov rax,1 ; initialize value to be copied into array mov rsi,fib ; address of first array element mov [fib],rax ; moves 1st element "1" into array add rsi,8 ; increments rsi to 2nd array element add rbx,1 ; 2nd value of array mov [fib],rbx ; moves rbx value 1 to 2nd element into array [fib] = [1,1] mov rcx,fib_size ; set up loop counter .L1: add rsi,8 ; update address to next index add rax,rbx ; next #, (rdx = fiba+ fibb) mov [fib],rax add rsi,8 ; next index add rax,rbx ; rbx = (+next fib #rbx) mov rdx,rax sub rsi,8 add rdx,rsi mov rdx,rbx add [fib],rdx ; mov value to index array add rsi,8 ; next position of array add rax,rdx ; add rax + rdx mov rbx,rax ; overwrite rbx mov [fib],rbx ; move value index loop .L1
; End the program mov rax, 0x3c ; system call for exit xor rdi, rdi ; exit code 0 syscall ; invoke operating system call
section .data ; start of initialized data segment
section .bss ; start of uninitialized data segment
fib_size equ 20 ; Size of array to receive Fibonacci Numbers fib resq fib_size ; Array to receive Fibonacci Numbers
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
