CSC195, Class 5: More Behind the Scenes; An Introduction to C Overview * Three sample programs + "English" + A pseudocode notation + Revised * A short introduction to C + About + Historical + Vs. Java Administrivia * Eboard or Whiteboard? + Don't stop taking notes. * Thoughts on the course Web? * Extended office hours, now including MTuWF 9:00-9:50. * Questions on the homework? ---------------------------------------- How do you get to the innermost expression? * Easy answer: Recursively * In some more detail: If prop is NOT If first child is true or false, evaluate NOT Otherwise recurse on the first child If prop is some binary operation (equals, and, or, implis) If first child is neither true nor false, recurse on that child Otherwise, if second child is neither true nor false recurse on that child Otherwise, evaluate the binary operation Is there a built-in implies procedure? * No * Try (not (a and (not b))) * Equivalent to ((not a) or b) I hope. Test it. ---------------------------------------- Simple model of computing. Computer is: * Input * Output * CPU * Memory * Registers Just a few key operations * Input (typed), only to registers * Output (typed), only from registers * Arithmetic (typed), only on registers * Move between registers and memory locations * Jump to labeled instructions * Conditional jumps (e.g., jump-if-zero) to labeled instructions * Stop A few algorithms to try to write in this limited language * Read two numbers, print out their sum * Read numbers until you read 0, print out their sum * Read numbers until you read 0, print them out in reverse Read two numbers, print out their sum * Read an integer into register A * Read an integer into register B * Add register A to register B * Print register B Read three numbers, print out their sum, using only two registers (A and B) * Read an integer into register A * Read an integer into register B * Add register A to register B * Copy register B to register A * Read an integer into register B * Add register A to register B * Print register B Peter's alternative * Read an integer into register A * Read an integer into register B * Add register A to register B * Read an integer into register A * Add register A to register B * Print register B Read numbers until you read 0, print out their sum, using only two registers. * Design choice (PL): Maintain running sum in B. Read values into A. * Move the constant integer 0 into B * Label: START_OF_LOOP * Read an integer into A. * If A is 0, then Jump to End-of-Procedure * Add A to B * Jump to START_OF_LOOP * Label: End-Of-Procedure * Print B * Stop Input: 2 7 3 0 A: B: * Move the constant integer 0 into B Input: 2 7 3 0 A: B: 0 * Read an integer into A Input: 7 3 0 A: 2 B: 0 * If A is 0 jump to END * Add A to B Input: 7 3 0 A: 2 B: 2 * Jump to START_OF_LOOP * Read an integer into A Input: 3 0 A: 7 B: 2 * If A is 0 jump to END * Add A to B Input: 3 0 A: 7 B: 9 * Jump to START_OF_LOOP * Read an integer into A Input: 0 A: 3 B: 9 * If A is 0 jump to END * Add A to B Input: 0 A: 3 B: 12 & Jump to START_OF_LOOP * Read an integer into A Input: A: 0 B: 12 * If A is 0 jump to END * Print B 12 * Stop Okay ---------------------------------------- Problem: Read in three numbers, print them out in reverse order. Assume you only have two registers, A and B. * Idea: Move values into memory. + Put first value into memory location 0 + Put second value into memory location 1 + Put third value into memory location 2 * Last two steps may not be necessary * Read integer into A * Move A into memory location 0 * Read integer into A * Move A into memory location 1 * Read integer into A * Move A into memory location 2 * Move memory location 2 into A * Print A * Move memory location 1 into A * Print A * Move memory location 0 into A * Print A Idea: Use B as an index into memory * Put 0 into B * Label: INPUT_LOOP * Read integer into A * Put A into memory location B * Add 1 to B * If B < 3 JUMP INPUT_LOOP * Label: OUTPUT_LOOP * Subtract 1 from B * Read from memory location B into A * Print A * IF B = 0 JUMP END * JUMP OUTPUT_LOOP B is 2, print Mem[2] B is 1, print Mem[1] B is 0, print Mem[0] ---------------------------------------- Problem: Read until you reach 0, print in reverse order * Put 0 into B * Label: INPUT_LOOP * Read integer into A * If A = 0 jump OUTPUT_LOOP * Put A into memory location B * Add 1 to B * Jump INPUT_LOOP * Label: OUTPUT_LOOP * Subtract 1 from B * Read from memory location B into A * Print A * IF B = 0 JUMP END * JUMP OUTPUT_LOOP ---------------------------------------- How does C compare to Java? * Looks fairly similar but more restrictive * Similar + Control structures (other than method invokation): while, for, do, if, switch... + Primitives: +, -, ++, ... * Different or "More restrictive" + External variables (vs. classes) + Java programmers don't think of chars as ints. * Empty for loop for (;;) ; * Boolean vs. 0/nonzero, as in while(1) * Very different printing operations C permits formatted output Java permits ... * Variables must be declared at the start of a block * Different type system. * Real programmers don't need a safety net. * C lets you declare a fucntion (prototype) before you define it.