Getting started with the Little Man Computer (post #01)

The Little Man Computer is a simple way of showing how a real computer works underneath.

We can imagine a little man running around inside the computer fetching instructions, and faithfully executing them.  

The little man uses the following instructions, which can be either coded as mnemonics in a program, or posted directly into the computer's 'brain'. The computer memory is represented as one hundred boxes numbered 0-99.

Instruction                    Mnemonic          Code
ADD                            ADD               1..
SUBTRACT                       SUB               2..
STORE                          STA               3.. 
LOAD                           LDA               5..
INPUT                          INP               901
OUTPUT                         OUT               902
BRANCH ALWAYS                  BRA               6.. 
BRANCH IF ZERO                 BRZ               7..
BRANCH IF ZERO OR POSITIVE     BRP               8.. 

For example, the Code 399 means 'STORE the current value in box 99.  We could write this as "STA 99"

Example program one.
Get a number from the human and store it in the computer memory (for later).

... a simple LMC program
inp

sta 99

The first line is a comment and is ignored by the little man.  It is there to help us understand our program.
The second line is a request for input.
The third line is our STORE in address 99

Here is a screenshot:
Your program after you compile it and press "execute".  You should see the LMC has converted our mnemonics into two codes in adresses 00 and 01.  The value we entered is put into address 99.

In fact we could store our value at any point in the computer memory.  Try the following program:

Example program two.
Get a number from the human and store it in the computer memory (for later).

... a simple LMC program two
inp

sta 54


Here we store the human's input at address 54.

What is happening?
Here, the mnemonic "INP" has been translated to the command "901".  When the little man reads this, he knows that it means, "get input from the human".  Here I typed the number '4', and the value of 4 is placed into the accumulator. The mnemonic instruction "sta 54" is translated to "354" and the little man understands that this means, "write the value in the accumulator into address 54".

Example program number three.

Let the little man decide where to put the human's input.

...example of variables
inp
sta first
inp

sta second


Here the little man has decided that it is best to store the first value in address 99 and the second in 98.
What is happening?
In this program there are two inputs.  The little man also receives the instruction "sta first", which means "store the value in the accumulator into the next available address and label it 'first'".
The instruction "sta second" means "store the value in the accumulator into the next available address and label it second."

Next time.  Adding numbers together.