Teach Any 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 Lessons →
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.
while LDA value1
count DAT 1
nineninenine DAT 999
1 STA 10
2 LDA 10
3 BRZ 9
4 SUB 11
5 STA 10
6 LDA 12
8 BRA 2
10 DAT 0
11 DAT 1
12 DAT 999
- Copy the 13 line program above and paste it into the Program box.
- Click on the “Assemble Program” button.
- After 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.
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.
INPUT = 5
OUTPUT = 999, 999, 999, 999, 999