Exam 2: Midterm

Solutions and Commentary

Part of the homework for CS:2630, Summer 2018
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Grade Distributions

Exam II

                 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

Total of Exams I and II

Mean   = 8.04  X
Median = 7.2   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

Machine Problems 1-5

Mean   = 9.75
Median = 8.0
             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.

Homeworks 1-11

                         X
Mean   = 19.70           X
Median = 20.1            X
                         X
             X       X   X X
  _______X_X_X___X___X_X_X_X___X_____X_________X_________
    10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30. 32. 34.

Total Scores

Mean   = 37.49
Median = 34.9      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. 72. 74.

Solutions and Commentary

  1. Here is a little floating point format that is based on all of the same ideas as the IEEE floating point format:
                       
    sexpmnt
        s =sign of mantissa
    exp=biased exponent
      111 – not a number, infinity if m=0
      011 – represents zero
      000 – non-normalized values (hidden bit = 0)
    mnt=mantissa with hidden bit in 1's place

    Answer the following (0.3 points each):
     
    a) Give the largest mantissa value in decimal.
        ___1.75________________________
     
    b) Give the smallest normalized mantissa in decimal.
    ___1.00________________________
     
    c) Give the smallest non-normalized mantissa in decimal.
    ___0.00________________________
     
    d) Give the smallest exponent, in decimal.
    ___-2__________________________
     
    e) Give the largest exponent, in decimal.
    ___+3__________________________

    Just under half got parts b and c right, 5 or fewer got parts a, d and e right. 1/3 of the class earned no credit on each part of the problem, and half the class earned no credit on parts b and d.

    f) Now assume that the least significant 6 bits of R3 contain a number in this format. Fix the following SMAL Hawk code so that it extracts the exponent field of this number and converts it to a 32-bit 2's complement value; ignore the NAN and non-normalized cases. (0.5 points)

            SR      R3, __2_______
    
            TRUNC   R3, __3_______
    
            ADDSI   R3, _-3_______
    

    Just 2 earned full credit, whille 1/3 of the class earned no credit. The final instruction subtracting the bias of 3 was the hardest; 1/3 of the class missed this while earning partial credit with other parts of the answer correct.

  2. A little problem: Suppose R3 and R4 hold a 64-bit integer, with the least significant 32 bits in R3. Write SMAL Hawk code to decrement this integer. (0.5 points)
        ________ADDSI___R3,-1__________________________________________
    
        ________BCS_____NOBORROW_______________________________________
    
        ________ADDSI___4,-1___________________________________________
    
        NOBORROW:______________________________________________________
    

    Only 1 got this right, while almost half the class left this blank and over 1/3 gave bizarre answers. Among the odd answers were those involving loops, shifts and operands in memory. Among those earning partial credit, one flipped the sense of the carry bit.

  3. Background: Here is a fragment of Hawk code.
            LIL     R3,A
            LOAD    R4,R2,B
            ADD     R4,R3,R4
            LOADS	R3,R4
            EXTB	R3,R3,R4
            STORE	R3,R2,C
    

    a) What kind of variable is A. That is, where is it and what does it hold. (0.3 points)

        ___A is a global or array of bytes_____________________________
    

    2 got this, over 1/3 earned no credit. Among those earning partial credit, most understood that A was global (either a constant array in ROM or statically allocated in a COMMON), while 1/5 failed to mention that it was an array and 1/3 failed to indicate that the array entries were bytes.

    b) What kind of variable is B. That is, where is it and what does it hold. (0.3 points)

        ___B is a local integer variable_______________________________
    

    None earned full credit, while 1/2 earned no credit. Among those earning partial credit, the most common error was to omit the fact that this is an array of integers (or at least, an array of values that can be used as array indices).

    c) What kind of variable is C. That is, where is it and what does it hold. (0.3 points)

        ___C holds one byte____________________________________________
    

    2 earned full credit, while close to 2/3 earned no credit. Among those earing partial credit, many left out the fact that C holds a single byte.

    d) The computation done by the above code can be expressed as a single high-level language statement. Give that statement. (0.5 points)

        ___C = A[B]____________________________________________________
    

    3 did well. 1/3 gave answers that were worthless, while another 1/3 left this blank. Among those earning partial credit, a number said C=A+B, failing to understand the use of an array, while some attempted long-winded and frequently ambiguous answers in English instead of a simple statement in a programming language notation.

    e) Give assembly lanugage code to describe an activation record that contains just a return address and the local variables used in the above code. (0.6 points)

        ___RETAD   =       0___________________________________________
    
        ___B       =       4___________________________________________
    
        ___C       =       8___________________________________________
    
        ___ARSIZE  =       12__________________________________________
    
        _______________________________________________________________
    
        _______________________________________________________________
    

    3 did well, 1/3 earned no credit. 1/4 of the class earned partial credit by adding A to the activation record, while 2 others omitted a definiton of ARSIZE, a value that was defined for every activation record ever demonstrated for this class.

  4. Background: Prior to 1971, there were 252 pence (pennies) to the guinea in British currency, so converting guineas to pennies, you had to multiply by 252 which is 21×12 or 111111002. Write efficient Hawk code to multiply by 252. (1 point)
        ___MOVESL  R1,R3,2_____________________________________________
    
        ___SL      R3,8________________________________________________
    
        ___SUB     R3,R3,R1____________________________________________
    
        _______________________________________________________________
    
        _______________________________________________________________
    
        _______________________________________________________________
    
        _______________________________________________________________
    
        _______________________________________________________________
    
        _______________________________________________________________
    

    1/2 of the class earned full credit. 3 gave essentially the above answer, hands down the most efficient solution. 2 multiplied by 12 and 21, using optimal sequences for each for a total of 5 instructions, and 2 more multiplied by the prime factors 3, 3, 4 and 7, again using optimal sequences for each for a total of 6 instructions. Finally, one tried to add all the partial products using a MOVE followed by 6 ADDSL instructions, but made a small error, earning partial credit.

    One student managed to write out the full iterative multiply algorithm with correct assignment of a constant multiplier of 252, earning full credit despite the fact that this solution is hands-down the longest and slowest solution. 4 others tried to do this but made multiple errors, in some cases, so many errors that they earned no credit at all.

    Several earned partial credit for code that multiplied by some of the factors of 252 and then stopped.

    One person (not a student in this class) said that it was a wonder that England ever managed to run an empire with a currency system that was so bizarre. 252 pence to the guinea is, after all even more bizarre than 12 inches to the foot.

  5. Background: Consider the logic circuit shown to the right.

    a) Give a boolean expressions for c, d and e. (0.6 points)

    c = ___not( a or b )________________________

    d = ___not( (not b ) or e )_________________

    e = ___not( c or d )________________________

    1/5 got all 3 correct, while 2 or 3 gave nonsense answers to each part. 1/6 confused nand and nor, while 1/10 seemed to have difficulty interpreting the "bubbles" as meaning not.

    b) Give as much of a truth table for this circuit as you can. Note that, because it contains a feedback loop, some rows of the truth table may have multiple solutions. (1.0 points)

     a  b   c  d   e 
     
    00
     _1_  _0_   _0_ 
     
    01 0 1
     _0_  _1_   _0_ 
     
    10
     _0_  _0_   _1_ 
     
    11 0 1
     _0_  _1_   _0_ 

    3 earned full credit. Only 2 earned no credit, although some apparently earned some credit at random -- after all, there were only 2 alternatives for many of the blanks here. By far the hardest part of the question was recognizing that there were two solutions to this problem in each of two different rows.

    c) What combination of the inputs, (if any) causes this circuit to store the value of e (0.3 points)

        ___b = 1_______________________________________________________
    

    1 got this. 1/5 lost points for specifying a specific value of a when in fact the value of a is ignored when b is set. 2/3 earned no credit. Of these, many specified values of c and d, where these are not external values that a user can control in order to make this circuit do something.

    d) How can you set e to 0. (0.3 points)

        ___a = 0 and b = 0_____________________________________________
    

    2 got this. Only 1 earned partial credit. Wrong answers were similar to those discussed above.

    e) How can you set e to 1. (0.3 points)

        ___a = 1 and b = 0_____________________________________________
    

    1/5 got this, while an equal number earned partial credit for giving one of the two values correctly. 1/3 earned no credit for specifying external control over c and d, as discussed above.

  6. Background: Calling a subroutine involved fiddling with the activation record as well as some flavor of JSR instruction. Wouldn't it be nicer to write CALL SUBR where SUBR is the label on a subroutine?

    A problem: Write a SMAL macro definition for the CALL macro that does this, using our standard (non-optimized) Hawk calling sequence. (1.0 point)

        ___MACRO   CALL,=subr__________________________________________
    
        _____ADDI    R2,R2,ARSIZE______________________________________
    
        _____LIL     R1,subr___________________________________________
    
        _____JSRS    R1,R1_____________________________________________
    
        _____ADDI    R2,R2,ARSIZE______________________________________
    
        ___ENDMAC______________________________________________________
    
        _______________________________________________________________
    
        _______________________________________________________________
    

    1 earned full credit, while 2/3 earned no credit, mostly by leaving this blank. Errors leading to partial credit, each made by 2 or fewer students included forgetting to declare a parameter for the macro, forgetting to adjust R2, omitting one of the register operands for one of the instructions, using a PC-relative JSR so that the macro won't work for calling a monitor routine, and adding gratuitous extra instructions.

  7. Background: Here is a fragment of C code:
            if (a[i] < 0) a[i] = 0;
    

    Assume that A is a local array of integers stored in the current activation record.

    Assume that I is a local variable, and that it is an integer that is already known to be a legal index value for the array.

    A Problem: Write SMAL Hawk code for equivalent to the above C code. Please use consecutive registers starting at R3 to solve the problem. (1 point)

        ___________LEA     R3,R2,A_____________________________________
    
        ___________LOAD    R4,R2,I_____________________________________
    
        ___________ADDSL   R4,R3,2_____________________________________
    
        ___________LOADSCC R0,R4_______________________________________
    
        ___________BGE     ENDIF_______________________________________
    
        ___________STORES  R0,R4_______________________________________
    
        ___ENDIF:______________________________________________________
    
        _______________________________________________________________
    
        _______________________________________________________________
    
        _______________________________________________________________
    
        _______________________________________________________________
    
        _______________________________________________________________
    

    Note: It can be done with 6 instructions.

    1/6 left this blank, and almost 1/2 gave answers that were so packed with errors that there was no credit. Among the popular errors were using LOAD instead of LEA, omitting R2 in the addressing of local variables.

    Forgetting to load I was common (generally, people did not forget A in a similar way; perhaps people assumed I was alread in a register, despite the problem statement). Forgetting to multiply I by 4 was also common -- despite the fact that A is explicitly an array of integers, not characters. Inverting or otherwise changing the condition tested on A[I] was also common, as was forgetting to zero it.

    Finally, 1/4 of the class added a wrapper turning the code into a callable function instead of just a fragment of code out of context, and 1/4 added byte manipulation code.