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 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
loopTop LDA count
endLoop BRA start
step DAT 1
|0 LDA 17
1 STA 16
3 BRZ 13
4 STA 15
5 LDA 16
6 ADD 14
7 STA 16
9 SUB 15
10 BRP 12
11 BRA 5
12 BRA 0
14 DAT 1
15 DAT 0
16 DAT 0
17 DAT 0
- Copy the 18 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 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.
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.
INPUT = 5
OUTPUT = 1, 2, 3, 4, 5