Lecture 16 - Control Flow II
Goals
- Learn the connection between condition codes and branching
- Learn the
comparecommand for setting condition codes - Learn how to implement a loop in assembly
Review
Reminder – when we left we had started to talk about control flow
In particular, we are trying to figure out how to handle code that looks like
if (x > y ){
x = x - y;
}
y = x;branch
Obviously with this build up, there is an instruction for doing this – it is called branch and it uses the mnemonic b
3 2 1 0
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
+---------------------------------------------------------------+
| cond |1 0 1 0| offset |
+---------------------------------------------------------------+
We are finally using the first four bits!
The first fours bits specify the condition under which we should branch
If the condition is false, then PC ← PC + 4
If the condition is true, then PC ← PC + 4 + sign-extend(offset)*4
Notice the use of sign_extend – it means we can jump forwards AND backward
Here are the condition codes for the first four bits (this is a partial list).
| code | mnemonic | meaning |
|---|---|---|
| 0000 | beq | equal |
| 0001 | bne | not-equal |
| 1010 | bge | greater-than-or-equal |
| 1011 | blt | less-than |
| 1100 | bgt | greater-than |
| 1101 | ble | less-than-or-equal |
| 1110 | b | always |
We finally have an explanation for the e at the start of our example instructions! The e says “always go to the next instruction”
Here is the big question – when what is equal to what? (or not-equal, or any of the others)
conditions
Again we have the problem that the instructions are already too packed for us to include things like register addresses
Our solution is to have a special register called the Current Program Status Register (CPSR). Among other things, the CPSR stores condition codes. The condition codes (or flags) can be set as the result of an ALU operation. The available flags include
- N - the result was negative
- Z - the result was zero
- C - the operation generated a carry out
- V - the operation generated a carry into bit 31
We do the operation before we reach the branch, setting the condition codes, and then the branch instruction just looks at the codes.
How do we set the condition codes? Now we can talk about the mysterious S in bit 20 of our machine code diagrams. If that is a 1, the condition codes are set by the operation, if it is 0, then they are not. In the assembly we would indicate that we want to set the codes by adding {S} to the instruction: add{S} R1, R2, R3
compare
While we can set the codes with our data processing operations, it is more common to use another special instruction: compare, which uses the mnemonic cmp
The immediate version:
3 2 1 0
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
+---------------------------------------------------------------+
| |0 0 1 1 0| | Rn | | imm12 |
+---------------------------------------------------------------+
and the register version
3 2 1 0
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
+---------------------------------------------------------------+
| |0 0 1 1 0| | Rn | | Rm |
+---------------------------------------------------------------+
These compare their two operands, but you will notice that they do not specify a destination because they just set the condition codes. But what is the comparison actually doing? Subtraction. So, for example, if the two value are the same, which condition codes get set?
This sets the Z flag, so beq will branch when Z is 1 and bne will branch when Z’
examples
Let’s take a simple example.
if (x > y ){
x = x - y;
}
y = x;We might translate that to
cmp r0, r1
ble #1
sub r0,r0,r1
mov r1, r0Note that the conditional was actually translated to two instructions
What about an else statement?
if (x > y){
x = x - y;
} else{
x = x + y;
}
y = x;This might be translated to
cmp r0, r1
ble #2
sub r0, r0, r1
b #1
add r0,r0,r1
mov r1, r0
Loops
Let’s think about some more complex control flow
x = 0
while (y > 0){
x = x + z
y = y - 1
}How might we turn this into assembly?
mov r0 #0
cmp r1, #0
ble #3
add r0, r0, r2
sub r1, r1, #1
b #-5
Whoa – the only difference between a while loop and an if statement is a single unconditional jump!
It will actually be more common for us to reverse the order a little
mov r0 #0
b #2
add r0, r0, r2
sub r1, r1, #1
cmp r1, #0
ble #-4
Why might we do that? we reduce the length of the loop by an instruction
If we take the initial branch off, we get a different kind of loop that you may not have encountered before: the do-while loop
x = 0
do {
x = x + z
y = y - 1
} while (y > 0)Obviously this isn’t the best choice for this loop
Mechanical level
vocabulary
Skills