Little Man Computer

Little Man Computer is a simplified implementation of a computer. It has only 100 memory locations, a single register (the 'accumulator') for adding and subtracting numbers and a program counter so it knows where in memory to read the next instruction from.

Downloads

There are several implementations of LMC, each with its own strengths:


LMC from York University, Canada

The implementation of LMC by S.Chen (referenced in the OCR GCSE Computing documentation) doesn't display all 3 digits of the memory locations in some browsers. In this screenshot mailboxes 0 and 1 contain '901' and '902' respectively, but you can only see the middle '0' which makes screenshots for reports difficult:


Riven LMC

This JAVA version has syntax colouring and highlights the current line, which is helpful in following a program's execution:

In this implementation there is no 'DAT' keyword, just a colon after the label:

loopstart: ADD 1

and to jump TO a label, put a colon before the :label

BRA :loopstart

The following code examples are converted from the samples on the Chen site.

Example 1: Input and Output

// No changes (no labels)
INP
OUT
HLT

Example 2: Using memory

// This is the original code for the Chen LMC:
INP
STA FIRST
INP
STA SECOND
LDA FIRST
OUT
LDA SECOND
OUT
HLT
FIRST DAT
SECOND DAT

// This is the amended code for the Riven LMC:
// Input 2 numbers then output them in the same order
INP
STA :FIRST
INP
STA :SECOND
LDA :FIRST
OUT
LDA :SECOND
OUT
HLT
FIRST:
SECOND:


Example 3: Adding and Substracting

INP
STA :FIRST
INP
ADD :FIRST
OUT
INP
SUB :FIRST
OUT
HLT
FIRST:

Example 4: Making Decisions

// Input two numbers, output the larger of the two
INP
STA :FIRST
INP
STA :SECOND
SUB :FIRST
BRP :SECONDBIG
LDA :FIRST
OUT
BRA :PROGRAMEND
SECONDBIG: LDA :SECOND
OUT 
PROGRAMEND: HLT
FIRST:
SECOND:

Example 5: Looping from 1 to 10

// Count from 1 to 10 (or from step to max)
LDA :step
STA :count
OUT
looptop: LDA :count
ADD :step
OUT
STA :count
SUB :max
BRP :endloop
BRA :looptop
endloop: HLT
step: 001
max: 010
count: 000

Example 6: Triangular Numbers

// Input: a number. Output: the term, if it's a triangular number, otherwise 0.
// Test: enter '6', should return '3' (6 is the 3rd triangular number)
INP
STA :value
LDA :zero
STA :trinum
STA :n
loop: LDA :trinum
SUB :value
BRP :endloop
LDA :n
ADD :one
STA :n
ADD :trinum
STA :trinum
BRA :loop
endloop: LDA :value
SUB :trinum
BRZ :equal
LDA :zero
OUT
BRA :done
equal: LDA :n
OUT
done: HLT
value: 000
trinum: 000
n: 000
zero: 000
one: 001

This version of LMC was written by Riven at Java-Gaming.org.


Dr Magnus Bordewich's LMC (Mac-only)

This version visualises the Little Man moving between the Program Counter, mailboxes and input/output boxes very clearly.

 

The compiler requires three-column entry, separated by tabs, with labels in the first column, mnemonics in the second and address labels in the third column:

In addition, some mnemonics differ slightly from other implementations: INP => IN, STA => STO, BRA => BR.

Download Dr Magnus' version here.

Example 6: Triangular Numbers

# If input is a triangular number, output its term
          IN
          STO       value
          LDA       zero
          STO       trinum
          STO       n
loop      LDA       trinum
          SUB       value
          BRP       endloop
          LDA       n
          ADD       one
          STO       n
          ADD       trinum
          STO       trinum
          BR        loop
endloop   LDA       value
          SUB       trinum
          BRZ       equal
          LDA       zero
          OUT
          BR        done
equal     LDA       n
          OUT
done      HLT
value     DAT       000
trinum    DAT       000
n         DAT       000
zero      DAT       000
one       DAT       001


Č
ċ
ď
Little Man Computer.jar
(110k)
John Kershaw,
8 Sep 2012 15:20
Comments