Assignment 10, Solutions

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

Problems

  1. Background: In the long-integer package for mp4, there are methods for both comparison and subtraction.

    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.

  2. Background: In the long integer package from mp4, all the arithmetic routines operate on their first parameter and return that parameter as a result, so longadd(a,b) adds the long integer pointed to by b to the long integer pointed to by a and returns the pointer a.

    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
    

  3. Background: Long ago (in the 1970's) Bell Labs was experimenting with ways to digitize sound. Long term results of this included little things like MP2 and MP3, but early in the process, they talked about storing sound as a string of floating-point values, using 8 bits per value. Each floating point value in the proposed number system had the following fields:
            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.

  4. A Problem: Write a SMAL Hawk assembly code fragment that, when given an 8-bit floating-point number int the format given above in R3, breaks it up and puts the exponent field in R4, converted to a 32-bit 2's complement value, and the mantissa in R5, also converted to a 32-bit 2's complement value, with 5 places after the point. (1.0 points)
                    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: