CS 354, version A
Spring 2007
Answer Key
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 = _____ /   6
Q2 = _____ /   8
Q3 = _____ / 10
Q4 = _____ /   8
Q5 = _____ /   5
Q6 = _____ /   8
Total = _____ / 45



Question 1 (6 points total)
Identify the correct order of the six steps of the instruction fetch and execute cycle by numbering them, starting with 1.

_3___ decode

_1___ fetch the instruction

_4___ load the operands

_6___ store the result

_5___ do the instruction's operation

_2___ update the PC

Question 2 (8 points total, 4 points for each part)
A C program is invoked with

  argc = 4
  argv[3] = "-quiet"
  argv[2] = "file16"
  argv[1] = "-update"
  argv[0] = "a.out"
PART A
What command line did the user type to invoke this program?

a.out -update file16 -quiet
PART B
What is the value of *( *(argv + 2) + 1 ) , and what is this value's type?

*(argv + 2) is a pointer to a character, as is argv[2]. Take this pointer and add 1, and it refers to the second character within the string that is argv[2]. Therefore, the value of this expression is 'i', and it is of type char.

Question 3 (10 points)
Write the output from an execution of this C program to the right of the source code.


#include <stdio.h>
#define MAX  4

int  addTwo(int x)
{
    int y;

    y = x + 2;
    x = x + 2;
    return (y);
}

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

   arp = ar;
   for (i=0; i<MAX; i++) {
      *arp = addTwo(a);
      a--;
      arp++;
   }

   for (i=0; i<MAX; i++) {
      printf("%d ", ar[i]);
   }
   printf("\n");

   arp = ar;
   printf("%d %d\n", a, *(ar + 3));

   return(0);
}


   22 21 20 19
   16 19

Question 4 (8 points, 4 points each)
Show your work (where possible) 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.


     11110010
   + 10111000
   -----------
     10101010 





     01111100
   - 00110110
   -----------


     11001001
     +      1
     --------
     11001010


     01111100
   + 11001010
   -----------
     01000110


Question 5 (5 points)
Show your work in doing the following 5-bit, unsigned multiplication. Give your 10-bit product in binary.


                    11011
                  * 11100
              -----------

                  1101100
                 11011000
              + 110110000
              -----------
               1011110100


Question 6 (8 points)
4 characters are packed into a single integer-sized variable called A. A code fragment is desired to exchange 2 of the characters, without changing the others. The leftmost byte is to be exchanged with the rightmost byte. Here is an example diagram of the desired result:

                -------------------------
 (original) A   | 'E' | 'F' | 'G' | 'H' |
                -------------------------

                -------------------------
 (resulting) A  | 'H' | 'F' | 'G' | 'E' |
                -------------------------
A set of variables (masks) are available for you to use:

Here are the allowable (invented) instructions to choose from:


  ror  X, Y    # right rotate of Y by 1 byte (1 character), placing the result into X
  lls  X, Y    # logical left shift of Y by 1 byte (1 character), placing the result into X
  and  X, Y, Z # bitwise logical AND of Y with Z, placing the result into X
  or   X, Y, Z # bitwise logical OR of Y with Z, placing the result into X
  copy X, Y    # copy the value within Y to X

Write a code fragment to do the exchange, using only the allowable instructions. For any intermediate results or temporary variables, use variables named T1, T2, T3, etc.

There are many variations of this code that all work equally well. Here is one of those possibilities.

copy  T1, A       # T1 is  E F G H
and   A, A, M1    # A is   0 F G 0
and   T2, T1, M3  # T2 is  E 0 0 0
ror   T2, T2
ror   T2, T2
ror   T2, T2      # T2 is now 0 0 0 E
and   T3, T1, M4  # T3 is 0 0 0 H
lls   T3, T3
lls   T3, T3
lls   T3, T3      # T3 is now H 0 0 0
or    A, A, T2
or    A, A, T3