| Assignment 10, Solutions
    
     Part of 
      
      the homework for 22C:60, Fall 2009
      
     
      | 
A question: Explain why long comparison has the potential to be faster than subtraction. (0.5 points)
To subtract, you always have to process both words of each operand. In comparison, you can conclude that one operand is greater than the other after comparing just the most significant words. Only if the most significant words differ do you need to compare less significant words.
a) Assuming that X, Y, Z are local variables, each holding a long integer, write Hawk code equivalent to the following expression in a high level language: X = 100*Y + Z. In your code, take maximal advantage of the background information presented above. Your code must, of course, be well commented. (0.5 points)
                LEA    R3,R2,X
                LIS    R4,100
                ADDSI  R2,R2,ARSIZE
                LIL    R1,INT2LONG
                JSRS   R1,R1
                ADDSI  R2,R2,-ARSIZE    ; X = 100
                LEA    R4,R2,Y
                ADDSI  R2,R2,ARSIZE
                LIL    R1,LONGMUL
                JSRS   R1,R1
                ADDSI  R2,R2,-ARSIZE    ; X = 100*Y
                LEA    R4,R2,Z
                ADDSI  R2,R2,ARSIZE
                LIL    R1,LONGADD
                JSRS   R1,R1
                ADDSI  R2,R2,-ARSIZE    ; X = 100*Y + Z
        S E E E M M M M
         S - the sign of the mantissa
         E E E - the 3-bit exponent, biased
         M M M M - the 4-bit mantissa, with a hidden 5th bit
The normalization rule is the common one for binary floating point numbers: the mantissa is strictly less than one and greater than or equal to 0.5, so that the hidden bit is always 1 -- and therefore, need never be stored. This number system has no NaNs and no unnormalized values, so the exponent values run over the entire range from 000 to 111.
a) What is the largest value that can be represented in this number system? Give both the binary representation and the decimal equivalent. (0.3 points)
0 111 1111 = 0.11111 * 2**3 = 111.11 * 2**0 = 7.75
b) What is the binary representation of 1.0 in this number system? (0.2 points)
0 101 0000 = 0.10000 * 2**1 = 1.0 * 2**0 = 1.0
c) What is the smallest positive nonzero value that can be represented in this number system. Give both the binary representation and the decimal equivalent. (0.3 points)
0 000 0000 = 0.10000 * 2**-4 = 1.0 * 2**-5 = 0.03125
d) Explain the problem this number system poses for the representation of zero? (0.2 points)
There is no representation for zero because the number system is defined with a hidden bit that is always one.
                MOVE    R4,R3	; setup to compute the exponent in R4
                SR      R4,4    ;   discard the mantissa
                ADDSI   R4,-4   ;   convert from biased to 2's complement
                MOVE    R5,R3	; setup to compute the mantissa in R5
                LIS     R6,#0F  ;   mask
                AND     R5,R6   ;   clear all but mantissa bits
                LIS     R6,#10  ;   hidden bit
                OR      R5,R6   ;   set hidden bit
                BITTST  R3,7    ;   test sign
                BCR     NOTNEG
                NEG     R5,R5   ;   negate mantissa if needed
        NOTNEG: