LMC Addition and Subtraction

KS3 Programming Resources (14-16 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

A-Level Functions and procedures (16-18 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

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

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.

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

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

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)