CSCI 162 practice midterm 2 [48 marks total]

The midterm is closed book, closed notes, no calculators or other electronics permitted. A reference sheet is included with the midterm, covering key Marie opcodes and boolean logic identities.


Part 1: data representation [16 marks]

  1. For the C function below, use the example mult(11, 5) to explain why the function works for positive integers at a binary level. (Hint: first show the values of m and n in binary for each pass through the loop, *then* try to explain why it works.)

    long mult(long m, long n)
    {
       long result = 0;
       while (m > 0) {
          if (m & 1) result += n;
          m = m >> 1;
          n = n << 1;
       }
       return result;
    }
    

  2. Explain why the function does not work for two's complement negative integers, and outline a fix that would allow it to work for negatives.


Part 2: digital logic [16 marks]

    Suppose you are given the truth table below, where - indicates don't-care values.
    w x y z   f  
    0 0 0 0 -
    0 0 0 1 1
    0 0 1 0 0
    0 0 1 1 0
    0 1 0 0 0
    0 1 0 1 1
    0 1 1 0 0
    0 1 1 1 -
    1 0 0 0 -
    1 0 0 1 0
    1 0 1 0 0
    1 0 1 1 1
    1 1 0 0 0
    1 1 0 1 -
    1 1 1 0 0
    1 1 1 1 1

  1. Produce a karnaugh map corresponding to the table above.

  2. Produce a minimal sum-of-products expression for the function.

  3. Draw a circuit equivalent to your sum-of-products expression but using only NAND gates (i.e. using the SOP-to-NAND conversion discussed in lectures).


Part 3: architecture and assembly language [16 marks]

Produce a Marie program with behaviour corresponding to the C program shown below.

#include <stdio.h>

int main()
{
   int x = 10;
   int y = 20;
   int z = 0;
   scanf("%d", &x);
   scanf("%d", &z);
   if (x > y) {
      if (x > z) {
         z = x;
      }
      printf("%d", z);
   } else {
      printf("%d", y);
   }
}