ASSEMBLY X86 MS VISUAL STUDIO! Write an assembly program that takes n(where n<10
ID: 3914249 • Letter: A
Question
ASSEMBLY X86 MS VISUAL STUDIO!
Write an assembly program that takes n(where n<100) from a user and finds n Fibonacci numbers.
A Fibonacci series is a series of numbers in which the next element is the sum of previous two elements. Assume that the first two elements are already initialized as follows:
fibArr DWORD 0,1, 98 DUP(?)
Example:
If users gives n=5, the result should be:
fibArr = 0, 1, 1, 2, 3.
If user gives n = 7, the result should be
fibArr = 0, 1, 1, 2, 3, 5, 8.
Hint: You would use ReadInt function from the Irvine32 library.
Explanation / Answer
CODE in x86-64 gcc 8.1
.LC0:
.string "Enter the number of terms: "
.LC1:
.string "%d"
.LC2:
.string "Fibonacci Series: "
.LC3:
.string "Number should be smaller than 100"
.LC4:
.string "%d, "
main:
push rbp
mov rbp, rsp
sub rsp, 32
mov DWORD PTR [rbp-8], 0
mov DWORD PTR [rbp-12], 1
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
lea rax, [rbp-20]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call scanf
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
mov eax, DWORD PTR [rbp-20]
cmp eax, 100
jle .L2
mov edi, OFFSET FLAT:.LC3
mov eax, 0
call printf
jmp .L3
.L2:
mov DWORD PTR [rbp-4], 1
.L4:
mov eax, DWORD PTR [rbp-20]
cmp DWORD PTR [rbp-4], eax
jg .L3
mov eax, DWORD PTR [rbp-8]
mov esi, eax
mov edi, OFFSET FLAT:.LC4
mov eax, 0
call printf
mov edx, DWORD PTR [rbp-8]
mov eax, DWORD PTR [rbp-12]
add eax, edx
mov DWORD PTR [rbp-16], eax
mov eax, DWORD PTR [rbp-12]
mov DWORD PTR [rbp-8], eax
mov eax, DWORD PTR [rbp-16]
mov DWORD PTR [rbp-12], eax
add DWORD PTR [rbp-4], 1
jmp .L4
.L3:
mov eax, 0
leave
ret