Exam 2 Solutions

22C:18, Spring 1996, 13 points

Douglas W. Jones
  1. Fixed point numbers. Here is the complete table:
        Base 2           Base 8           Base 10           Base 16
    
       1110.1011         16.54             14.6875            D.B
                                              
        101.010           5.2               5.25              5.4
    
       1010.0111         12.34             10.4375            A.7
    
     111001.11           74.6              57.75             39.C
    
       1010.111          12.7              10.875             A.E
    

  2. Floating point numbers: Given the following representation:
                             _ _ _ _ _ _ _ _
                            |_|_|_|_|_|_|_|_|
                            |s|  exp  | man |
                               biased 2's comp
                                                       7
        The largest positive is   0 1111 111 = .875 x 2    = 112
                                                     7
        The most negative is      1 1111 000 = -1 x 2      = -128
                                                       -8
        The smallest positive is  0 0000 001 = .125 x 2    = .0004882812
                                                        -8
        The largest negative is   1 0000 111 = -.125 x 2   = -.0004882812
    
    Note that, depending on how you normaliz the numbers, there are other sensible answers to the final three parts. Full credit was given for these alternatives.

  3. Programming with fixed point arithmetic: Given that all variables in the problem are represented as 16 bit fixed point two's complement binary numbers with with 7 bits of fraction, the pre and postconditions are bridged as follows:
         ; A is in D0.W.
         ; B is in memory, pointed to by A0.
         ; C is in field F of a record in memory,
         ;     A1 points to the record,
         ;     the symbol F gives the offset of the field.
    
                MULS.W   D0,(A0)    ; compute A*B
                ARS.L    #7,D0      ; align the point
                ADD.W    (A0)
                ADD.W    (A0)       ; compute A*B + 2*B
                ADD.W    F(A1)	; compute result
    
         ; D1.W contains A*B + 2*B + C
    

  4. Procedures and functions! The fully commented heading is:
         FIBO:
         ; function to compute the Nth fibonacci number.
         ; FIBO(N) = FIBO(N-1) + FIBO(N-2)
         ; FIBO(1) = 1
         ; FIBO(0) = 0
         ; accepts N in D0
         ; returns FIBO(N) in D0
         ; destroys D1
         ; preserves or does not use D2-D7,A0-A7,SP
    

  5. M68000 Programming: The following C code adds 2 5-digit 6-bit BCD numbers using 32 bit binary arithmetic:
       T1 = A + B + 06666666666;
       T2 = T1  &   06060606060;
       SUM = T1 - (T2 | (T2 >>3));
    
    In M68000 assembly language, this is:
         BCDADD:
         ; add 2 5-digit BCD numbers, packed 1 digit per 6 bits
         ; given A and B in D0 and D1
         ; returns A+B in D0
         ; preserves all other registers
    
    	MOVE.L  D1,-(SP)	; save registers
    	ADD.L   #@6666666666,D0
    	ADD.L   D1,D0		; D0 = D0 + D1 + @6666666666
    	MOVE.L  #@6060606060
    	AND.L   D0,D1		; D1 = D0 & @6060606060
    	SUB.L   D1,D0		; D0 = D0 - D1
    	ARS.L   #3,D1
    	SUB.L   D1,D0		; D0 = D0 - (D1 >> 3)
    	MOVE.L  (SP)+,D1	; restore registers
            RTS