LMC Branching – Instructions To Implement Decisions

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.

GCSE Basic 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 Basic programming constructs (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 use of the three branch instructions: BRZ, BRP and BRA.

Running the program: INPUT a number and the program will OUTPUT a sequence of numbers, starting at 1 and finishing with the INPUT number. The program will continue until a 0 is entered.

start LDA zero
STA count
INP
BRZ finish
STA maxNumber
loopTop LDA count
ADD step
STA count
OUT
SUB maxNumber
BRP endLoop
BRA loopTop
endLoop BRA start
finish HLT
step DAT 1
maxNumber DAT
count DAT
zero DAT
0 LDA 17
1 STA 16
2 INP
3 BRZ 13
4 STA 15
5 LDA 16
6 ADD 14
7 STA 16
8 OUT
9 SUB 15
10 BRP 12
11 BRA 5
12 BRA 0
13 HLT
14 DAT 1
15 DAT 0
16 DAT 0
17 DAT 0
branching

Instructions

  • Copy the 18 line program above and paste it into the Program box.
  • Click on the “Assemble Program” button.
  • Ram AddressesAfter the program is assembled you should see RAM addresses 0 to 13 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

step, maxNumber, count and zero are used to label DAT instructions. DAT identifies the 15th, 16th, 17th and 18th instructions as data. The labels therefore refer to RAM addresses 14, 15, 16 and 17 (0-indexed counting).

0 start LDA step //load the contents of memory address 17 (labelled zero) 
 into the accumulator.
1 STA count //store the accumulator contents in RAM address 16
 (labelled count).
2 INP //INPUT a number and store in the accumulator.
3 BRZ finish //if the accumulator contains zero then branch to the
 instruction at memory location 13 (labelled finish)
4 STA maxNumber //store the accumulator contents in RAM address 15 
 (labelled maxNumber).
5 loopTop LDA count //load the contents of memory address 16 (labelled count)
 into the accumulator. This instruction is the start
 of the counting loop.
6 ADD step //add the contents of RAM address 14 (labelled step, value = 1)
 to the accumulator and store the result in the accumulator.
7 STA count //store the accumulator contents in RAM address 16 (labelled 
 count).
8 OUT //OUTPUT the value in the accumulator.
9 SUB maxNumber //subtract the contents of RAM address 15 (labelled maxNumber)
 from the acccumulator and store the results in the 
 accumulator.
10 BRP endLoop //if the accumulator contains zero or a positive number then
 count has reached maxNumber so branch to the instruction at
 memory location 12 (labelled endLoop).
11 BRA loopTop //branch to the instruction at memory location 5 (labelled
 loopTop) to continue the counting loop.
12 endLoop BRA start //branch to the instruction at memory location 0 (labelled
 start) to zero the counter and INPUT another number.
13 finish HLT //halt the program.

Example results

INPUT = 5
OUTPUT = 1, 2, 3, 4, 5

Further Readings: