Translating C Control Structures


This web page contains schemes for translating the following C control structures into assembly language.

If statements

C code
if (condition) {
    statements
}

Assembler code
    branch to next if condition is false
    code for statements
next:

If-else statements

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:

While loops

C code
while (condition) {
    statements
}

Assembler code
loop_top:
    branch to next if condition is false
    code for statements
    unconditional branch to loop_top
next:

Alternate assembler code
    unconditional branch to condition_test
loop_top:
    code for statements
condition_test:
    branch to loop_top if condition is true
next:

Do-while loops

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 label condition_test is only needed if one of the statements in statements is a continue statement.

For loops

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.

Break statements

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.

Continue statements

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.

Switch statements

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.


Page URL: http://www.d.umn.edu/~gshute/spimsal/control.html
Page Author: Gary Shute
Last Modified: Sunday, 12-Feb-2012 06:19:45 CST
Comments to: gshute@d.umn.edu