COMPUTERS REALLY ARE STUPID

Here is an example to show that computers just blindly follow instructions.

Three employees work 20, 40, and 50 hours, respectively, one week. Their hourly rates of pay are 6, 10, and 9 dollars, respectively. Calculate their gross wages, adjusting for time-and-a-half for hours worked over 40 per week.

The computer's RAM (Random Access Memory) contains the following values at the given address.

The computer's CPU (Central Processing Unit, microprocessor) has two registers, R and I, containing 0 and 1, respectively. The CPU also has a register called PC (Program Counter) that it uses to keep track of which step it is currently executing.

The computer executes the following steps.

  1. Load into register R the contents of the memory location whose address is given in register I.
  2. Subtract from the value in register R the contents of the memory location at address 0.
  3. If the value in register R is less than or equal to 0, then skip the next three steps (that is, go to step 7).
  4. Multiply the value in register R by the contents of the memory location at address 10.
  5. Multiply the value in register R by the contents of the memory location whose address is given by adding 3 to the contents of register I.
  6. Store the value in register R into the memory location whose address is given by adding 6 to the contents of register I.
  7. Load into register R the contents of the memory location whose address is given in register I.
  8. Multiply the value in register R by the contents of the memory location whose address is given by adding 3 to the contents of register I.
  9. Add to the value in register R the contents of the memory location whose address is given by adding 6 to the contents of register I.
  10. Store the value in register R into the memory location whose address is given by adding 6 to the contents of register I.
  11. Increment by 1 the value in register I.
  12. If the value in register I is greater than 3, then HALT (the program is done).
  13. Go back to step 1.

See if you can reproduce on scratch paper on your own what we did in class.

For the curious, the following C++ program performs the same calculations as the machine language steps above.

#include <iostream>
using namespace std;

int main() {
  const double WEEK = 40.0;       // 40 hours is a standard work week.
  const double OVERTIME = 0.5;    // Extra pay for working overtime.
  const int EMPLOYEES = 3;
  double hoursWorked[EMPLOYEES] = { 20.0, 40.0, 50.0 };
  double payRate[EMPLOYEES] = { 6.00, 10.00, 9.00 };
  double payCheck[EMPLOYEES];
  for (int i = 0; i < EMPLOYEES; i++) {
    payCheck[i] = 0;
    if (hoursWorked[i] - WEEK > 0) {
      double extraOvertime = (hoursWorked[i] - WEEK) * OVERTIME;
      payCheck[i] = payCheck[i] + extraOvertime * payRate[i];
    }
    payCheck[i] = payCheck[i] + hoursWorked[i] * payRate[i];
  }
  cout << payCheck[0] << " " << payCheck[1] << " " << payCheck[2] << endl;
  return 0;
}


home page: http://elvis.rowan.edu/~hartley/index.html
e-mail: hartley@elvis.rowan.edu