How do I code the Fibonacci sequence LMC?

I was wondering if it was possible to create a program that creates the Fibonacci Sequence in "Little Man Computer".

The program would output in the letter boxes the individual numbers of the sequence. The only input made would be asking the user how high it would like the sequence to go. For example if I input "20" it would only go up to to number 13.

Any help would be greatly appreciated,

Isaac.

Answers


Source: link

        INP
        STA N
LOOP    LDA A
        SUB N
        BRP ENDLOOP
        LDA A
        OUT
        LDA B
        ADD A
        STA ACC
        LDA B
        STA A
        LDA ACC
        STA B
        BRA LOOP
ENDLOOP HLT
A       DAT 0
B       DAT 1
N       DAT
ACC     DAT

The fibonacci series can be printed in the LMC in the following manner:

     INP
      STO   n     #number of terms
      LDA   one
      OUT

loop  LDA   fib     #THE MAIN LOOP THAT PRINTS FIBONACCI NUMBERS
      STO   temp2
      ADD   temp
      STO   fib
      LDA   temp2
      STO   temp
      BR    check

check LDA   n         #LOOP TO KEEP A TRACK OF NUMBER OF TERMS
      BRZ   halt
      SUB   one
      STO   n
      BRZ   halt
      LDA   fib
      OUT
      BR    loop

halt  HLT        
n     DAT   000
fib   DAT   001
temp  DAT   001
temp2 DAT   000
one   DAT   001

Explanation: The program first asks for an input n, the number of terms to be printed.Then the loop starts running - it will print the default value of variable fib, which has been set to 1. The value of fib is stored in another variable temp2, and the variable temp is used to record the previous value of fib so that the numbers are added correctly and a correct sequence is obtained. The loop controls the values of fib, temp and temp2 as the number of terms are increased.

The program then checks if the number of terms required have been printed and decrements the value of n. If n=0 it means that the required number of terms has been reached and the program halts. If n is not zero, the loop continues till n=0.

**Though there might me more efficient methods, however this one works correctly and uses a pretty less number of mailboxes which makes it quite efficient.


Need Your Help

How to get the last directory from a ftp url C#

c# url ftp directory

Is there a class or method in .NET 3.5 to get the last directory from a ftp Url?

Eloquent select statement based on a condition in another table

mysql laravel laravel-4

I have an laravel eloquent select statement which looks like this: