Assignment #4: Arithmetic, Data Operations, and the ALU


Assigned: Wednesday, October 24
Due: Wednesday, October 31 by 5pm

Contents:


Overview

Topic: Operating on binary integer numbers. Arithmetic (+, -, *, /, %), Boolean (&, |, ^, ~), and shifts (<<, >>)
Related Reading: Sections 5.2, 6.2, and class notes


Practice Problems


Problems to be Submitted (25 points)

When you turn in your assignment, you must include a signed cover sheet (PDF version) with your assignment (you're assignment will not be graded without a completed cover sheet).

You are allowed to submit your assignment via email, but if you choose to do so, you must bring a hardcopy of your assignment along with a completed cover sheet to the instructor at the next class. (Note: Do not email the instructor any .zip file attachments, as SLU's email may not accept these emails; i.e. the instructor may not receive your email.)

  1.   (10 points)    Operations on Data:

      Given the following variable declarations:

         short a = 0b0010101100011001;
         short b = 0b0101100110101011;
         short c = 0b1111111101110010;
    
         unsigned short j = 0x95;
         unsigned short k = 0xF35C;
    
         int n = 0xA39D2;
         int p = 0x5A48;
    

      Compute results for the following data operations.

            Be sure to show your work, and please give your answer in binary or hex

    1.    a   +   b
    2.    n   ‐   p
    3.    b   &   c
    4.    n   ^   p
    5.    a   <<   7
    6.    k   >>   6
    7.    c   >>   5
    8.    (23   <<   4) ‐ 0xFF
    9.    j % 6
    10.    ((a + b < c) || ((p < n) && !(j % 2)))

  2.   (6 points)    Shifts and Bit Manipulation for Fast Multiplies and Divides:

    Assume for the following problems that x and y have been declared as follows:

        short x, y;
    

    Note:  x can be any value that results in valid value for y.

    1.   The following code corresponds to the mathematical equation: y = K * x, for some fixed value of K. In this case, what is K?
    2.       x = 13
            y = (x << 3) - x
      
    3.   The following code similarly corresponds to: y = K * x, but for what value of K?
    4.       x = 6
            y = x + (x << 1) - (x << 4)
      
    5.   The following code corresponds to the mathematical equation: y = K * x, for some fixed value of K < 1. What is the value of K?
    6.       x = 22
            y = (x >> 2) + (x >> 3)
      
    7.   Relative to multiplication and division, what mathematical equation does the following code produce?
    8.       x = 117
            y = x & 0x0f
      
  3. (9 points)

    Using the diagram of the ALU from Figure 5.17 in the textbook, displayed here or downloadable here:

    Indicate what the 32-bit Result output, and each of the 1-bit Zero, Negative, and Carry outputs, would be for each of the following sets of inputs:

    1.   A = 0x3D56B,   B = 0x28C5,   ALUControl1:0 = 11
    2.   A = 0x3D56B,   B = 0x28C5,   ALUControl1:0 = 00
    3.   A = 0x28C5,   B = 0x28C5,   ALUControl1:0 = 01
    4.   A = 0x3A5,   B = 0xC48,   ALUControl1:0 = 10
    5.   A = 0x3D56B,   B = 0x28C5,   ALUControl1:0 = 01
    6.   A = 0xFFF3D56B,   B = 0xFFFF28C5,   ALUControl1:0 = 10