Write an Assembly program using pep8 that reads an integer and then stores all F
ID: 3568181 • Letter: W
Question
Write an Assembly program using pep8 that reads an integer and then stores all Fibonacci numbers from 0 to the integer entered (including that) into an array of integers of maximum size 50. Once all numbers have been stored, print them and then read an integer and display it's index in the array if it's one of the series or print "Not in series", if not. To search for the number, use binary search on the array elements.
The following is the C++ program for binary search:
int vector[50];
//populate array with Fibonacci numbers up to the limit
int first = 0, last = size - 1, mid = (first + last) / 2; where size is the number of integers stored in the array
while(first <= last)
{
if(value < nums[mid]) //if value < middle value
{
last = mid - 1;
mid = (first + last) / 2;
}
else if(value > nums[mid])
{
first = mid + 1;
mid = (first + last) / 2;
}
else
{
cout << " Value found at index " << mid;
//return
}
}
cout << " Value is not part of the series.";
//return
Example:
Enter limit: 150
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 134
Enter number: 89
89 found at index 11.
use C++ code for commenting your statements.
Explanation / Answer
C++ program that reads an integer and stores all Fibonacci numbers from 0 to the integer entered into an array of integers of maximum size 50 ia as follows:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int vector[50];
//populate array with Fibonacci numbers up to the limit
int size = 50;
int first = 0, last = size - 1, mid = (first + last) / 2;
vector[0] = 0;
vector[1] = 1;
int i = 1;
while((vector[i] + vector[i-1]) < size)
{
vector[i+1] = vector[i-1] + vector[i];
i++;
cout<<vector[i]<<" ";
}
int value = 8;
//where size is the number of integers stored in the array
while(first <= last)
{
if(value < vector[mid]) //if value < middle value
{
last = mid - 1;
mid = (first + last) / 2;
}
else if(value > vector[mid])
{
first = mid + 1;
mid = (first + last) / 2;
}
else
{
cout << " Value found at index " << mid;
//return
}
}
cout << " Value is not part of the series.";
}
Assembly program that reads an integer and stores all Fibonacci numbers from 0 to the integer entered into an array of integers of maximum size 50 ia as follows:
main:
pushq %rbp
movq %rsp, %rbp
subq $240, %rsp
movl $50, -216(%rbp)
movl $0, -232(%rbp)
movl -216(%rbp), %eax
subl $1, %eax
movl %eax, -228(%rbp)
movl -228(%rbp), %eax
movl -232(%rbp), %edx
addl %edx, %eax
movl %eax, %edx
shrl $31, %edx
addl %edx, %eax
sarl %eax
movl %eax, -224(%rbp)
movl $0, -208(%rbp)
movl $1, -204(%rbp)
movl $1, -220(%rbp)
jmp
movl -220(%rbp), %eax
leal 1(%rax), %ecx
movl -220(%rbp), %eax
subl $1, %eax
cltq
movl -208(%rbp,%rax,4), %edx
movl -220(%rbp), %eax
cltq
movl -208(%rbp,%rax,4), %eax
addl %eax, %edx
movslq %ecx, %rax
movl %edx, -208(%rbp,%rax,4)
addl $1, -220(%rbp)
movl -220(%rbp), %eax
cltq
movl -208(%rbp,%rax,4), %eax
movl %eax, %esi
movl $_ZSt4cout, %edi
call _ZNSolsEi
movl $.LC0, %esi
movq %rax, %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl -220(%rbp), %eax
cltq
movl -208(%rbp,%rax,4), %edx
movl -220(%rbp), %eax
subl $1, %eax
cltq
movl -208(%rbp,%rax,4), %eax
addl %edx, %eax
cmpl -216(%rbp), %eax
jl
movl $8, -212(%rbp)
jmp
movl -224(%rbp), %eax
cltq
movl -208(%rbp,%rax,4), %eax
cmpl -212(%rbp), %eax
jle
movl -224(%rbp), %eax
subl $1, %eax
movl %eax, -228(%rbp)
movl -228(%rbp), %eax
movl -232(%rbp), %edx
addl %edx, %eax
movl %eax, %edx
shrl $31, %edx
addl %edx, %eax
sarl %eax
movl %eax, -224(%rbp)
jmp
movl -224(%rbp), %eax
cltq
movl -208(%rbp,%rax,4), %eax
cmpl -212(%rbp), %eax
jge
movl -224(%rbp), %eax
addl $1, %eax
movl %eax, -232(%rbp)
movl -228(%rbp), %eax
movl -232(%rbp), %edx
addl %edx, %eax
movl %eax, %edx
shrl $31, %edx
addl %edx, %eax
sarl %eax
movl %eax, -224(%rbp)
jmp
movl $.LC1, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl -224(%rbp), %edx
movl %edx, %esi
movq %rax, %rdi
call _ZNSolsEi
movl -232(%rbp), %eax
cmpl -228(%rbp), %eax
jle
movl $.LC2, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PK
movl $0, %eax
jmp
movq %rax, %rdi
call _Unwind_Resume
leave
ret
movl $_ZStL8__ioinit, %edi
call _ZNSt8ios_base4InitC1Ev
movl $__dso_handle, %edx
movl $_ZStL8__ioinit, %esi
movl $_ZNSt8ios_base4InitD1Ev, %edi
call __cxa_atexit
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
cmpl $1, -4(%rbp)
jne .L12
cmpl $65535, -8(%rbp)
jne
leave
ret
pushq %rbp
movq %rsp, %rbp
movl $65535, %esi
movl $1, %edi
call _Z41__static_initialization_and_destruction_0ii
popq %rbp
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.