This web page contains schemes for translating various C control structures into assembly language. It also describes how to implement compound conditions formed by ORing or ANDing simpler conditions.

C code
if (condition) {
    statements
}
          
Assembler code
    branch to next if condition is false
    code for statements
next:
          
C code
if (condition) {
    then_statements
} else {
    else_statements
}
          
Assembler code
    branch to else if condition is false
    code for then_statements
    unconditional branch to next
else:
    code for else_statements
next:
          
C code
while (condition) {
    statements
}
          
Preferred assembler code
    unconditional branch to condition_test
loop_top:
    code for statements
condition_test:
    branch to loop_top if condition is true
next:
          
Alternate assembler code
loop_top:
    branch to next if condition is false
    code for statements
    unconditional branch to loop_top
next:
          

Although the alternate assembler code may seem more natural, it executes two branches for each iteration of the loop. The preferred code executes only one branch per iteration.

C code
do {
    statements
} while (condition);
          
Assembler code
loop_top:
    code for statements
condition_test:
    branch to loop_top if condition is true
next:
          

The assembler code is just the preferred assembler code for a while loop with the unconditional branch omitted.

The label condition_test is only needed if one of the statements in statements is a continue statement.

C code
for (initialization; condition; increment) {
    statements
}
          
Assembler code
    code for initialization
    unconditional branch to condition_test
loop_top:
    code for statements
increment:
    code for increment
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. For 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 loop_top in the while loop assembler code, and condition_test in the and do-while assembler code and the while loop alternate assembler code.

For 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.

C code
switch (expression) {
case val_1:
    case_1_statements
case val_2:
    case_2_statements
.
.
.
case val_n:
    case_n_statements
default:
    default_statements
}
          
Assembler code
    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 default clause is omitted)
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.

Compound condition
    branch to somewhere if condition1 OR condition2
          
Assembler code
    branch to somewhere if condition1 is true
    branch to somewhere if condition2 is true
          

This construction generalize to any number of subconditions.

Compound condition
    branch to somewhere if condition1 AND condition2
          
Assembler code
    branch to next if condition1 is false
    branch to next if condition2 is false
    branch to somewhere
next:
          
Simplified assembler code
    branch to next if condition1 is false
    branch to somewhere if condition2 is true
next:
          

This construction generalize to any number of subconditions. In the simplified assembler code, only the last subcondition can be merged with the branch to somewhere.