Exam 2 Solutions

22C:18, Fall 1996


Grade Distribution

   Mean = Median = 8.8                  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 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
Grade (roughly)     D   ||||   C   ||||   B   ||||   A   ||


  1. Fixed point numbers:
        Base 2          Base 10    |    Base 2          Base 10          
                                   |
       1110.1011         14.6875   |
                                   |  _0011.1001_         3.5625
       1001.0110      ____9.375__  |  
                                   |  _1001.1111_         9.9375
       0110.0001      ____6.0625_  |
    
    Almost everyone got this perfect; there was only one careless mistake.

  2. Floating point numbers with a biased binary exponent and a signed magnitude mantissa, normalized with the point between sign and magnitude bits and the most significant magnitude bit equal to 1:
                                 _ _ _ _ _ _ _ _
                                |_|_|_|_|_|_|_|_|
                                |s|exp|   man   |
        The largest         
          positive number.       0 1 1 1 1 1 1 1     __1.9375____
        The most negative    
          number.                1 1 1 1 1 1 1 1     _-1.9375____
        The smallest nonzero
          positive number.       0 0 0 1 0 0 0 0     __0.125_____ 
        The largest nonzero 
          negative number.       1 0 0 1 0 0 0 0     _-0.125_____
    
    1 in 5 did this perfectly, but perhaps 1 in 10 used the wrong signed number representation -- usually two's complement, and 1 in 5 misread the exponent or uniformly misplaced the point. A remarkable number, perhaps 1 in 7, managed to study last semesters test so carefully that they gave answers in terms of the format given in that test instead of the format given here!

  3. Programming with fixed point arithmetic using 7 bit fixed point fractions:
         ; A is in R3
         ; B is in memory, pointed to by R8
         ; C is in field of a record in memory,
         ;     R9 points to the record,
         ;     F is defined as the offset of that field.
         ; TIMES (pointed to by PTIMES) is in monitor.a
    
                 LOADS   R4,R8       ; get B
    	     LOAD    R1,PTIMES   ; (wipes out R4-7)
                 JSRS    R1,R1       ; R3 = A*B, unnormalized
    
                 SRU     R3,7        ; normalize the product
                 LOADS   R4,R8       ; get B
                 ADDSL   R4,R3,1     ; R4 = A*B + 2*B
                 LOAD    R3,R9,F     ; get C
                 ADD     R3,R3,R4    ; R3 = A*B + 2*B + C
    
         ; R3 contains D; D = A*B + 2*B + C
    
    This 5 instruction answer is quite short compared to the answers many gave. Nonetheless, 1 in 6 gave perfect answers here. The most common errors were to forget to shift the product 7 places left in order to align the points, and to forget that TIMES wipes out R4-7, assuming that B was still in R4 after the multiply.

  4. Adding appropriate comments to procedures and functions:
         ; --------------------
         ; R2 points to AR with following format
         ;       0  ;return address
    
         TIMBSV= 4  ;_Multiplicand Save Location______________
    
         TIMAR=  8  ;_Activation Record Size__________________
    
         TIMES:  ; horrible function to multiply two numbers
    
                 ; expects numbers in _R3 and R4______________
    
                 ; returns result in _R3______________________
    
                 ; wipes out _R4______________________________
                    
                 TESTR   R3		  ; see if multiplier is zero
    	     BZS     TIMQT        ;   return zero if so
                 STORES  R1,R2        ; save the return address
                 STORE   R4,R2,TIMBSV ; save the multiplicand
                 ADDI    R4,R3,-1     ; decrement the multiplier
                 LOAD    R3,R2,TIMBSV ; restore the multiplicand
                 ADDI    R2,TIMAR     ; adjust the AR pointer
                 JSR     R1,TIMES     ; recursive call
                 ADDI    R2,-TIMAR    ; restore the AR pointer
                 LOAD    R4,R2,TIMBSV ; recover the multiplicand
                 ADD     R3,R3,R4     ; add it to the product
                 LOADS   R1,R2        ; recover the return address
         TIMQT:  JUMPS   R1           ; return
    
    This problem should have been easy, but many students had problems; it may have been the hardest problem on the exam! The most common minor error was to declare that the program wiped out R3 -- it returns a value there! Far more serious errors were common.

  5. Hawk Programming:
         ;----------------------------
         ASCIIADD:       ; add 2 4-digit ASCII numbers.
                         ; given A and B in R3 and R4
    		     ; returns A+B in R3
    		     ; may destroy R4-7
    
               ADD    R3,R3,R4
               LOAD   R4,C96
               ADD    R3,R3,R4    ; R3 = T1 = A + B + 0x96969696
               LOAD   R4,C30
               AND    R4,R3       ; R4 = T2 = T1 & 0x30303030
               SRU    R4,3
               SUB    R3,R3,R4    ; R3 = T3 = T1 - (T2 >> 3)
               LOAD   R4,C0F
               AND    R3,R4
               LOAD   R4,C30
               OR     R3,R4       ; R3 = SUM = (T3 & 0x0F0F0F0F) | 0x30303030
               JUMPS  R1          ; return
    
         C96:  W      #96969696
         C30:  W      #30303030
         C0F:  W      #0F0F0F0F
    
    A fair number did quite well here, but only 1 in 20 did perfectly. The most common errors were forgetting to return and forgetting to put the result in the correct register. The most common serious error was forgetting that immediate mode on the Hawk machine is limited to 16 bit constants. 1 in 3 made this error!