Not a premium member yet? Save 100's of hours on lesson prep with a comprehensive library of GCSE Computer Science resources - including worksheets, tests, and PowerPoint presentations.
Download the complete GCSE Computer Science bundle including all teacher resources and student revision:
- 200+ Computer Science/ICT resources
+ any we release in the future!
- Access to all student revision notes
- OCR, AQA, Edexcel and WJEC compatible
- New 9-1 GCSE spec
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