LMC Addition and Subtraction

Teach any GCSE Computer Science class

We have put together a full GCSE Computer Science curriculum that will give you all the teaching materials you need to teach any topic.

Whether you’re a brand new Computer Science teacher, or you’ve been teaching ICT for years, our resources will save you hours and hours of lesson preparation every single week.

View the resources →

The following program will demonstrate the ADD and SUB instructions of the LMC instruction set.

INP
STA first
INP
ADD first
OUT
INP
SUB first
OUT
HLT
first DAT 0 INP
1 STA 9
2 INP
3 ADD 9
4 OUT
5 INP
6 SUB 9
7 OUT
8 HLT
9 DAT 0

Running the program: when prompted, INPUT a set of three numbers. The program should add the first two numbers and output the answer, then subtract the first number from the third and output the answer.

Instructions

  1. Copy the ten line program above and paste it into the Program box.
  2. Click on the “Assemble Program” button.
  3. Ram AddressesAfter the program is assembled you should see RAM addresses 0 to 8 contain the machine code instructions shown in the image.
  4. Click on the RUN button.
  5. 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

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.

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

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)