Exam 2 Solutions

22C:18, Summer 1997, 12 points

  1. Fixed point numbers. The following table is completed:
        Base 2          Base 10    |    Base 2          Base 10          
                                   |
       1110.1011         14.6875   |    
                                   |  _0011.1101_         3.8125
       1101.0101      ___13.3125_  |  
                                   |  _1001.1001_         9.5625
       0111.1001      ____7.5625_  |
    

  2. Floating point numbers: Our number system:
                             _ _ _ _ _ _ _ _
                            |_|_|_|_|_|_|_|_|
                            |s| exp |  man  |
    
    1. What is the largest possible exponent? ___3_______

    2. What is the smallest possible exponent? _-4________

    3. What is the largest possible mantissa? __0.96875__

    4. What is the smallest positive normalized mantissa? _0.5_______

    5. What is the largest positive number in this system? _0.96875 x 23

    6. What is the smallest positive number in this system? _0.5 x 2-4
  3.      ; A is in R3, an 8 digit fixed point unsigned BCD number;
         ; A has 2 digits of fraction and 8 digits of integer part!
         ; B is pointed to by R8; B is a BCD integer.
         ; assume that BCDTOI and ITOBCD converts between 8 digit BCD
         ; numbers and binary integers in R3, and that they know nothing
         ; about fixed point numbers!
    
               CALL    BCDTOI    ; convert R3 to binary
    
               _ADDSL___R3,R3,2____________
    
               _SL______R3,1_______________
    
               CALL    ITOBCD    ; convert R3 back to decimal
    	     
         ; R3 now contains 10*A
    
               _ADDI____R3,#50_____________
    
               _BCDADJ__R3_________________
    
               _SRU_____R3,8_______________
    
         ; R3 now contains 10*A rounded to the nearest integer
    
               _STORES__R3,R8______________
    
         ; B now contains 10*A, in the format required for B
    

  4. 	; A is a common holding a global array of integers;
    	; AP is the label on a word pointing to A[0].
    	;
    	; I is a common holding an integer;
    	; IP is the label on a word pointing to I.
    	;
    	; I is known to be within bounds for indexing into A.
    	;
    	; B is the displacement from R2 of a local array of
    	; integers.  B points to location 0 of that array.
    	;
    	; J is the displacement from R2 of a local integer.
    	;
    	; J is known to be within bounds for indexing into B.
    	
               _LOAD____R4,AP______________
    
               _LOAD____5,IP_______________
    	
               _LOADS___R3,R5______________
    
               _ADDSL___R3,R4,2____________
    	
               _LOADS___R3,R3______________
    
    	; R3 holds the value of A[I]
    
               _LEA_____R4,R2,B____________
    	
               _LOAD____5,R2,J_____________
    
               _ADDSL___R5,R4,2____________
    	
               _STORES__R3,R5______________
    
    	; B[J] holds the value of A[I]
    
  5.      ; --------------------
         ; R2 points to AR with following format
         ;RA=    0  ; return address
         TIMEX=  4  ; extra value to add
         AR=     8  ; size of activation record
    
         TIMES:  ; horrible function to multiply two words
                 ; algorithm:  A x B = if B zero 0
                 ;                   = if B even B/2 x (2 x A)
                 ;                   = if B odd (B/2 x (2 x A)) + A
                 ; expects argument in R3, R4.
                 ; returns result in R3.
                 ; wipes out R5
    
    	     TEST    R4
    	     BZS     TIMQT     ; quit if B is zero
    
                 _STORES__R1,R2___ ; save return address
    
    	     MOVE    R5,R3     ; \
    	     MOVE    R3,R4     ;  > swap A and B
    	     MOVE    R4,R5     ; /  (R5 ends up a copy of A)
    	     SRU     R4	       ; get B/2, set C if B odd
    	     BCS     TIMODD
    	     LIS     R5,0      ; if B even, R5=0 else R5=A
         TIMODD:
    	     _STORE___R5,R2,TIMEX; set aside R5 as extra value
    
    	     SL	     R3,1      ; get 2xA
    
    	     _ADDI____R2,AR___ ; \
    
    	     _JSR_____R1,TIMES ;  > recursive call
    
    	     _ADDI____R2,-AR__ ; /
    
    	     _LOAD____R5,R2,TIMEX; Recover extra value in R4
    
    	     ADD     R3,R3,R4  ; add it to return value
    
                 _LOADS___R1,R2___ ; recover return address
    
         TIMQT:  JUMPS   R1