This web page contains schemes for translating C control structures into assembly language. These schemes are general descriptions involving abstract branch descriptions with the following form:
branch to label if condition is true/false
The exact form of this branch depends on the machine and the complexity of
the condition.
For example, the last two sections of this web page deal with ANDed
conditions and ORed conditions.
The italics in label and condition mean that they change for each application of the schemes.
if (condition) {
statements
}
branch to next if condition is false
code for statements
next:
if (condition) {
then_statements
} else {
else_statements
}
branch to else if condition is false
code for then_statements
unconditional branch to next
else:
code for else_statements
next:
while (condition) {
statements
}
unconditional branch to condition_test
loop_top:
code for statements
condition_test:
branch to loop_top if condition is true
next:
loop_top:
branch to next if condition is false
code for statements
unconditional branch to loop_top
next:
Although the alternate form seems more natural to many programmers, the first form is preferred because it is more efficient. It also is more like the implementation of a do-while loop.
do {
statements
} while (condition);
loop_top:
code for statements
condition_test:
branch to loop_top if condition is true
next:
The label condition_test is only needed if one of the statements in statements is a continue statement.
for (initialization_statements; condition; increment_statements) {
statements
}
code for initialization_statements
unconditional branch to condition_test
loop_top:
code for statements
increment:
code for increment_statements
condition_test:
branch to loop_top if condition is true
next:
The label increment is only needed if one of the statements in statements is a continue statement.
A break statement can occur inside a C loop or switch statement. It is implemented as a unconditional branch to a label immediately following the loop or switch statement. This label is named next in the loop and switch assembler code.
In assembly language, a conditional branch can be used to implement a conditional break statement. This has no direct equivalent in C.
A continue statement can occur inside a C loop. In while and do-while loops, it is implemented as a unconditional branch to a label for the condition test of the while or do-while statement. This label is named condition_test in the and do-while assembler code and the preferred while loop assembler code, and loop_top in the alternate while loop assembler code.
In a for loop, a continue statement is implemented as a unconditional branch to a label immediately preceding the code to implement the increment_statements. This label is named increment in the for loop assembler code.
In assembly language, a conditional branch can be used to implement a conditional continue statement. This has no direct equivalent in C.
switch (expression) {
case val_1:
case_1_statements
case val_2:
case_2_statements
.
.
.
case val_n:
case_n_statements
default:
default_statements
}
code to evaluate expression and save its value in exp
branch to case_1 if exp equals val_1
branch to case_2 if exp equals val_2
.
.
.
branch to case_n if exp equals val_n
unconditional branch to default, or next if no default clause
case_1:
code for case_1_statements
case_2:
code for case_2_statements
.
.
.
case_n:
code for case_n_statements
default:
code for default_statements
next:
Normally, C switch statements have a break statement at the end of each case. This is implemented with an unconditional branch to the next label.
branch to somewhere if condition1 AND condition2
branch to next if condition1 is false
branch to next if condition2 is false
branch to somewhere
next:
branch to next if condition1 is false
branch to somewhere if condition2 is true
next:
Both of these constructions generalize to any number of subconditions. In the simplified assembler code for ANDed subconditions, only the last subcondition can be merged with the branch to somewhere.
branch to somewhere if condition1 OR condition2
branch to somewhere if condition1 is true
branch to somewhere if condition2 is true
This constructions generalizes to any number of subconditions.