Branch Instruction
The LC-3 provides a single instruction used to change program flow based on the result of a previous instruction
Normal program flow is to execute each instruction in-order until the last instruction is complete. While this good enough to solve simple problems, more complex problems require code to be skipped or repeated
Common Flow Control Constructs
If - Based on a logical test (if(x > 12)
), execute or skip a block is instructions
If/Else - Based on a logical test, execute or skip a block is instructions
For() - Based on a logical test, repeat a block is instructions
Logical Conditional Test
Condition Code (CC) Register
The LC-3 sets the Condition Code (cc) Register as part of the execution of the following instructions:
- ADD
- AND
- NOT
- LD
This register can be used by future instructions that might decide to change the program's flow (like an If/Else -or- Loop behavior)
The CC will be in one of three possible states:
N - The last operation resulted in a Negative Value
Z - The last operation resulted in a Zero Value
P - The last operation resulted in a Positive Value
Other instructions do not change the CC, so it will holding last value until another of the above 4 instructions is executed
.ORIG x3000
AND R0, R0, #0; CC set to Z
ADD R1, R0, #2; CC set to P
LD R2, MyData; CC set to N
Done HALT
;End of Program
;Data Declarations-------------
MyData .FILL #-2
.END
Labels
If needed, a label is places before the instruction. It is, in effect, labeling the location os that instruction for latter use. They are any text the programmer chooses, as long as it is not a keyword or numeric vale
Labels in assembly source code are the method of denoting a particular location in the program that may be branched to from some other location in the program
Labels are human-readable text, named to help the programmer understand what the label is used for. During the assembly progress, the LC-3 Edit will convert labels to a PCOffset values needed for the BR instruction
.ORIG x3000
MyLabel AND R0, R0, #0; Instruction associated with MyLabel
MyOtherLabel
ADD R1, R0, #2; Instruction associated with MyOtherLabel
Done HALT
;End of Program
;Data Declarations-------------
.END
Whitespace in Labels
Labels can be on the same line -or- a separate line, before the instruction. This is a matter of style. The extra whitespace of the sends option makes the code a bit more readable for humans. The assembler, however does not care, and removed the separation
Line 2 has an added label on the same line as the instruction
Line 3 has an added label on a line before the instruction
BR
Branches use both Labels and the CC to cause an executing program to change from the normal program flow
The BR instruction includes references used to compare to the CC as the logical test
BRn label - Branch (change program flow to) to label if CC is Negative
BRp label - Branch (change program flow to) to label if CC is Positive
BRz label - Branch (change program flow to) to label if CC is Zero
References can be combined to test two (2) or all three (3) references
BRnz label - Branch (change program flow to) to label if CC is Negative or Zero
BRzp label - Branch (change program flow to) to label if CC is Zero or Positive
BRnzp label - Branch (change program flow to) to label if CC is Negative, Positive, or Zero
Order is Important
When using multiple references with BR they must be in the nzp order. If the order is different an assembler error will occur
.ORIG x3000
LD R0, MyData
BRz JumpHereIfZero
BRp JumpHereIfPositive
BRn JumpHereIfNegative
JumpHereIfZero
NOP; Do something if R0 contains Zero
BRnzp Done; Skip everything else and halt
JumpHereIfPositive
NOP; Do something if R0 is Positive
BRnzp Done; Skip everything else and halt
JumpHereIfNegative
NOP; Do something if R0 is Negative
; No need to 'BRnzp Done' here since
; normal program flow ends the program
Done HALT
;End of Program
;Data Declarations-------------
MyData .FILL xF00D
.END
- Lines 3 - 5 are reacting to the CC set by line 2
- Program flow will branch to line 7, 11, or 15 based on the CC
- After
JumpHereIfZero
orJumpHereIfPositive
execute their block of instructions, there is am unconditional branch (jump regardless of the CC value) (Lines 9 & 13) to the Done label. This prevents the normal program flow from executing other instruction blocks - As noted in the comment in lines 17 & 18, an unconditional branch is not needed here because normal program flow causes the program to fall through to Done.