LMC Conditional Structures

KS3 Computer Science

11-14 Years Old

48 modules covering EVERY Computer Science topic needed for KS3 level.

GCSE Computer Science

14-16 Years Old

45 modules covering EVERY Computer Science topic needed for GCSE level.

A-Level Computer Science

16-18 Years Old

66 modules covering EVERY Computer Science topic needed for A-Level.

KS3 Programming Constructs (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 Data types, data structures and algorithms (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 show the use of branch instructions to create an IF/ELSE/ENDIF conditional structure in a program.

Running the program: when prompted, INPUT two numbers, if they are different then the program will OUTPUT a 0 (FALSE) and if they are the same then the program will OUTPUT a 1 (TRUE),

NOTE: The ‘if’ label has no function and is added to aid clarity.

INP
STA value1
INP
SUB value1
if BRZ else
   LDA false
OUT
BRA endif

else
LDA true
OUT

endif HLT
value1 DAT
true DAT 1
false DAT 0
0 INP
1 STA 11
2 INP
3 SUB 11
4 BRZ 8
5 LDA 13
6 OUT
7 BRA 10

8
LDA 12
9 OUT
10 HLT

11 DAT 0
12 DAT 1
13 DAT 0
LMC Conditional Structures Image 5

Instructions

  • Copy the 14 line program above and paste it into the Program box.
  • Click on the “Assemble Program” button.
  • LMC Conditional Structures Image 6After the program is assembled you should see RAM addresses 0 to 10 contain the machine code instructions shown in the image.
  • Click on the RUN button.
  • 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

value1, true and false are used to label DAT instructions. DAT identifies the 12th, 13th and 14th instructions as data. The labels therefore refer to RAM addresses 11, 12 and 13 (0-indexed counting).

0 INP //the first INPUT value is copied into the accumulator.
1 STA value1 //the accumulator contents are stored in RAM address 12 (labelled
 value1).
2 INP //the second INPUT value is copied into the accumulator.
3 SUB value1 //subtract the contents of RAM address 11 (labelled value1) from the 
 accumulator and store the result in the accumulator.
4 BRZ else //if the accumulator contains zero then the two numbers must be
 the equal so branch to the instruction at RAM address 8
 (labelled else)
5 LDA false //the contents of memory address 13 (labelled false) are loaded
 into the accumulator.
6 OUT //the value in the accumulator (0) is sent to the OUTPUT.
7 BRA endIf //branch to the end of the IF/THEN/ELSE program structure, memory 
 location 10 (labelled endIf). This makes the program jump past
 the 'else' part of the IF/THEN/ELSE program structure.
8 else LDA true //the contents of memory address 12 (labelled true) are loaded
 into the accumulator.
9 OUT //the value in the accumulator (1) is sent to the OUTPUT.
10 endIf HLT //the program halts.

Example results

INPUT = 5, 5
OUTPUT = 1
INPUT = 5, 9
OUTPUT = 0

Further Readings: