Flow Control
This section will include info on for loop, if statements, and if else statements.
For LC-3, conditionals and comparison are typically done using BR
Or branch which will check 3 flags, n, z and p
- n: Negative
- z: Zero
- p: Positive, numbers > 0
These flags can be set by instructions like...
- ADD, AND, NOT, LD, LDR, LDI, LEA, not ST
The flags are set automatically, i.e if you execute ADD, R3 < R1 + R0 and the value of 3 is stored inside of R3 then the p flag will be set to 1 and the z and n flags will b set to 0.
For Loops
To have perform a for loop, you have to keep track of a number, increment or decrement it, and for each iteration of the loop check to see if it has reached your desired number of iterations yet by subtract your desired iteration number from it.
For example, let's look at a for loop in Java
int i;
int r1 = 0;
for(i = 1; i <= 10; i++) {
// example of doing something here
r1 = r1 + 5;
}
As you can see, for this for loop it will start at i = 1
and do 10 iterations for 1-10.
Now, for LC-3 you have to manually add a line of code for each part inside the parenthesis for the loop as well as adding a line to jump back to the top of loop and another line to exit the loop once the condition is false.
Example of the for loop in LC-3 binary
0011 0000 0000 0000 ; starting address of 0x3000
0001 010 010 1 01010 ; ADD, R2 <- R2 + 10, Add R2(value is 0) + immediate value of 10 to equal 10
1001 010 010 111111 ; NOT, R2 <- R2, NOTing R2 to begin Two's complement to convert 10 to -10
0001 010 010 1 00001 ; ADD, R2 <- R2 + 1, Add + 1 to R2 to complete Two's complement
; BEGIN For loop
0001 101 000 0 00 010 ; ADD, R5 <- R0 + R2, check if R0 - 10 == 0
0000 010 000 000 011 ; BRz, Branch on Zero flag, Branch if R0 - R2 = 0, Offset = +4(1 + 3),
0001 000 000 1 00001 ; ADD R0 <- R0 + 1, Increment our R0(i counter) by 1.
0001 001 001 1 00101 ; ADD, R1 <- R1 + 5, Random code to "do stuff"
0000 111 111 111 011 ; BRnzp, Branch to top of for loop unconditionally, offset = -4(-5 + 1)
; END for loop
1111 0000 0010 0101 ; halt end program, HALT
For the version with more detailed comments:
0011 0000 0000 0000 ; starting address of 0x3000
; R0 and R1 are both x0000 which is equal to 0 by default
; So no initialization is needed.
; Initializing r2 to -10
; We need -10 because we will exit the for loop once i = 10.
; To check if i = 10 we check if i - 10 = 0 which will set the z flag(remember z flag means 0)
; which we allow the branch to break out of the loop
0001 010 010 1 01010 ; ADD, R2 <- R2 + 10, Add R2(value is 0) + immediate value of 10 to equal 10
1001 010 010 111111 ; NOT, R2 <- R2, NOTing R2 to begin Two's complement to convert 10 to -10
0001 010 010 1 00001 ; ADD, R2 <- R2 + 1, Add + 1 to R2 to complete Two's complement
; So now R2 = -10
; BEGIN For loop
; Conditional check to see if R0 == 10 by checking if R0 - 10 == 0
; Remember R2 == -10 right now so we're actually doing R0 + (-10)
; If R0 == 10 then R0 + R2 should equal 0, else it will equal a negative number
0001 101 000 0 00 010 ; ADD, R5 <- R0 + R2
0000 010 000 000 011 ; BRz, Branch on Zero flag, Branch if R0 - R2 = 0, Offset = +3,
0001 000 000 1 00001 ; ADD R0 <- R0 + 1, Increment our R0(i counter) by 1.
; This is just a placeholder code so we're actually doing something in the loop
0001 001 001 1 00101 ; ADD, R1 <- R1 + 5
0000 111 111 111 011 ; BRnzp, Branch to top of for loop unconditionally, i.e. branch no matter what
; This branch will branch -5 addresses, so it will go UP to the beginning of the loop again
; Remember: The offset is 1 + Offset so it's 1 + -5 = -4 instructions
; END for loop
1111 0000 0010 0101 ; halt end program, HALT
; SO in closing
; R0 = equivalent to int i. This register will increment by 1 until it hits 10
; And when it hits 10 the branch should break out of the for loop.
; R1 = A register we are using to increment by 5 each time as our "do stuff" code
; Otherwise our for loop would be doing nothing but iterating 10 times.
; This register will become 50 by the end of the for loop.
; R2 = -10, This register just calculates then holds -10 for the entire program.
; The -10 is used so we can calculate R0 + R2(Which holds -10) each loop until R0 + R2 == 0
; which will set the z flag and allow the Branch instruction to exit the loop
; R3 = Unused
; R4 = Unused
; R5 = Random register we're storing the R0 + R2 calculation in because the ADD instruction
; Needs a destination register.