Exam 2

22C:18, Fall 1996, 13 points

Douglas W. Jones
Name ______________________________________   Section ___________

This exam is open-book and open-notes, but closed neighbor! You have one (academic style) hour, which is defined as 50 minutes. Answer the questions in the space provided. If you need scratch paper, feel free to use the backs of the exam pages, but please transcribe your answers into the blanks provided.
  1. Fixed point numbers. In the following table, all numbers in each column share the same base, while all numbers in each row have the same value, fill in the blanks. (0.4 points per blank, 2.4 total.)
        Base 2          Base 10    |    Base 2          Base 10          
                                   |
       1110.1011         14.6875   |
                                   |  _____._____         3.5625
       1001.0110      _____._____  |  
                                   |  _____._____         9.9375
       0110.0001      _____._____  |
    

  2. Floating point numbers: Consider a system with the following odd floating point format. Numbers are 8 bits, represented using a biased binary exponent and a signed magnitude binary mantissa, normalized so that the most significant non-sign bit of the mantissa is one, with the point immediately to the left of that bit. The format is as follows, where exp is the exponent, s is the sign of the mantissa, and man is the the mantissa, except for the sign bit:
                             _ _ _ _ _ _ _ _
                            |_|_|_|_|_|_|_|_|
                            |s|exp|   man   |
    
    Give the binary values and fixed point decimal equivalents for each of the following numbers (0.4 each, 1.6 total):
        The largest         
          positive number.       _ _ _ _ _ _ _ _     ____________
        The most negative    
          number.                _ _ _ _ _ _ _ _     ____________
        The smallest nonzero
          positive number.       _ _ _ _ _ _ _ _     ____________ 
        The largest nonzero 
          negative number.       _ _ _ _ _ _ _ _     ____________
    
    
    
    

  3. Programming with fixed point arithmetic: Given that A, B, C and D in the following problem are represented as 32 bit fixed point two's complement binary numbers with with 7 bits of fraction, write code to bridge the gap between the indicated pre and postconditions: (3 points)
         ; 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
                 
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
                 ____________________________
    
         ; R3 contains D; D = A*B + 2*B + C
    
    
    
    
    
    
    
    
    
    

  4. Procedures and functions! The following code is missing some key comments that would help a caller understand how to call it. Give them (2 points):
         ; --------------------
         ; R2 points to AR with following format
         ;       0  ;return address
    
         TIMBSV= 4  ;_________________________________________
    
         TIMAR=  8  ;_________________________________________
    
         TIMES:  ; horrible function to multiply two numbers
    
                 ; expects numbers in ________________________
    
                 ; returns result in _________________________
    
                 ; wipes out _________________________________
                    
                 TESTR   R3
    	     BZS     TIMQT
                 STORES  R1,R2
                 STORE   R4,R2,TIMBSV
                 ADDI    R4,R3,-1
                 LOAD    R3,R2,TIMBSV
                 ADDI    R2,TIMAR
                 JSR     R1,TIMES
                 ADDI    R2,-TIMAR
                 LOAD    R4,R2,TIMBSV
                 ADD     R3,R3,R4
                 LOADS   R1,R2
         TIMQT:  JUMPS   R1
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

  5. Hawk Programming: It turns out that it is possible to do arithmetic directly on strings of 4 ASCII digits packed into a 32 bit register with the least significant digit in the low byte of the register (backwards from the usual HAWK order, a minor problem in I/O! To add two ASCII numbers A and B in this format, do the following magic:
       T1 = A + B + 0x96969696
       T2 = T1  &   0x30303030
       T3 = T1 - (T2 >> 3)
       SUM = (T3 & 0x0F0F0F0F) | 0x30303030
    
    (The above code is given in C, where the & operator means bit-by-bit Boolean and, the >>3 operator means right shift 3 bits, and a leading 0x on a constants means that the radix is hexadecimal.) Write a properly commented SMAL Hawk procedure called ASCIIADD that uses this algorithm and conforms to the header comments given. You do not need to understand how or why the algorithm works to solve this problem! (4 points)
         ;----------------------------
         ASCIIADD:       ; add 2 4-digit ASCII numbers.
                         ; given A and B in R3 and R4
    		     ; returns A+B in R3
    		     ; may destroy R4-7
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________