1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 114, … etc. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. We define Fib(0)=0, Fib(1)=1, Fib(2)=1, Fib(3)=2, Fib(4)=3, etc. The first 22 Fibonacci numbers given below: Fib(0) Fib(1) Fib(2) Fib(3) Fib(4) Fib(5) Fib(6) Fib(7) Fib(8) Fib(9) Fib(10) 0 1 1 2 3 5 8 13 21 34 55 Fib(11) Fib(12) Fib(13) Fib(14) Fib(15) Fib(16) Fib(17) Fib(18) Fib(19) Fib(20) Fib(21) 89 144 233 377 610 987 1597 2584 4181 6765 10946 Write a MARIE program to calculate Fib(n), where the user inputs n. For example, if the user inputs 7, the program outputs the value 13; if the user inputs 15, the program outputs the value 610; if the user inputs 20, the program outputs the value 6765 etc. You need to write and run the program using MARIE simulator. Please include appropriate comments to make your code readable.[10 marks]

The Fibonacci numbers are a sequence of integers characterized by each number being the sum of the previous two numbers. The first two numbers in the sequence are always 0 and 1, and each subsequent number is obtained by adding the two preceding numbers. The Fibonacci sequence can be represented as: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

In this task, we are required to write a MARIE program that calculates the Fibonacci number at position ‘n’, where ‘n’ is a user input. For example, if the user inputs 7, the program should output the value 13, as the 7th Fibonacci number is 13. Similarly, if the user inputs 15, the program should output the value 610, as the 15th Fibonacci number is 610.

To accomplish this task, we will use a loop and variables to store the previous two numbers in the sequence. We will start with the initial values of Fib(0) = 0 and Fib(1) = 1, and then iteratively calculate the Fibonacci number at the given position ‘n’.

Below is the MARIE program code that implements this logic:

“`
INPUT ; Read user input for ‘n’
STORE n ; Store the value of ‘n’ in memory
LOAD n ; Load ‘n’ into the accumulator
STORE i ; Store ‘n’ in ‘i’
LOAD n ; Load ‘n’ into the accumulator
SUBT 1 ; Subtract 1 from ‘n’
STORE j ; Store ‘n-1’ in ‘j’

LOAD n ; Load ‘n’ into the accumulator
SKIPCOND 400 ; Skip next instruction if ‘n’ is zero
JUMP start ; If ‘n’ is not zero, jump to ‘start’

LOAD i ; Load ‘i’ into the accumulator
OUTPUT ; Output the value stored in ‘i’
HALT ; End program execution

start, LOAD i ; Load ‘i’ into the accumulator
ADD j ; Add ‘j’ to accumulator
OUTPUT ; Output the value in the accumulator
STORE temp ; Store the sum in a temporary variable

LOAD j ; Load ‘j’ into the accumulator
STORE i ; Store ‘j’ in ‘i’

LOAD temp ; Load the sum from ‘temp’ into the accumulator
STORE j ; Store the sum in ‘j’

LOAD n ; Load ‘n’ into the accumulator
SUBT 1 ; Subtract 1 from ‘n’
STORE n ; Store the updated value of ‘n’

JUMP start ; Jump back to ‘start’ to continue the loop

“`

This MARIE program initializes variables ‘i’, ‘j’, and ‘temp’ to 0 and stores the user input ‘n’ in ‘i’. It then enters a loop where it calculates the Fibonacci number at the position ‘n’ by adding ‘i’ and ‘j’ and storing the sum in ‘temp’. The values in ‘j’ and ‘temp’ are then swapped, and ‘n’ is decremented. The loop continues until ‘n’ becomes zero, at which point the final Fibonacci number is outputted and the program ends.

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Click Here to Make an Order Click Here to Hire a Writer