Exam 2: Midterm

Solutions and Commentary

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

Grade Distributions

Exam II

                     X        
Mean   = 4.45        X        
Median = 4.1         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_X_____X_______
    0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10

Total of Exam I and Exam II

                               X  
Mean   = 12.81             X   X X
Median = 13.1              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 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20
A rough grade scale:   D     C     B     A   

Homeworks 1 to 10

Mean   = 19.51                       X     X       X      
Median = 19.0                        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_X_____
    0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30

Machine Problems 1 to 3

                 X
Mean   = 8.96    X
Median = 9.0     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_X___X_
    0 . 2 . 4 . 6 . 8 . 10. 12. 14.

Overall Total Scores

Mean   = 41.28         X
Median = 40.5          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_X_______X___X___
    20. 24. 28. 32. 36. 40. 44. 48. 52. 56. 60. 64
     F         D         C         B         A     
A rough grade scale

Exam Solutions and Commentary

  1. Consider this high level language expression: c = a[b[c]];
    Your job is to translate this to Hawk assembly code, in the context of the following definitions.
            COMMON  A,1024  ; int a[256] -- 256 words
    PA      W       A
            COMMON  B,256   ; char b[256] -- 256 bytes
    PB      W       B
    
    RA      =       0
    C       =       4       ; int c -- a local variable
    ARSIZE  =       8
    

    a) First translate b[c], leaving the value in R3. R4-7 are available for temporaries. Comments are provided here for your guidance. (1 point)

    ;       Blanks to fill.    The comments below were given.
    
            LOAD    R3,R2,C    ; get local variable c
            LOAD    R4,PB      ; get address of array b
            ADD     R4,R4,R3   ; get address of array element b[c]
            LOADS   R3,R4      ; get word holding array element b[c]
            EXTB    R3,R3,R4   ; get byte b[c] into R3
    

    4 did perfect work. Most got the LOADS instruction, while the initial load of the local variable C caused the most trouble, and getting the address of the array B caused almost as much trouble.

    b) Second finish c = a[b[c]], assuming that R3 initally holds the value of b[c]. Again, R4-7 are available for temporaries. Here, you are responsible for comments. The problem can be solved in 4 instructions, but may be easier to underestand in 5. (1 point).

    ;       code and comments were left to the student
    
            LOAD    R4,PA      ; get address of arrray a
            SL      R3,2       ; multiply subscript by 4 (a is array of words!)
            ADD     R3,R3,R4   ; compute address of array element a[b[c]]
            LOADS   R3,R3      ; get value of a[b[c]]
            STORE   R3,R2,C    ; finish c = a[b[c]]
    

    Without question, the two most difficult elements of this had to do with failure to read the question carefully. The array a is clearly stated to be an array of words (not characters), yet 17 students left out the SL instruction and 13 included an EXTB instruction. Similarly, the notation in the question is clearly for an assignment to the variable c, yet 18 students left out the final STORE. 7 students left this blank or gave answers indicating total confusion.

    A circuit

  2. The logic circuit shown here is actually some kind of flipflop.

     

    a b f
    0 0 remember
    0 1   0
    1 0   1
    1 1 remember
    a) Fill in the truth table to the right to describe the behavior of this circuit.

    Appropriate values for entries in the f column are 1, 0 only when the inputs uniquely determine the outputs, remember for combinations of inputs that allow the flipflop to retain memory, and metastable for combinations of inputs that lead to metastable behavior. (1 point)

    b) Given your answer to part A, which of the flipflops that we discussed does this flipflop most closely mimic? (0.5 points)

     __RS______________________________
    

    c) Show that you understand your answer to part b by marking, on the above figure, which inputs correspond to C, D, R, S (or whatever is appropriate) and which outputs correspond to Q or Qbar (whichever is appropriate). (0.5 points)

    8 did well on parts a and c, while 14 did well on part b. For part a, all but 6 got lines 01 and 10 of the truth table correct, while 20 had difficulty with line 00, frequently indicating that it was a metastable state, where in fact, lines 00 and 11 of both make this flipflop remember its input.

    There are two sensible labelings of the A and B inputs, depending on whether you consider 00 or 11 the resting state of the inputs. Wrong answers frequently managed to plaster all possible combinations of letters related to flip-flop inputs and outputs on parts of the diagram.

  3. Consider a 15-bit binary floating-point number with the mantissa sign, a 6-bit biased exponent, and an 8-bit fraction (signed magnitude, normalized when possible).
     |___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|
     |   |       exponent        |            mantissa           |
    
    a) Give the representation of the following numbers in this system (1.2 points)
      _1_ _1_ _0_ _0_ _0_ _0_ _1_ _1_ _0_ _0_ _0_ _0_ _0_ _0_ _0_  = -1
    
      _0_ _1_ _0_ _0_ _0_ _1_ _0_ _1_ _0_ _0_ _0_ _0_ _0_ _0_ _0_  = +2
    
      _1_ _1_ _0_ _0_ _0_ _1_ _1_ _1_ _0_ _1_ _0_ _0_ _1_ _0_ _0_  = -5.125
    
    b) Give the values of the following, expressed as the power of two represented by the exponent times a multiplier (possibly a fraction) representing the mantissa. (0.8 points)

    The smallest positive normalized number ___1/2 × 2-32___

    The smallest positive nonnormalized number ___1/256 × 2-32___

    4 got the representation of -1 correct, while 3 earned no credit and 8 had severe problems. Getting the sign wrong was a common error. Most did as well with the representation of +2, but only 2 got -5.125 correct, and 19 had severe difficulty, earning little or no credit. The fraction 0.125 was deliberately easy (1/8), yet a fair number of students filled the margins of the page with arithmetic as they tried to grapple with this!

    Part b was even harder, with only 2 doing well, while 15 earned little or no credit.

  4. The header file numbin.h, distributed as part of MP4, contains EXT directives for many symbols. The need for some of these is obvious, for example NUMBIN, the address of the class description itself. Under what circumstance would an applications program make direct use of the others, for example, NBIADD, the address of the NADD method? (0.5 points)
    ___If you know you have an object of class NUMBIN_then______
    ___you can directly call NBIADD for a bit of a speedup______
    

    9 did well here, while 9 earned no credit.

  5. The header file number.h, distributed as part of MP4, contained no EXT directives, but only the definitions of symbols such as NASSBIN=0, NASSNUM=4, NTOBIN=8, NTODSP=12 and NADD=16, along with lots of comments. What is the intended use of these symbols? (0.5 points)
    ___These are offsets into the class descriptors for_________
    ___objets of subclasses of NUMBIN for the method pointers___
    

    3 did well here, while 15 earned no credit. A large fraction of the latter gave descriptions of the data structure as having something to do with activation records or the stack, neither of which is the case.

  6. Consider the code fragments given below from the file numbin.a that is the subject of MP4 (with added line numbers):
    20  ; class description for all instances of this class
    21          INT     NUMBIN
    22  NUMBIN: W       NBIASBN ; address of ASSBIN routine for NUMBIN
    23          W       NBIASNM ; address of ASSNUM routine for NUMBIN
    24          W       NBITOBN ; address of NTOBIN routine for NUMBIN
    25          W       NBITODS ; address of NTODSP routine for NUMBIN
        ...
    33          INT     NBIASBN ; address of ASSBIN routine for NUMBIN
    34  NBIASBN:
    35          ; takes   R3 = pointer to destination number object
    36          ; takes   R4 = binary integer value to assign
    37          ; returns R3 = pointer to destination object
    38
    39          LEA     R5,NUMBIN
    40          STORES  R5,R3           ; dest.class = NUMBIN
    41          STORE   R4,R3,INTREP    ; dest.intrep = R4
    42          JUMPS   R1              ; return
    

    a) If the NBIASBN method is always called as a method of a previously initialized number object, two of the instructions in the code given above will never do anything useful. Which two? (0.5 points) __39___40_____________________

    9 did well here, while 6 earned no credit. The most popular wrong answer, given by 20, was 40 and 41 (good for minimal partial credit).

    b) Why might a program link to the code for NBIASBN directly instead of calling it indirectly through the class description? Hint: The most important reason is related to part a. (0.5 points)

    ___to initialize the object_________________________________
    ___or to call the method when the object's class is known___
    

    6 did well here while 19 earned no credit. Some answers verged on incoherent.

    c) Given that R3 contains a pointer to an object of some concrete subclass of the class NUMBER (possibly NUMBIN), where the first word of the object points to the class descriptor, write code to call the ASSBIN routine of that object to assign it the value 5. (Please ignore the need to adjust the stack pointer). (1.0 points)

            LIS     R4,5            ; load the constant
            LOADS   R1,R3           ; get pointer to class descriptor
            LOAD    R1,R1,ASSBIN    ; get pointer to method
            JSRS    R1,R1           ; call method
    

    4 did well here. 8 earned no credit, 10 forgot the LOADS, 8 had trouble with the LOAD and 5 forgot to deal with the constant 5. The solution to this problem could be lifted almost verbatim from the code distributed for the final machine problem. Students who had studied and understood that code should have been at a significant advantage.

    d) Given that LOCAL is the offset in the current activation record of an uninitialized object of class NUMBIN (LOCAL therefore refers to a 2-word block of memory), write code to call the NBIASBN routine to initialize this object to the value 5. Here, you will definitely not be able to ignore the stack pointer! (1.0 points)

            LIS     R4,5            ; load the constant
            LEA     R3,R2,LOCAL     ; get the address of the object
    ;       ADDI    R2,R2,ARSIZE    ; (optional)
            LOAD    R1,PNBIASBN     ; get pointer to method
            JSRS    R1,R1           ; call method directly
    ;       ADDI    R2,R2,-ARSIZE   ; (optional)
    

    2 did well, 9 left this blank, 6 more earned no credit. The most difficult part was the LEA instruction to get the address of the constant, where 10 had trouble. A significant number tried to duplicate their code from part a, despite the fact that the local variable was explicitly stated to be uninitialized. This problem did not correspond directly to any code distributed with the machine problem.

    Note that it is harmless to push and pop the current activation record, but unnecessary, because the code to NBIASBN never needs to reference the stack. On the other hand, confusing the addressing of the local variable LOCAL with pushing and popping the stack is a serious mistake that was made by several students.