You are on page 1of 8

Little Man Computer (LMC)

A-level Computing
Independent Study Project
Tasks 1-3
The Little Man Computer (LMC) is a simulator which
models the basic features of a modern computer. It
features a central processing unit (CPU) consisting of
an arithmetic logic unit (ALU) and registers, a control
unit containing an instruction register and program counter, input and output mechanisms and memory (RAM) to
store both data and instructions.
CPU (Central Processing Unit)
The LMC is based on the concept of a little man shut inside a computer
acting like the control unit of a CPU, i.e. fetching instructions from memory,
decoding and executing the instructions, and managing the input and
output. This is the part that executes the
program and tells the other
The LMC can be programmed using assembly language which is then
components what they need to
converted (‘assembled’) into machine code (appearing in denary rather
do. the main function of the CPU
than binary). Each assembly code instruction is converted into a 3 digit
is to fetch and execute the
machine code instruction (1 digit for the instruction and 2 for the memory
instructions.
address). The 3 digit machine code instructions are then loaded into RAM,
starting at memory address 0. Any data is also loaded into memory at a ALU (Arithmetic/Logic Unit) &
particular memory address. The 'Little Man' can then begin execution, (RAM) memory
starting with the instruction in RAM at memory address 0.
The ALU is the part of the
computer that performs all the
math and logic operations. The
First program
memory keeps track of
To get a feel for it straight away, load the following set of instructions to information so that it can be
demonstrate how the simulator works. recalled later.

Click on 'Submit' to load the instructions followed by 'RUN' to execute the code (or ‘STEP’ to run it line by line).

The LMC performs the following steps to execute a program:

 Check the Program Counter so it knows the memory address to look at.
 Fetch the instruction from the memory address that matches the program counter.
 Increment the Program Counter (so that it contains the memory address of the next instruction).
 Decode the instruction (includes finding the memory address for any data it refers to).
 If required by the instruction code, fetch the data from the memory address found in the previous step.
 Execute the instruction and if necessary set the Program Counter to match any branch instructions.

What did the program do? What is the purpose of each of the commands?
The LMC Instruction Set

The Little Man Computer has a limited instruction set containing the following commands:

Machine
Instruction Mnemonic Further information
Code

Load the contents of address xx onto the accumulator.


Load LDA 5xx
Note: the contents of the address are not changed.

Store the contents of the accumulator to address xx.


Store STA 3xx
Note: the contents of the accumulator arenot changed.

Add the contents of address xx to the accumulator.


Add ADD 1xx Note: the contents of the address are notchanged and the total
cannot exceed 999.

Subtract the contents address xx from the accumulator.


Note: the contents of the address are notchanged.
Subtract SUB 2xx
Note: If a subtract instruction causes negative results then a
negative flag will be set so that BRP can be used properly.

Input INP 901 Copy the value from the "in box" onto the accumulator.

Copy the value from the accumulator to the "out box".


Output OUT 902
Note: the contents of the accumulator arenot changed.

End HLT 000 Stop the LMC simulator executing the program.

Branch always BRA 6xx Set the program counter to address xx.

If the contents of the accumulator are ZERO, set the program


Branch if zero BRZ 7xx
counter to address xx.

Branch if zero If the contents of the accumulator are ZEROor positive (i.e. the
or positive
BRP 8xx
negative flag is not set), set the program counter to address xx.

Reserve as data the memory address reached when this


instruction is compiled.
Data storage DAT Note: a value N can be stored at the memory address by using
DAT N

NOTE: xx represents a memory address between 0 and 99.


TASK ONE
Create a 30-second presentation to explain the basic concepts and features of the Little Man Computer (LMC).
Expect that this presentation will be delivered to your Computing teachers.

TASK TWO
Work through the following 4 sections that take you through the basic uses of the LMC. These will help you to gain a
good enough understanding of how to program the LMC for other purposes.

For each section:

 Make your own notes using whichever style and level of detail is appropriate for your understanding now
and for future revision.
 Clearly list any program code that you use (useful for reference back to later)
 Leave some space to develop these notes as you progress through other work with the LMC

Task 2a - Input and Output


The following program will demonstrate the INPUT and OUTPUT instructions of the LMC.

Use this program which simply INPUTS a number, then OUTPUTS it.

INP
OUT
HLT

Instructions:

1. Copy this three-line program into the program box and click Submit
2. Click on the "Assemble into RAM" button.
3. After the program is assembled you should see RAM addresses 0 to 2 contain the machine code instructions
shown in the image.

4. Click on the RUN button.


5. When prompted, INPUT a number.
6. You can also use STEP instead of RUN so you can follow each step.

Explanation:

 INP //the input value is copied into the accumulator.

 OUT //the value in the accumulator is sent to the OUTPUT.

 HLT //the program halts.

Example results:

 INPUT = 2

 OUTPUT = 2
Task 2b - Using labels with the LMC Simulator
Labels are used to label a memory address. This makes it much easier to refer to memory addresses which
hold data or instructions.

 data1 DAT

o Explanation: The memory address where this data is stored would be labelled data1. No data would
initially be stored at this location.

 data1 DAT 50

o Explanation: The memory address where this data is stored would be labelled data1 and store the
data 50.

 STA data1

o Explanation: The contents of the accumulator would be stored at the memory address allocated for
this label

 LDA data1

o Explanation: The data which is stored at the memory address for this label would be loaded into the
accumulator.

The memory address used for the label will correspond to whichever line of code the statement is on, e.g. if the
command “data1 15” is written as the 4th line of code then the memory address 03 will be given the label “data1”
and the number 15 stored at that address.

It is usual for these statements to be written at the end of the program code. This keeps their memory addresses out
of the way of those used for the main program code.

Example 1: loading a value that has been stored using the 4th line of code

LDA data1 00 LDA 03


OUT 01 OUT
HLT 02 HLT
data1 DAT 15 03 DAT 15

Example 2: inputting a value, storing it at address 05, loading it back again and outputting it.

INP 00 INP
STA data1 01 STA 05
LDA data1 02 LDA 05
OUT 03 OUT
HLT 04 HLT
data1 DAT 05 DAT 00
Task 2c - Load and Save

INP 0 INP
The following program will help to further demonstrate the use STA first 1 STA 9
of LDA and STA, the Load and Save instructions used in the LMC instruction INP 2 INP
set. STA second 3 STA 10
Running the program: when prompted, INPUT a set of two numbers. The LDA first 4 LDA 9
program should then OUTPUT them in the same order that they were entered. OUT 5 OUT
LDA second 6 LDA 10
OUT 7 OUT
Instructions: HLT 8 HLT
first DAT 9 DAT 0
1. Type in the program on the right second DAT 10 DAT 0
2. After the program is assembled you should see RAM addresses 0 to 8
contain the machine code instructions shown in the image.

3. Click on the RUN button.

4. If you have difficulty following what is happening, read the explanation below and use STEP instead of RUN
so you can follow each step.

Explanation:

first and second are used to label a DAT instruction. DAT identifies the 10th and 11th instructions as data. The labels
therefore refer to RAM addresses 9 and 10 respectively (0-indexed counting).

 0 INP //the first input value is copied into the accumulator.

 1 STA first //the accumulator contents are stored in RAM address 9 (labelled
first).

 2 INP //the second input value is copied into the accumulator.

 3 STA second //the accumulator contents are stored in RAM address 10 (labelled
second).

 4 LDA first //the contents of memory address 9 (labelled first) are loaded into
the accumulator.

 5 OUT //the value in the accumulator is sent to the OUTPUT.

 6 LDA second //the contents of memory address 10 (labelled second) are loaded into
the accumulator.

 7 OUT //the value in the accumulator is sent to the OUTPUT.

 8 HLT //the program halts.

Example results:

 INPUT= 2, 3

 OUTPUT = 2, 3
Task 2d - Addition and subtraction
The following program will demonstrate the ADD and SUB instructions of the INP 0 INP
LMC instruction set. STA first 1 STA 9
INP 2 INP
Running the program: when prompted, INPUT a set of three numbers. The
ADD first 3 ADD 9
program should add the first two numbers and output the answer,
OUT 4 OUT
then subtract the first number from the third and output the answer.
INP 5 INP
Instructions: SUB first 6 SUB 9
OUT 7 OUT
1. Type in the program on the right HLT 8 HLT
2. After the program is assembled you should see RAM addresses 0 to 8 first DAT 9 DAT 0
contain the machine code instructions shown in the image.

3. Click on the RUN button.

4. If you have difficulty following what is happening, read the explanation below and use STEP instead of RUN
so you can follow each step.

Explanation:

first is used to label a DAT instruction. DAT identifies the 10th instruction as data. The label therefore refers to RAM
address 9 (0-indexed counting).

 0 INP //the first input value is loaded into the accumulator.

 1 STA first //the accumulator contents are stored in RAM address 9 (labelled
first).

 2 INP //the second input value is loaded into the accumulator.

 3 ADD first //add the contents of RAM address 9 (labelled first) to the
accumulator and store the result in the accumulator.

 4 OUT //the value in the accumulator is sent to the OUTPUT.

 5 INP //the third input value is loaded into the accumulator.

 6 SUB first //subtract the contents of RAM address 9 (labelled first) from the
accumulator and store the result in the accumulator.

 7 OUT //the value in the accumulator is sent to the OUTPUT.

 8 HLT //the program halts.

Example results:

 INPUT= 2 (stored in RAM address 9)

 INPUT = 3

 OUTPUT = 5 (the contents of the accumulator PLUS the contents of RAM address 9)

 INPUT = 8

 OUTPUT = 6 (the contents of the accumulator MINUS the contents of RAM address 9)
TASK THREE
Work through writing programs for these problems on the LMC. They can be attempted in any order but they are
arranged in order of challenge.

Use your book for each program:

 Give a full and clear title to explain what the program does
 Log any design ideas (flowcharts etc.)
 Write out your final code
 List any relevant outputs.
 Describe any issues, especially any that have not yet been fully resolved

Programs:

1. Write a program to add 3 numbers

2. Get two input values, a and b, and output the difference, a – b

3. Ask the user for one number, then double it

4. Write a program that will prompt for 2 numbers, subtract the first from the second and output the
answer, then subtract the second from the first and output the answer.

5. Ask the user for three numbers and then repeat them in reverse order.

6. Write a program that correctly calculates x + y + z – a – b

You might also like