/************************************************************************** * class-bomb.c * * 11/19/08: * Original bomb-lab can be found at http://csapp.cs.cmu.edu/public/labs.html. * Their bomb-lab can only be used in our course if we adopt their book. * This class-bomb is inspired by the original bomb-lab but was mostly * written by Michelle Strout. The phrasing of the bomb was borrowed. ***************************************************************************/ #include #include void phase_1(); void phase_2(); void phase_3(); void phase_4(); void phase_5(); void phase_6(); void explode_bomb(); int main(int argc, char *argv[]) { printf("Welcome to my fiendish little bomb. You have 6 phases with\n"); printf("which to blow yourself up. Have a nice day!\n"); // understanding if statements phase_1(); printf("\nPhase 1 defused. How about the next one?\n"); // understanding while and do while loops phase_2(); printf("\nThat's number 2. Keep going!\n"); // understanding for loops phase_3(); printf("\nHalfway there!\n"); // understanding pointers and arrays phase_4(); printf("\nSo you got that one. Try this one.\n"); // understanding data structures in C phase_5(); printf("\nGood work! On to the next...\n"); // understanding C I/O phase_6(); printf("\nBomb defused!!!\n"); return 0; } // understanding if statements void phase_1() { int a=3, b, c; // example 1, show LC3 and MIPS assembly in class if (a > 0) { b = 7; } // example 2 if (b < c) { explode_bomb("phase_1 example 2"); } // example 3 if (a = -3) { explode_bomb("phase_1 example 3"); } // example 4 a = 21; if (a < 21) a = 40; explode_bomb("phase_1 example 4"); // example 5, show LC3 and MIPS assembly in class b = -1; if (b > 0) { a = 3; } else { a = 4; } // example 6 c = 3; if (c == 3) if (c < 3) b = 3; else explode_bomb("phase_1 example 6"); a = 3; } // understanding while and do while loops void phase_2() { int a=3, b=2, c=1; // example 1, show LC3 and MIPS assembly in class while (a > 0) { printf("%d", a); a--; } printf("\n"); // example 2, show LC3 and MIPS assembly in class a = -1; do { printf("%d", a); a--; } while (a > 0); // example 3 b = -3; c = 0; while ( b < 0 ) { c+=100; if (c < 0) { explode_bomb("phase_2 example 3"); } } printf("\n"); // example 4, show LC3 and MIPS assembly in class b = -4; c = 0; while ( b < 0 ) { c ++ ; printf("c = %d\n", c); if (c > 15) { break; } } } // understanding for loops void phase_3() { int a=1, b=2, c=3; // example 1, show LC3 and MIPS assembly in class for (a=0; a<4; a++) { printf("a = %d\n", a); } // example 2 for (b=0; b<=5; b++, a-=2) { b = a; if (b < 0) { explode_bomb("phase_3 example 2"); } } } // understanding pointers and arrays void phase_4() { } // understanding data structures in C void phase_5() { } // understanding C I/O void phase_6() { } void explode_bomb(char * mesg) { printf("\nBOOM!!!\nThe bomb has blown up in %s.\n", mesg); exit(1); }