The LMC simulator takes the following form:

These are main components in the window that are easily recognizable:

- The window for typing in the code
- The two buttons - to load the code into memory and then run
- The window for an input, if any - not necessary
- An indicator that shows the progress of the code - step by step
- Memory locations where instructions and data are stored, as specified in von Neumann architecture - 100 cells, from 00 to 99.
- The window for the output/s during the execution of the code
- Options for controlling the flow of the execution - slow to fast, etc

The best way to learn the LMC is running set of codes, from the simplest to the more advanced gradually, rather than making an effort to understand the simulator fully at first. This is the approach adopted in this tutorial.

Before that, however, you have to be familiar with the set of instructions: there are not many; just 11 of them. They are as follows:

Mnemonic Code | Numeric Code | Instruction |
---|---|---|

INP | 901 | Input data |

ADD | 1XX | Add data |

SUB | 2XX | Subtract data |

STA | 3XX | Store data |

LDA | 5XX | Load data |

BRA | 6XX | Branch to specified cell |

BRZ | 7XX | If 0, branch to a specified cell |

BRP | 8XX | If 0 or positive, branch to a specified cell |

OUT | 902 | Output data |

HLT | HLT | Break execution |

DAT | Treat content as data |

First of all, type in the following code in the **LMC** as shown in the animation in order to see how it works:

INP

STA 20

OUT

HLT

STA 20

OUT

HLT

The following animation shows how it works:

Please note that not only is the input - 45 - displayed in output window, but also in the memory location 20.

The same can be achieved the following way too - specifying a variable - A in this case - without specifying a particular memory cell.

INP

STA A

OUT

HLT

A DAT

STA A

OUT

HLT

A DAT

The following animation illustrates it:

Please note that the value is stored in the fifth memory cell - 045.

Here is the code for adding two numbers and displaying the sum:

INP

STA A

INP

STA B

LDA A

ADD B

OUT

HLT

A DAT

B DAT

The following animation shows how the two numbers are taken in as two inputs and later answer is given out:

The same can be achieved without providing a user input: the numbers to be added are stored in two variables, instead - A and B.

Here is the code for subtracting the smaller number from the bigger one and then displaying the difference:

INP

STA A

INP

STA B

LDA A

SUB B

OUT

HLT

A DAT

B DAT

STA A

INP

STA B

LDA A

SUB B

OUT

HLT

A DAT

B DAT

The following animation shows how the the smaller number is taken away from the bigger:

The same operation can be carried out exactly like addition; the two numbers can be stored in two different variables, rather than inputting them.

The LMC can be used to model decision making as well. For instance, a number can be taken away from a second number and the output can be sent along a chosen path.

In order to perform operations of this kind, the limited number of **loops** must be used. They are:

**BRA** - branch always.

**BRZ** - branch if the outcome is 0.

**BRP** - branch if the outcome is positive or zero.

Now, let's use the LMC to determine the bigger of two numbers by using the above loops; the output must be the bigger number regardless of its order of input. This is the code:

INP

STA A

INP

STA B

LDA A

SUB B

BRP isPositive

LDA B

OUT

HLT

isPositive LDA A

HLT

A DAT

B DAT

STA A

INP

STA B

LDA A

SUB B

BRP isPositive

LDA B

OUT

HLT

isPositive LDA A

HLT

A DAT

B DAT

The following animation shows how the output always shows the bigger of the two numbers, regardless of the order they are put in:

**BRP** plays the major role here: it branches out the execution to a sub-route, defined as **isPositive.**, if **A-B** turns out to be positive. Otherwise, the execution continues along the main route.

The LMC can be used to create a countdown - a form of iteration, with the aid of **BRZ** and **BRA** loops. The user provides the programme with an input to generate a countdown. This is the code:

INP

STA A

LOOP LDA A

OUT

SUB ONE

STA A

BRZ ENDTHIS

BRA LOOP

ENDTHIS LDA A

SUB A

OUT

HLT

A DAT

ONE DAT 1

STA A

LOOP LDA A

OUT

SUB ONE

STA A

BRZ ENDTHIS

BRA LOOP

ENDTHIS LDA A

SUB A

OUT

HLT

A DAT

ONE DAT 1

The following animation shows the iteration that leads to a countdown, based on the user input:

The LMC can add or subtract numbers, but it can neither multiply nor divide. The obvious drawback actually makes programming even more interesting: the multiplication, in fact, can be carried out with the aid of addition!

Here is the trick:

2 x 3 = 6

So, 2 + 2 + 2 = 6

3 x 4 = 12

So, 3 + 3 + 3 + 3 = 12

So, with the aid of a loop we keep adding the **first** number as many as **second** number of times!

Here is the code for the two inputs:

INP

STA FIRST

INP

STA SECOND

LOOP LDA SECOND

BRZ ENDTHIS

SUB ONE

STA SECOND

LDA ANS

ADD FIRST

STA ANS

BRA LOOP

ENDTHIS LDA ANS

OUT

SUB ANS

STA ANS

HLT

FIRST DAT

SECOND DAT

ONE DAT 1

ANS DAT 0

STA FIRST

INP

STA SECOND

LOOP LDA SECOND

BRZ ENDTHIS

SUB ONE

STA SECOND

LDA ANS

ADD FIRST

STA ANS

BRA LOOP

ENDTHIS LDA ANS

OUT

SUB ANS

STA ANS

HLT

FIRST DAT

SECOND DAT

ONE DAT 1

ANS DAT 0

The following animation shows how two numbers, entered by a user, can be multiplied to produce the product:

As there is no operation to carry out division in the LMC either, this is how subtraction is used to do the division of a number by another:

12 :- 3 = 12 - 3 - 3 - 3- 3 => 4 steps => answer = 4

9 : 3 = 9 - 3 - 3 - 3 => 3 steps => answer = 3

You keep subtracting the **divisor - 3 - ** from the **dividend - 12 or 9 - ** until the answer becomes zero in a loop! In the meantime, a variable must be included in the same loop in such a way that its value goes up by one with every subtraction of the divisor from the divided. Drawing a flow chart will also be helpful. Here is the code:

INP M

STA M

INP N

STA N

LOOP LDA M

BRZ END

SUB N

STA M

LDA ANS

ADD ONE

STA ANS

BRA LOOP

END LDA ANS

OUT

SUB ANS

STA ANS

HLT

M DAT

N DAT

ANS DAT 0

ONE DAT 1

STA M

INP N

STA N

LOOP LDA M

BRZ END

SUB N

STA M

LDA ANS

ADD ONE

STA ANS

BRA LOOP

END LDA ANS

OUT

SUB ANS

STA ANS

HLT

M DAT

N DAT

ANS DAT 0

ONE DAT 1

The dividend and divisor are given as two inputs by the user and the quotient - answer - is given as output:

The Little Man Computer can be used to square a number. The procedure is an extension of the process of multiplication.

This is the code for squaring a number:

INP

STA x

LDA x

STA y

LOOP LDA y

BRZ END

SUB ONE

STA y

LDA ANS

ADD x

STA ANS

BRA LOOP

END LDA ANS

OUT

SUB ANS

STA ANS

HLT

x DAT

y DAT

ONE DAT 1

ANS DAT 0

STA x

LDA x

STA y

LOOP LDA y

BRZ END

SUB ONE

STA y

LDA ANS

ADD x

STA ANS

BRA LOOP

END LDA ANS

OUT

SUB ANS

STA ANS

HLT

x DAT

y DAT

ONE DAT 1

ANS DAT 0

The output is as follows:

This code raise the power of 2 to until it produces a three-digit output; it works **only** for an index that is greater than **0**.

- The first input sholud be number 2.
- The second input should be the power you want 2 to be raised to, provided that the final answer remains a two-digit number.

The code for the above programme is as follows:

INP

STA num-two

INP

STA power

SUB one

STA power

LOOP LDA power

BRZ end

SUB one

STA power

LDA num-two

ADD num-two

STA num-two

PRP loop

end LDA num-two

OUT

SUB num-two

STA num-two

HLT

num-two DAT

power DAT

one DAT 1

STA num-two

INP

STA power

SUB one

STA power

LOOP LDA power

BRZ end

SUB one

STA power

LDA num-two

ADD num-two

STA num-two

PRP loop

end LDA num-two

OUT

SUB num-two

STA num-two

HLT

num-two DAT

power DAT

one DAT 1