CS 354, version A
Fall 2007
Name:___________________
ID:___________________
Exam 1
No electronic devices may be used while taking this exam. Examples of devices not allowed are calculators, pagers, cell phones, wrist calculators/computers, laptop computers, pocket computers. Each student is allowed one 8.5 by 11 inch sheet of paper with handwritten notes. The notes may be on both sides of the paper.

Show all work, and do any/all calculations on the exam. Extra scratch paper may not be used.

Exam Score
Q1 = _____ / 12
Q2 = _____ /   6
Q3 = _____ / 10
Q4 = _____ /   8
Q5 = _____ /   8
Total = _____ / 44



Question 1 (12 points total)
List the six steps of the instruction fetch and execute cycle in their given (and correct) order.

  1. _____________________________
  2. _____________________________
  3. _____________________________
  4. _____________________________
  5. _____________________________
  6. _____________________________

Question 2 (6 points)

/*   program expects a command line of
       a.out 20 100                         */

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int lower, upper;      /* command line integers expected */

    lower = atoi(argv[1]);
    upper = atoi(argv[2]);

    if(argc != 3) {
        printf("program exiting due to error\n");
        exit(1);
    }
}
In a single sentence, describe the programmming error in this C program.





Question 3 (10 points)
Write the output from an execution of this C program to the right of the source code.
Note: If your answer is wrong, but you show your work (with diagrams), you may get partial credit.


#include <stdio.h>
#define MAX  4

void  fcn(int *x) {
    *x = (*x) * 3;
}

int main() {
   int ar[MAX];
   int *arp;
   int i;      /* loop induction variable */

   for (i=0; i < MAX; i++) {
      ar[i] = i + 2;
   }

   arp = ar;
   for (i=0; i < MAX; i++) {
      fcn(arp + i);
   }

   printf("%d %d\n", ar[1], *(arp + 3));

   return(0);
}

Question 4 (8 points, 4 points each)
Show your work in binary in doing the following 8-bit, two's complement integer operations. Give your answer in binary. For any integer arithmetic operation that causes overflow, write the word OVERFLOW next to the computed answer.


     11111010
   + 01111001
   -----------







     01111100
   - 10110110
   -----------





Question 5 (8 points)
We want to implement code that does a rotate right by 3 bit positions of a 32-bit variable called A, but without using a rotate right instruction. We only have logical left and right shifts, logical (bitwise) and, and logical (bitwise) or.

Assume all variable and immediate values are 32 bits. Here are the allowable (invented) instructions:

Fill in the blanks within the code fragment such that it does the rotate right by 3 bits, using only the given instructions.
   lls   B,   A,  ____

   lrs   ___, A,  ____

   ____  A,   A,  B