Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an assembly program that generates the first n numbers of the Fibonacci se

ID: 3695392 • Letter: W

Question

Write an assembly program that generates the first n numbers of the Fibonacci series. In this series, the first two numbers are 0 and 1, and each subsequent number is generated by adding the preceding two numbers. For example, for n = 8, the series is 0, 1, 1, 2, 3, 5, 8, 13

Fib(n) = Fib(n — 1) + Fib(n — 2)

Your program should store the numbers in successive memory word locations starting at 0x10010000 The “n” number should be a variable that can be changed in the code.

Program must be run using MIPS simulator to examine the memory locations starting at 0x10010000.

Explanation / Answer

Source Program:
MVI D, COUNT : Initialize counter
MVI B, 00 : Initialize variable to store previous number
MVI C, 01 : Initialize variable to store current number
MOV A, B :[Add two numbers]
BACK: ADD C :[Add two numbers]
MOV B, C : Current number is now previous number
MOV C, A : Save result as a new current number
DCR D : Decrement count
JNZ BACK : if count 0 go to BACK
HLT : Stop.

------------------------------------

-----------------------------------

INCLUDE Irvine32.inc

.DATA

; insert variables here

      dPrevious DWORD 0

dCurrent  DWORD 1

dNext DWORD 1

dNumberOfTerms DWORD 0

dSum DWORD 0

msg1 BYTE "Enter Number of Fibonacci Series Terms : ",0 ; A null-terminated string

msg2 BYTE "Fibonacci Series is ",0

msg3 BYTE "Sum of Fibonacci Series  = ",0

msg4 BYTE "Error: Fibonacci Series Contain at least 2 number of terms",0

msgFibonacciSeries BYTE "Display Fibonacci Series and Sum of Fibonacci Series in Assembly Language ",0

.CODE

; insert all executable statements here

main PROC

mov edx, OFFSET msgFibonacciSeries  ; "Display Fibonacci Series and Sum of Fibonacci Series in Assembly Language "

call WriteString                    ; Display a null-terminated string.

call Crlf                           ; Writes an end-of-line sequence to the console window.

mov edx, OFFSET msg1      ; "Enter Number of Fibonacci Series Terms : "

call WriteString          ; Display a null-terminated string.

call ReadInt              ; Input a 32-bit signed decimal integer

                                  ; from the keyboard and save in EAX.

      mov dNumberOfTerms, eax

mov ebx,dNumberOfTerms

cmp ebx,1                ; compare EBX and 1

      jle Error                ; jump if Less than or equal  (<=)

mov edx, OFFSET msg2     ; "Fibonacci Series is "

call WriteString

call Crlf                ; Writes an end-of-line sequence to the console window.

mov eax,dPrevious

call WriteDec            ; Display an unsigned 32-bit integer value

                                 ; in EAX in decimal format.

add dSum,eax

call Crlf

mov eax,dCurrent

call WriteDec

add dSum,eax

call Crlf

mov ecx,dNumberOfTerms    ; ECX  initialize with Number Of Terms

sub ecx,2                 ; Exclude First Two Terms

cmp ecx,0                 ; compare EBX and 0

      je showSum                ; jump if equal  (=)

doPart1:

mov ebx,dPrevious

add ebx,dCurrent

mov dNext,ebx

mov eax,DNext

call WriteDec

call Crlf

add dSum,eax

mov eax,dCurrent

mov dPrevious,eax

mov eax,dNext

mov dCurrent,eax

loop doPart1        ; if ECX == 0 Then loop stop

showSum:

mov edx, offset msg3

call WriteString

mov eax,dSum

call WriteDec

call Crlf

jmp next

Error:

mov edx, OFFSET msg4     ; "Error: Fibonacci Series Contain at least 2 number of terms"

call WriteString

call Crlf

next:

call WaitMsg    ; Displays a message and waits for a key to be pressed.

exit            ; The exit statement (indirectly) calls a predefined

                    ; MS-Windows function that halts the program.

main ENDP  ; The ENDP directive marks the end of the main procedure.

END main       ; The END directive marks the last line of the program to be assembled.

INCLUDE Irvine32.inc

.DATA

; insert variables here

      dPrevious DWORD 0

dCurrent  DWORD 1

dNext DWORD 1

dNumberOfTerms DWORD 0

dSum DWORD 0

msg1 BYTE "Enter Number of Fibonacci Series Terms : ",0 ; A null-terminated string

msg2 BYTE "Fibonacci Series is ",0

msg3 BYTE "Sum of Fibonacci Series  = ",0

msg4 BYTE "Error: Fibonacci Series Contain at least 2 number of terms",0

msgFibonacciSeries BYTE "Display Fibonacci Series and Sum of Fibonacci Series in Assembly Language ",0

.CODE

; insert all executable statements here

main PROC

mov edx, OFFSET msgFibonacciSeries  ; "Display Fibonacci Series and Sum of Fibonacci Series in Assembly Language "

call WriteString                    ; Display a null-terminated string.

call Crlf                           ; Writes an end-of-line sequence to the console window.

mov edx, OFFSET msg1      ; "Enter Number of Fibonacci Series Terms : "

call WriteString          ; Display a null-terminated string.

call ReadInt              ; Input a 32-bit signed decimal integer

                                  ; from the keyboard and save in EAX.

      mov dNumberOfTerms, eax

mov ebx,dNumberOfTerms

cmp ebx,1                ; compare EBX and 1

      jle Error                ; jump if Less than or equal  (<=)

mov edx, OFFSET msg2     ; "Fibonacci Series is "

call WriteString

call Crlf                ; Writes an end-of-line sequence to the console window.

mov eax,dPrevious

call WriteDec            ; Display an unsigned 32-bit integer value

                                 ; in EAX in decimal format.

add dSum,eax

call Crlf

mov eax,dCurrent

call WriteDec

add dSum,eax

call Crlf

mov ecx,dNumberOfTerms    ; ECX  initialize with Number Of Terms

sub ecx,2                 ; Exclude First Two Terms

cmp ecx,0                 ; compare EBX and 0

      je showSum                ; jump if equal  (=)

doPart1:

mov ebx,dPrevious

add ebx,dCurrent

mov dNext,ebx

mov eax,DNext

call WriteDec

call Crlf

add dSum,eax

mov eax,dCurrent

mov dPrevious,eax

mov eax,dNext

mov dCurrent,eax

loop doPart1        ; if ECX == 0 Then loop stop

showSum:

mov edx, offset msg3

call WriteString

mov eax,dSum

call WriteDec

call Crlf

jmp next

Error:

mov edx, OFFSET msg4     ; "Error: Fibonacci Series Contain at least 2 number of terms"

call WriteString

call Crlf

next:

call WaitMsg    ; Displays a message and waits for a key to be pressed.

exit            ; The exit statement (indirectly) calls a predefined

                    ; MS-Windows function that halts the program.

main ENDP  ; The ENDP directive marks the end of the main procedure.

END main       ; The END directive marks the last line of the program to be assembled.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote