CS 354, version A
Spring 2009
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 = _____ /   5
Q2 = _____ / 15
Q3 = _____ / 10
Q4 = _____ / 20
Total = _____ / 50
















Question 1 (5 points)
(Briefly) What does an assembler do?











Question 2 (15 points total, 5 points each part)

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

int main(int argc, char *argv[]) {
  int i;
  int userarg;  /* converted command line argument */

  userarg = atoi(argv[1]);

  printf("%d\n", userarg);

  printf("%c\n", *(argv[0] + 2));

  return 0;
}
Part A
This program compiles without warnings or errors, yet it has a programming error. (Briefly) Describe the programming error.





Part B
Assume that the program is invoked with the command line:
  argvtest 20 -run
Draw a diagram that describes argv and argc.









Part C
Assuming the same command line as in Part B, what is the output of the program's execution?



Question 3 (10 points total)
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.


     01101101
   + 00101001
   -----------








     10110100
   - 11111100
   -----------



 















Question 4 (20 points total)
Do 2 things:

  1. (12 points) Using the diagram next to the code for the program variables, complete the diagram to show the values of each of the variables as the program runs. Use decimal values for the integers and arrows for the pointers.
  2. (8 points) Write the output from an execution of this C program.

#include <stdio.h>

int main(int argc, char *argv[]) {
   int x;
   int y;
   int z;               --------       ---------
   int *px;          px |      |       |       |  x
   int *py;             --------       ---------
   int *pz;
                        --------       ---------
   px = &x;          py |      |       |       |  y
   py = &y;             --------       ---------
   pz = &z;
   x = -1;              --------       ---------
   y = 14;           pz |      |       |       |  z
   z = 20;              --------       ---------

   y = *px + 10;

   *pz = *px;

   *px = *py + z;

   py = pz;

   printf("x = %d, *px = %d\n", x, *px);
   printf("y = %d, *py = %d\n", y, *py);
   printf("z = %d, *pz = %d\n", z, *pz);

   return(0);
}