Little Man Computer

Computers - it's all about logic!

lmc-introduction

 

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

The Von Neumann architecture, illustrated in the following image, consists of five major components with machine equivalent of division of labour.
They are,
  1. Input-Output Unit
  2. Control Unit
  3. Logic Unit
  4. Memory
  5. 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.

 

von-neumann architecture

 

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:

lmc-introduction

 

Main Parts

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

  1. The window for typing in the code
  2. The two buttons - to load the code into memory and then run
  3. The window for an input, if any - not necessary
  4. An indicator that shows the progress of the code - step by step
  5. Memory locations where instructions and data are stored, as specified in von Neumann architecture - 100 cells, from 00 to 99.
  6. The window for the output/s during the execution of the code
  7. 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 CodeNumeric CodeInstruction
INP901Input data
ADD1XXAdd data
SUB2XXSubtract data
STA3XXStore data
LDA5XXLoad data
BRA6XXBranch to specified cell
BRZ7XXIf 0, branch to a specified cell
BRP8XXIf 0 or positive, branch to a specified cell
OUT902Output data
HLTHLTBreak execution
DATTreat 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:

INP
STA 20
OUT
HLT

The following animation shows how it works:

lmc-introduction

 

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

The following animation illustrates it:

lmc-data-input

 

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:

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:

lmc-data-addition

 

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

lmc-data-addition

 

 

Subtracting a Number

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

INP
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:

lmc-data-subtraction

 

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:

INP
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:

lmc-data-decision

 

BRZ 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:

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

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

lmc-data-iteration

 

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 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

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

lmc-data-multiplication

 

Division

As there is no operation to carry out division in the LMC, this is how subtraction is used to do 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

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

lmc-data-division

 

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:

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

The output is as follows:

lmc-squaring a number

 

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.

 

lmc-powers-of-two

 

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
brp loop
end lda num-two
out
sub num-two
sta num-two
hlt
num-two dat
power dat
one dat 1

 

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:

INP
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:

lmc-data-fibonacci sequence

 

Bit Shift

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

<script;>
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:

Enter y here:     

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

 

INP
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:

lmc-data-bit shift

 

 

 

 

 

 

 

Resources at Fingertips

There is a significant selection of tutorials here, covering ASP.Net, HTML5, CSS3 and JavaScript. They are categorized clearly for you to access them easily on any device - desktops/laptops, smartphones and laptops.
There are quite a few of them on HTML5 Canvas and CSS3. In addition, there are tutorials on JavaScript and ASP.Net too.

Email: 

Stand Out - from the crowd

students

"There's no such thing as a free lunch."

The best things in nature are free with no strings attached - fresh air, breathtakingly warm sunshine, scene of meadow on the horizon...

Vivax Solutions, while mimicking nature, offers a huge set of tutorials along with interactive tools for free.

Please use them and excel in the sphere of science education.

Everything is free; not even registration is required.

 

 

Recommended Reading

 

The best book to master HTML5 canvas animations. The author tells you how to manipulate the canvas element with JavaScript in a progressive way - starting from the easier to harder worked examples. It is ideal for anyone who wants to be a game developer in HTML5.

Advertisements

Advertisement