### Little Man Computer

Computers - it's all about logic!

The arrangement of a massive collection of logic gates in miniature form evolved into a computer that we are familiar with today.

**Little Man Computer - LMC -** is a simulator that mimics the modern computer architecture, known as **von Neumann architecture.** It was a brainchild of Dr Stuart Madnick, invented in 1965; Since it can model the modern computer, it is still widely used as a teaching tool.

**Von Neumann Architecture**

They are,

- Input-Output Unit
- Control Unit
- Logic Unit
- Memory
- BUS

**Input-Output Unit**

The unit allows a user interact with the computer while providing it with an input in anticipation of an output.

**Control Unit**

This unit deals with the handling of instructions, processing of the data, storing it in memory and reading it back from it at the right moment.

**Logic Unit**

This unit is responsible for carrying out basic mathematical operation such as addition, subtraction etc.

**Memory**

This section stores data and the instructions to deal with data.

**BUS**

This is the machine equivalent of umbilical chord - the connector of the other four parts to the

**motherboard.**

There is no better simulator than the **Little Man Computer** to understand the working of the **Von Neumann Architecture. The purpose of the tutorial is just that.**

This tutorial is based on the excellent LMC simulator provided by Peter Higginson, which can be used here. I take this opportunity to show my immense appreciation for the work done by **Mr Peter Higginson** - in providing the world with this wonderful simulation and the effort made in the task.

The LMC simulator takes the following form:

#### Main Parts

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 |

**XX**is the cell number in the memory compartment.

#### The basic manipulation of the LMC

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

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.

STA A

OUT

HLT

A DAT

The following animation illustrates it:

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

#### Adding Two Numbers

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

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.

`Human Anatomy in a Cube - highly recommended`

If you want to learn the important facts about human anatomy in a short period time - with fun - here is an amazing invention. It's an interactive cube with with clear labels; you can learn many systems in a matter of seconds. It's unique, elegant and above all, first of its kind.

`The Solar System in a Cube - highly recommended`

If you want to learn the important facts about our solar system, especially the planets, in a short period time - with fun - here is an amazing invention. It's an interactive cube with with important facts in a nut-shell; you can learn a lot in a matter of seconds. It's unique, elegant and above all, first of its kind.

This is how these cubes work:

#### Subtracting a Number

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

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.

#### Decision Making

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:

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.

#### Iteration - countdown

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:

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:

#### Multiplication

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:

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:

#### Division

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:

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:

#### Squaring a Number

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:

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:

#### Powers of 2

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:

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

`Recommended Books`

If you are learning computer science at GCSE, here is a set of books for you: they are revision guides, yet they cover every single topic, while giving ample information to grasp the concepts in an innovative way; the books show a clear path to follow, something that the bulky text books fail to do; they are good even for a sixth former.

#### Fibonacci Sequence

The Little Man Computer can be used to generate the Fibonacci Sequence: you can input the initial two numbers and the number of iterations in that order.

This is the code for it:

STA x

INP

STA y

INP

STA lmt

LDA x

OUT

LDA y

OUT

loop LDA lmt

BRZ end

SUB one

STA lmt

LDA x

ADD y

STA z

OUT

LDA y

STA x

LDA z

STA y

BRA loop

end LDA z

SUB z

HLT

x DAT

y DAT

z DAT

lmt DAT

one DAT 1

This is the animation:

#### Bit Shift

Let's look at the following code snippet written in JavaScript:

var x = 14;

var y = 2;

x <<= y;

document.write(x);

</script>

This is called **bit shift** in programming: the **binary value** of **14 - 1110 -** is shifted to the **left** by **2 - 111000**, which is **56.**

Let's change the value of **y** and see how **x** changes by bit shift. Just put y in the text box and click the button:

**y**here:

The same can be achieved by the LMC. This is how it is done:

- Enter the number as first input, say 14, for instance.
- Enter the base - 2, for instance.
- Enter the bit shift you want - say, 1, 2 or 3.

STA number

INP

STA base

INP

STA shift

LDA shift

SUB one

STA shift

loopshift LDA shift

BRZ loopmain

SUB one

STA shift

LDA base

ADD base

STA base

BRA loopshift

loopmain LDA base

BRZ end

SUB one

STA base

LDA number

ADD ans

STA ans

BRA loopmain

end LDA ans

OUT

SUB ans

STA ans

HLT

ans DAT

base DAT

shift DAT

one DAT 1

number DAT

This is the animation of bit shift:

Now that you have read this tutorial, you will find the following tutorials very helpful too: