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 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.
if BRZ else
else LDA true
true DAT 1
false DAT 0
1 STA 11
3 SUB 11
4 BRZ 8
5 LDA 13
7 BRA 10
8 LDA 12
11 DAT 0
12 DAT 1
13 DAT 0
- Copy the 14 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 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.
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.
INPUT = 5, 5
OUTPUT = 1
INPUT = 5, 9
OUTPUT = 0