You can run this program on any LMC emulator, such as http://peterhigginson.co.uk/LMC/
LMC, which stands for Little Man Computer is a model of a computer, used to teach students how CPUs work. Read More.
- Takes a number as an input, and stores it in a mailbox labelled
N
. We will not attempt to find any number in the sequence larger than this number.
- We start the loop process by loading in the value from mailbox
A
. We've labelled this mailboxLOOP
as this is the start of the loop for generator the sequence. - We then enter in to the logic for deciding if we have reached our limit to the sequence. In a language with complex conditionals, we would use something like
if a > n
, but LMC doesn't have that. Instead, we reverse it so we doif a - n > 0
, which we can do in LMC usingBRP
. - We have loaded
A
into the accumulator at the start of the loop, and so we simply need to take away our sequence maximum,N
. We do this on line 4. - We know that if the accumulator is a negative number, then we have reached the end of the sequence that we want to generate and so we can skip to the end of the program, which we have labelled as
ENDLOOP
. We do this withBRP
, which will branch to a mailbox if the accumulator value is a negative number.
- This is simply loading
A
back into the accumulator and outputting it, so we can see each item in the sequence.
- This is the logic for how we generate a fibonacci sequence. It's the equivalent to something like
a, b = b, a + b
. Unfortunately, we need the mailboxACC
as I'm not sure that there is an easy way to swap the values in mailboxes in LMC.
- This jumps back to the start of the loop, where we'll check if the sequence is done.
- This is the end of the program, labelled
ENDLOOP
so we can end the loop once the sequence is done.
- This is just labelling a few memory boxes.
- We're using a swap memory box,
ACC
, which we may or may not need. It should probably be calledSWP
anyway. - The sequence starts on 0, whereas a real fibonacci sequence starts on 1.
My entire class benefited from this.