- Branching allows a program to take two different paths depending on a
condition.
- From a high-level language standpoint, branching is used for if statements
and loops.
- Intel uses the cmp instruction to compare to entities. It actually performs a subtraction to determine the difference.
- Using condition codes that indicate if the difference is positive, negative, or zero, the program can jump to another location
- Because there are no braces to group statements, assembler uses labels to indicate the destination of a jump
- Here are the more common branch commands:
jmp branch always
jl branch on less than zero
jle branch on less than or equal to zero
je branch on equal to zero
jne branch on not equal to zero
jge branch on greater than or equal to
jg branch on greater than
- Let's practice with an example. Consider translating the following into
assembler:
z = 20;
if (x == y) {
z = 44;
}
x = 77;
- And here is a possible translation that accomplishes the same thing:
mov dword [z], 20 ; z = 20
mov eax, [x]
cmp eax, [y] ; Note: cmp won't allow memory-to-memory comparisons
jne after
mov dword [z], 44 ; z = 44
after:
mov dword [x], 77 ; x = 77
- With a little more branching with labels, we can handle then blocks as well as else blocks,
so this code
// code before this snippet
if (x > y) {
z = 55;
w = -22
} else {
z = 77;
w = 606;
}
// code after this snippet
- becomes this code
// code before this snippet
mov eax, [x]
cmp eax, [y] ; compare x and y
jle else ; decide if we need to skip over then block
mov dword [z], 55 ; z = 55
mov dword [w], -22 ; w = -22
jmp after ; at this point we should skip over else block
else:
mov dword [z], 77 ; z = 77
mov dword [w], 606 ; w = 606
after:
// code after this snippet
- With a then block and an else block, we need two places to branch: one to
skip the then block and one to skip the else block.
- The idea is that, if the conditions of the jump are not met, execution "falls through" to the next statement.
- Practice#2: Now try translating the following [solution]:
x = 55;
y = 11;
z = 43;
if (z + x + y <= 101) {
print "then block";
w = 16;
} else {
print "else block";
w = 9;
}
print w;
- Practice#3: try translating a nested example (assign x, y, z some values) [solution]:
// find the largest of the three and set w to it
// assume no two are equal
if (x > y) {
if (x > z) {
w = x;
} else {
w = z;
}
} else {
if (y > z) {
w = y;
} else {
w = z;
}
}
print w;
- Practice#4: one more nested example [solution]:
x = 10;
z = -3;
y = (z * x) - 4;
if (x + 3 >= z) {
if (z != 5) {
y = 77;
x = x + 2;
} else {
y = y / 2;
}
z = z - 2;
} else {
x = x - 29;
}
z = x + 1;
- A while loop like this
x = 20;
while (x < 40) {
z--;
x++;
}
print z;
translates into this
mov dword [x], 20
beginwhile:
cmp dword [x], 40 ; compare x and 40
jge endwhile ; if x greater than or equal to 40, then jump to endwhile label
sub dword [z], 1
add dword [x], 1
jmp beginwhile ; if we got here, then let's try the condition again
endwhile:
; print z
- A do loop like this
x = 20;
do {
z--;
x++;
} while (x < 40);
print z;
translates into this
mov dword [x], 20
begindo:
sub dword [z], 1
add dword [x], 1
cmp dword [x], 40
jl begindo
; print z
- And for loops can really be translated into while loops.