Initialize a MARIE program to generate and display the Fibonacci sequence.
· Prompt the user to input the number of terms (n) they want in the Fibonacci sequence.
· Calculate and display the Fibonacci sequence up to the nth term.
· Ensure that the program handles inputs correctly and terminates gracefully.
Note:
· Assume that the user will input a positive integer for the number of terms.
· The Fibonacci sequence should start from the 0th term and continue up to the nth term.
· Display each term of the Fibonacci sequence as it is calculated
ORG 100
; Define memory locations for variables
N, F1, F2, COUNTER, TEMP, INPUT_MSG, OUTPUT_MSG SET 0
; Prompt user for input
INPUT:
LOAD INPUT_MSG
OUTPUT
INPUT
STORE TEMP_INPUT
LOAD TEMP_INPUT
STORE N
; Validate user input (optional)
; You might want to check if the input is valid here
; Initialize Fibonacci sequence
LOAD 1
STORE F1
LOAD 1
STORE F2
LOAD 2
STORE COUNTER
; Display the initial values
LOAD F1
OUTPUT
LOAD F2
OUTPUT
; Calculate and display Fibonacci sequence
CALCULATE:
LOAD F1
ADD F2
STORE TEMP
LOAD F2
STORE F1
LOAD TEMP
STORE F2
LOAD COUNTER
ADD 1
STORE COUNTER
LOAD TEMP
OUTPUT
; Output the Fibonacci sequence here, if desired
LOAD N
SUB COUNTER
SKIPCOND 400
JUMP CALCULATE
INPUT_MSG, DEC 0
DATA “Enter the number of terms in the Fibonacci sequence:”
OUTPUT_MSG, DEC 0
DATA “Fibonacci sequence:”
TEMP_INPUT, DEC 0
everytime i tried to assemble this its showing some kind of error
Doisma Gurung is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.