1) Write a program in assembly language to create the following Fibonacci series.
{0,1, 1, 2, 3, 5, 8}
;MS Visual Studio 2017
INCLUDE irvine32.inc
.data
number dword 7
msgSpace BYTE " ",0
.code
main PROC
mov ebx, 1
mov eax, 0
call WriteDec ; write value (eax) first Fib = 0
mov edx, OFFSET msgSpace ; address of msg
call writeString
mov ebx, 1
mov eax, 1
call WriteDec ; write value (eax) first Fib = 1
mov edx, OFFSET msgSpace ; address of msg
call writeString ; writes a null-terminated string to standard output
cmp number, 1 ; counter = 1?
jz return
mov eax, 0 ; first = 0
mov ebx, 1 ; second = 1
mov ecx, 2 ; counter
begin:
cmp ecx,number ; counter = number?
jz return
mov edx, eax ; F(n-2)
add edx, ebx ; F(n) = F(n-1)+F(n-2) next = first + second
mov eax, ebx ; first = second
mov ebx, edx ; second = next
push eax
mov eax, edx
call WriteDec ; write value (eax)
mov edx, OFFSET msgSpace ; address of msg
call writeString ; writes a null-terminated string to standard output
inc ecx ; counter++
pop eax
jmp begin
return:
call crlf
call crlf
exit
main ENDP
END main
Comments
Leave a comment