LMC Iteration Structures: While/Endwhile Loop

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 Introduction to programming (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 a WHILE/ENDWHILE iteration (loop) structure in a program.

The WHILE/ENDWHILE iteration structure tests the condition at the beginning of a loop (in this program, the loop continues while value1 is not zero). This means that if this test result is false (value1 is zero) at the beginning of the loop then the instructions inside the loop are never carried out.

Running the program: when prompted, INPUT a number, if the number is 0 then the program just ends. If any other number is entered then the program will OUTPUT the number 999 that number of times.

INP
STA value1
while LDA value1
BRZ endwhile
SUB count
STA value1

   LDA nineninenine
OUT

BRA while
endwhile HLT

value1 DAT
count DAT 1
nineninenine DAT 999
0 INP
1 STA 10
2 LDA 10
3 BRZ 9
4 SUB 11
5 STA 10

6 LDA 12
7 OUT

8 BRA 2
9 HLT

10 DAT 0
11 DAT 1
12 DAT 999

Instructions

  • Copy the 13 line program above and paste it into the Program box.
  • Click on the “Assemble Program” button.
  • LMC Iteration Structures: While/Endwhile Loop Image 1After the program is assembled you should see RAM addresses 0 to 9 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, count and nineninenine are used to label DAT instructions. DAT identifies the 11th, 12th and 13th instructions as data. The labels therefore refer to RAM addresses 10, 11 and 12 (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 10
 (labelled value1).
 2 while LDA value1 //the start of the while loop, the data stored in RAM
 address 10 (labelled value1) is loaded into the accumulator.
 3 BRZ endwhile //the check to exit the loop, branch to RAM address 9
 (labelled endwhile) if the accumulator contains zero.
 4 SUB count //subtract the contents of RAM address 11 (labelled count) from
 the contents of the accumulator and store the result in the
 accumulator.
 5 STA value1 //the accumulator contents are stored in RAM address 10
 (labelled value1).
 6 LDA nineninenine //the data stored in RAM address 12 (labelled nineninenine) is
 loaded into the accumulator.
 7 OUT //the value in the accumulator is sent to the OUTPUT.
 8 BRA while //the end of the while loop, branch to the memory location 2
 (labelled while).
 9 endwhile HLT //the program halts.

Example results

INPUT = 5
OUTPUT = 999, 999, 999, 999, 999

Further Readings: