Exam 2: Midterm

Solutions and Commentary

Part of the homework for 22C:60, Fall 2007
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Grade Distributions

Exam 2

Median =  5.9                 X
Mean   =  5.44                X
                              X
            X     X           X
            X X   X         X X       X X
 _________X_X_X_X_X___X_X_X_X_X___X_X_X_X_____
   0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10

Exams 1 and 2

                                    X
Median =  13.9                      X
Mean   =  13.41                     X
                                    X
                          X   X     X   X
              X           X   X   X X   X
 _____________X_X_X_X_X_X_X_X_X___X_X_X_X_____
   0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20

Homeworks 1 to 11

Median = 24.7                                       X
Mean   = 22.02                              X       X
                                      X     X       X X X    X X
 _____________________________X___X___X_X_X_X_____X_X_X_X____X_X_X_X__
   0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30. 32

Machine Problems 1 to 3

Median = 10.0                               X                   X
Mean   = 10.32                              X                   X
                              X   X X     X X           X X     X
 ___________X___________X_____X_X_X_X_X___X_X_____X_____X_X_X_X_X___
   0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10. 11. 12. 13. 14. 15.

Total Scores

Median =  45.2                                 X
Mean   =  45.75                X               X
                   X       X   X X       X X   X X
 __X_____X___X_X___X___X_X_X_X_X_X___X___X_X___X_X_______
  16. 20. 24. 28. 32. 36. 40. 44. 48. 52. 56. 60. 64. 68
    -   D   +   -   C   +   -   B   +   -   A   +  

Exam and Solutions

    graphic notation for flipflop built from an and-or cascade
  1. Background: Consider this flipflop circuit:

    The space to the left is available for any
    logic diagrams, truth tables or other tools
    you might use in answering the following:
    (0.5 points each)

      a    b     c    d     e  
    00000
    0100/10/1
    10000
    11111
    a) What input values permit this flipflop to remember?

        A = ____0____   B = ____1____

    b) How do you set this flipflop?

        A = ____1____   B = ____1____

    c) How do you reset this flipflop?

        A = ____0____   B = ____0____

    7 did the problem perfectly, 5 more got part a) right, 3 more got part b) right, and 2 more got part c) right.

    9 students gave essentially the logic diagram above, 5 gave variant logic diagram with more "flipfloppy" layout. 13 gave truth tables. Only one student earned no credit on this problem.

  2. Consider this extraordinarily minimal floating point format:
      _ ___ _   s - 1 bit, the sign of the signed-magnitude mantissa
     |_|___|_|  e - 2 bits, the exponent, biased so 10 = 0
     |s| e |m|        note:  e = 00 means the mantissa is not normalized.
                m - 1 bit plus 1 hidden bit, the magnitude of the mantissa,
                      the point is between the hidden bit and the m bit.
    

    Give decimal equivalents of all 16 floating point values below. Feel free to use the space to the right as scratch space. (2.5 points)

        0000 = __0.0____       1000 = _-0.0_____       9 had trouble with zero

        0001 = __0.25___       1001 = _-0.25____       16 had trouble with this non-normalized value

        0010 = __0.5____       1010 = _-0.5_____

        0011 = __0.75___       1011 = _-0.75____

        0100 = __1.0____       1100 = _-1.0_____

        0101 = __1.5____       1101 = _-1.5_____

        0110 = __2.0____       1110 = _-2.0_____

        0111 = __3.0____       1111 = _-3.0_____

    2 did perfectly, 4 earned no credit at all. 2 seriously misinterpreted the sign bit.

  3. A Problem: Write SMAL Hawk code to multiply R3 by 165; if you need a scratch register, use R1. Your solution will be graded for efficiency, so you may want to explore both factoring and using a binary representation to see what works best. (3 points)
            ; best solution, based on 165 = 5 x 33            -- base score 3.0
                    ADDSL   R3,R3,2 ;    R3 times  5
                    ADDSL   R3,R3,5 ;    R3 times 33
    
            ; next best solution, based on 165 = 10100101     -- base score 2.5
                    MOVE    R1,R3
                    ADDSL   R3,R1,2 ;    101
                    ADDSL   R3,R1,3 ;    101001
                    ADDSL   R3,R1,2 ;    10100101
    
            ; brute force solution, based on 165 = 3 x 5 x 11 -- base score 2.0
                    ADDSL   R3,R3,1 ;    R3 times  3
                    ADDSL   R3,R3,2 ;    R3 times  5
                    MOVE    R1,R3   ; \
                    ADDSL   R3,R1,2 ;  | R3 times 11 = 1011
                    ADDSL   R3,R1,1 ; /
    

    6 gave the optimal solution, one more tried but made an error. 1 gave the next best solution, anf 4 more tried but made errors. 1 gave the brute-force solutioin and 7 made errors in trying this. There were several ad-hoc solutions tried, and a few tried to give general purpose multiply algorithms to multiply by a constant. Most efforts in this direction were peppered with errors. Of the 4 who earned no credit at all, 3 wrote considerable meaningless code.

  4. Background: The following two SMAL Hawk code fragments are inspired by a secondary problem you must solve to do MP5. They do the same thing.
    FIRST:  TBIT    R4,0            ; 1
            BCR     A               ; 2
            SL      R3,1
    A:      TBIT    R4,1            ; 3
            BCR     B               ; 4
            SL      R3,2
    B:      TBIT    R4,2            ; 5
            BCR     C               ; 6
            SL      R3,4
    C:      TBIT    R4,3            ; 7
            BCR     D               ; 8
            SL      R3,8
    D:
    
    SECOND: NEG     R4,R4           ; 1
            ADDI    R4,R4,15        ; 2
            BTRUNC  R4,2            ; 3
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SR      R4,2            ; 4
            BTRUNC  R4,2            ; 5
            SL      R3,4
            SL      R3,4
            SL      R3,4
    

    a) For random initial values of R4 from 0 to 15, how many instructions would you expect to be executed in an average run through each? (1 point)

        First: __10_______________________     Second: ___8_______________________

    5 did perfectly, itis difficult to classify wrong answers, but several earned partial credit for off-by one errors and several more gave the minimum number of instructions executed for the first solution while giving the maximum number of instructions for the second solution. 3 earned no credit at all.

    The method that works here is to count the minimum number of instructions that could be executed in a pass through the code (the comments in the code give this count), and then count the total number of instructions. Take the average of these two numbers to get the average number of instructions.

    b) The first fragment given above uses base 2, while the second fragment uses base 4. For random initial values of R4 from 0 to 15, how many instructions would you expect to be executed in an average run through the base 16 solution? (1 point)

        __10.5____________________________

    Just 1 got full credit here, 2 more had small penalties for off-by-one errors, while a few got a bit of credit for off by 1 errors on the minimum or maximum. 14 earned no credit at all. Some gave instruction counts far higher than the number of instructions in the code, impossible answers in this context, since the code contains no loops.

    The base 16 solution is shown below, with minimum instruction counts.

    BASE16: NEG     R4,R4           ; 1
            ADDI    R4,R4,15        ; 2
            BTRUNC  R4,4            ; 3
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SL      R3,1
            SL      R3,1
    
    c) What does it do? In a language such as C or Java, you can express the correct answer in one very simple assignment statement. (1 point)

        ___R3=R3<<R4_____________________

    3 did well. 5 more guessed that it was a left shift but were confused about how far it shifts. 3 got partial credit for indicating the right operands with a multiply or exponentiate operation, since those change the operand in the right way but by the wrong amount. 8 got no credit at all.