CS270 Colorado State University ==================== C Control Flow ==================== These notes cover C control flow, how C control-flow constructs map to LC3 and MIPS, and examples of such control flow in the class-bomb.c code. ---------------- If statements if (condition) { // statements } coding style I recommend that you ALWAYS put curly braces, no matter how many statements you have within the "if". Indent the statements. In this class with spaces. Be consistent where you put your curly braces. This is often dictated in more strict coding styles. -------------------------------------- Example 1 in phase_1() in class-bomb.c int a=3, b, c; if (a > 0) { b = 7; } LC3 code for "if statement" in Example 1 in phase_1() What are the addresses for variables a, b, and c? MIPS code for Example 1 in phase_1() What are the addresses for variables a, b, and c? ------------------------------------- bomb? To compile class-bomb.c and try this out for yourself, do the following: (1) grab class-bomb.c from class schedule page (2) compile class-bomb.c % gcc -g class-bomb.c (3) execute it to explode bomb % ./a.out (4) use code inspection and gdb to defuse the bomb % gdb a.out Using code inspection, figure out how to diffuse bomb in examples 2, 3, and 4 in phase_1. -------------------------------------- Example 5 in phase_1() in class-bomb.c b = -1; if (b > 0) { a = 3; } else { a = 4; } LC3 code for "if-else statement" in Example 5 in phase_1() MIPS code for Example 5 in phase_1() -------------------------------------- Example 6 in phase_1() in class-bomb.c ---------------- while and do-while statements while (condition) { // statements } The statements are executed in a loop as long as condition is valid. do { // statements } while (condition); The statements are always executed at least once and then in a loop as long as condition is satisfied. break -- terminate loop continue -- terminate current iteration of loop and start next iteration of loop -------------------------------------- Example 1 in phase_2() in class-bomb.c int a=3, b=2, c=1; // example 1 while (a > 0) { printf("%d", a); a--; } LC3 code for "while statement" in Example 1 in phase_2() What are the addresses for variables a, b, and c? MIPS code for "while statement" in Example 1 in phase_2() What are the addresses for variables a, b, and c? -------------------------------------- Example 2 in phase_2() in class-bomb.c a = -1; do { printf("%d", a); a--; } while (a > 0); LC3 code for "do-while statement" in Example 2 in phase_2() MIPS code for "do-while statement" in Example 2 in phase_2() -------------------------------------- Example 4 in phase_2() in class-bomb.c b = -4; c = 0; while ( b < 0 ) { c ++ ; if (c > 15) { break; } } LC3 code for "while statement" in Example 4 in phase_2() MIPS code for "while statement" in Example 4 in phase_2() ---------------- for loop statements for (init; condition; reinit ) { // statements } The statements are executed in a loop as long as the condition is valid. After each iteration of the loop, the reinit is executed. -------------------------------------- Example 1 in phase_3() in class-bomb.c for (a=0; a<4; a++) { printf("a = %d\n", a); } LC3 code for "for statement" in Example 1 in phase_2() MIPS code for "for statement" in Example 1 in phase_2() ------------------------ mstrout@cs.colostate.edu, 11/19/08