Exam 3

22C:18, Spring 1996, 13 points

Douglas W. Jones
Name ______________________________________   Section ___________


Some problems in this exam may rest on the subroutines NEW and DISPOSE. These may be called as follows: NEW and DISPOSE preserve all registers but A0; both change A0. If D0 is positive, A0 points to the byte in the block with the least address. If D0 is negative, A0 points to the byte just beyond the byte in the block with the greatest address.

  1. Number Representations: Give representations for "50" in each of the following forms (6 points):
         a) as ASCII text shown in hexadecimal   _____________
    
         b) as a BCD number shown in binary      _____________
    
         c) as a binary number                   _____________
    
         d) as an 8-bit two's complement binary
            number shown in hexadecimal          _____________
    
         e) as an 8-bit unsigned fixed point
            binary number with 2 places after
            the point, shown in hexadecimal      _____________
    
         f) as a 16-bit floating point number
            with a 10-bit signed-magnitude
            normalized binary mantissa and a
            5 bit biased exponent, shown in
            hexadecimal                          _____________
    
    
    
    
    
    
    
    
    
    
    

  2. Calling Sequences: Consider the following bit of pseudocode:
    		function add(a,b -- both 32-bit integers)
    		  returns a + b
    
    Write M68000 code for the function ADD using each of the following calling sequences:

    Part A: A and B are passed in D0 and D1, the result is returned in D0. Any other registers that must be modified are restored prior to return (2 points).

    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
    Part B: The stack is used for all parameter passing. The calling program pushes first A then B then the return address. The result is returned on the stack top, with all other remains of the function removed from the stack. The function saves and restores any registers it needs to modify (2 points).
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
    Part C: ADD expects a pointer to its activation record to be passed in A0. The caller must allocate this activation record and initialize the fields ADDA and ADDB prior to the call. The result is returned in field ADDRES on exit, and the activation record includes a field ADDRET that must hold the return address during the execution of the body of ADD. Use of SP is avoided where not absolutely required by the above. The caller is responsible for deallocating the activation record. The called function must save and restore any registers it modifies; ADDSV is a field of the activation record sufficiently large to hold any registers the function must save (2 points).
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
    Write M68000 code to call the function ADD using each of the above calling sequences with the actual parameters taken from the variables A and B, and the result stored in the variable C. All of these variables are statically allocated!

    Part D: A and B are passed in D0 and D1, the result is returned in D0. Any other registers that must be modified are restored prior to return (2 points).

    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
    Part E: The stack is used for all parameter passing. The calling program pushes first A then B then the return address. The result is returned on the stack top, with all other remains of the function removed from the stack. The function saves and restores any registers it needs to modify (2 points).
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
    Part F: ADD expects a pointer to its activation record to be passed in A0. The caller must allocate this activation record and initialize the fields ADDA and ADDB prior to the call. The result is returned in field ADDRES on exit, and the activation record includes a field ADDRET that must hold the return address during the execution of the body of ADD. Use of SP is avoided where not absolutely required by the above. The caller is responsible for deallocating the activation record. The called function must save and restore any registers it modifies; ADDSV is a field of the activation record sufficiently large to hold any registers the function must save. Assume that ADDSZ is the size of the activation record for add (3 points).
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    

  3. Record definitions: Give symbolic definitions in M68000 assembly language for the record fields and record size used by parts c and f above (3 points).
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
    

  4. Recursion: Using the calling sequence of your choice, write M68000 assembly code equivalent to the following pseudocode, making sensible assumptions about the definition of symbolic names for the fields of the records pointed to by the parameter t (3 points).
    	function count(t)
    	  if t=nil return 1 immediately
                otherwise,
                return count(t^.left) + count( t^.right)
    
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________
    
         _____________________________________________________