Branching on Zero A Worked Example
Branching on Zero Countdown from 99 to 1, then stop Cell Code Description 00LDA Load from … 0110 … cell 10 02OUT Output 03SUB Subtract … 0411 … cell 11 from the accumulator 05BRZ Test the accumulator; if zero, … 0609 …branch to cell 09 07BRBranch to… 0802 …cell 02 09HLTStop Processing 1099Data: Starting Value 1101Data: Counting down in 1’s
Trace the Program Use a program trace sheet like this to help you understand what is happening
Starting Position
00 LDA Load the contents of RAM Cell 10 into the Accumulator
02 OUT Output the contents of the Accumulator
03 SUB Subtract the contents of cell 11 from the Accumulator
05 BRZ Test the contents of the Accumulator. If the answer is zero, branch to RAM Cell 09 Test the contents of the Accumulator. If the answer is zero, branch to RAM Cell 09 Accumulator = 98 Do nothing. Accumulator = 98 Do nothing.
05 BR Branch to cell RAM Cell 02
02 OUT Output the contents of the Accumulator
03 SUB Subtract the contents of cell 11 from the Accumulator
05 BRZ Test the contents of the Accumulator. If the answer is zero, branch to RAM Cell 09 Test the contents of the Accumulator. If the answer is zero, branch to RAM Cell 09 Accumulator = 97 Do nothing. Accumulator = 97 Do nothing.
05 BR Branch to cell RAM Cell 02
02 OUT Output the contents of the Accumulator
The program continues.... Now... continue the program for yourself for another two ‘jumps’ back to cell 02
... to here This is the point you should now have reached.
... and further continues... This is the point you should now have reached. To save a lot of tedious work, we are going to miss out a lot of iterations and go to where the numbers reach down towards zero
... to here. We have now reached the point where the last number output was 2
03 SUB Subtract the contents of cell 11 from the Accumulator
05 BRZ Test the contents of the Accumulator. If the answer is zero, branch to RAM Cell 09 Test the contents of the Accumulator. If the answer is zero, branch to RAM Cell 09 Accumulator = 1 Do nothing. Accumulator = 1 Do nothing.
05 BR Branch to cell RAM Cell 02
02 OUT Output the contents of the Accumulator
03 SUB Subtract the contents of cell 11 from the Accumulator
05 BRZ Test the contents of the Accumulator. If the answer is zero, branch to RAM Cell 09 Test the contents of the Accumulator. If the answer is zero, branch to RAM Cell 09 Accumulator = 0 Branch to Cell 09 Accumulator = 0 Branch to Cell 09
09 HLT Stop the Program
Program Flowchart Store 99 in the Accumulator Subtract 1 from the Accumulator Is the answer zero? Start Stop Output the contents of the Accumulator Yes No