Homework 4

22C:18, Summer 1997

Due Monday, July 7

Douglas W. Jones
  1. Write an appropriately commented SMAL Hawk assembly language program that is as close as you can come to being equivalent to the following C program:
    int funct (int x)
    {
    	if (x <= 2) {
    		return x;
    	} else {
    		int a = funct( x - 1 );
    		int b = funct( x - 3 );
    		return a + b;
    	}
    }
    	
    main ()
    {
    	int i;
    	for (i = 1; i < 8; i++) {
    		printf("%d", funct( i ) );
    	}
    }
    

  2. Many better modern compilers take expressions of the form x/const and convert them to x*(1/const). That is, they replace division by a constant by multiplication by the reciprocal of the constant. For example, to divide by 12, we multiply by 1/12; this is more efficient because, in general, multiplication by a constant can be done quickly, while division is difficult. Give fixed-point binary equivalents of 1/x for all integer values of x between 1 and 10, inclusive. Most of these will be repeating fractions, so you need not give all of the bits!

  3. Consider the following simple (but typical) binary floating point format.
             _ _ _ _ _ _ _ _ _ _ _
    	|_|_|_|_|_|_|_|_|_|_|_|
            | |       |           |
             s   exp    mantissa
    
         s =        the sign bit for the mantissa; 0 is positive
         exp =      the biased exponent, between -8 and +7
         mantissa = the fixed point binary mantissa, between 0 and .5
    
    For all integer x between 1 and 10, what is the closest approximation of the value of 1/x in this floating point number system? Show your results in binary, and show the error, as a decimal number.

  4. Write a fast version of strcpy(), the standard C routine to copy one string to another. This should, when possible, copy the string groups of 4 bytes (one word), and only copy odd bytes at the start and end of the string or if the source and destination strings are aligned differently.

  5. Write a routine to convert BCD numbers to printable ASCII; call it BCDTOA. This should take a BCD number in R3 and the address of a string in R4 and return the result in the string, 8 characters long, with leading blanks ahead of the first nonzero digit. DSPHEX would suffice if leading zeros were acceptable!