Answer to Question #284828 in Assembler for Ahmed Ali

Question #284828

1) Write a program in assembly language to create the following Fibonacci series.

{0,1, 1, 2, 3, 5, 8}


1
Expert's answer
2022-01-04T13:55:12-0500
;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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS