Assignment 3, Solutions

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

  1. Background: Consider this bit of SMAL code that defines a constant data structure in RAM:
            NULL	=	0
    
            A:      W       B
                    W       Aend - Abegin
            Abegin: ASCII   "A string"
    	Aend:
    
            B:      W       C
                    W       Bend - Bbegin
            Bbegin: ASCII   "begins with a"
    	Bend:
    
            C:      W       NULL
                    W       Cend - Cbegin
            Cbegin: ASCII   "character"
    	Cend:
    

    a) If words are required to be aligned in memory, what align directives must be added to the above code? (0.4 points)

    See above. The align directive before C: is required because the previous string does not contain a multiple of 4 characters.

    b) If words are required to be aligned in memory, what additional align directives should be added to the above code? (0.4 points)

    See above. An align directive before A: might be required depending on what came before this. If there is any doubt about the context, it should be included.

    The align directive before B: is not required because the previous string contained an exact multiple of 4 characters. If anyone ever edits this string, they will cause trouble if this align directive is missing.

    c) Assemble the above code using the SMAL assembler, with the ALIGN directives you believe are appropriate in answer to parts a) and b). Directives you believe are a, mandatory or b, merely recommended should be commented as such. (0.4 points)

    A properly commented assembly listing for the above code is a sufficient answer for parts a), b) and c) of this problem.

    SMAL32 (rev 10/08)              t.                           16:20:39  Page  1
    
                                 1  USE     "hawk.macs"
                                 2
                                 3  NULL    =       0
                                 4
                                 5          ALIGN   4       ; recommended
    +000000:+00000010            6  A:      W       B
    +000004: 00000008            7          W       Aend - Abegin
    +000008: 41  20  73  74      8  Abegin: ASCII   "A string"
             72  69  6E  67
                                 9  Aend:
                                10
                                11          ALIGN   4       ; recommended
    +000010:+00000028           12  B:      W       C
    +000014: 0000000D           13          W       Bend - Bbegin
    +000018: 62  65  67  69     14  Bbegin: ASCII   "begins with a"
             6E  73  20  77
             69  74  68  20
             61
                                15  Bend:
                                16
                                17          ALIGN   4       ; required
    +000028: 00000000           18  C:      W       NULL
    +00002C: 00000009           19          W       Cend - Cbegin
    +000030: 63  68  61  72     20  Cbegin: ASCII   "character"
             61  63  74  65
             72
                                21  Cend:
                                22
                                23          END
                        no errors
    

    d) The data structure starting at A: in the above code can be described as consisting of several objects, some of which may have several components. In English, give a brief description of both the nature of these objects and how they are connected. An adequate answer can be written in well under 50 words. Answers that are excessively long may be penalized. (0.4 points)

    It is a linked list of three items, since each item contains the address of the next item in the list. Each item begins with two words, the address of the next item and the length of a string. Following these two words are the bytes of the string.

    e) The top-level objects in the above example could be several of a single class or structure. We don't know the methods of that class, but we can describe the structure of instances of this class. Give a declaration of this class or structure (omitting the methods, since we don't know them) in any high-level programming language you know that is sufficiently expressive to solve this problem. (0.4 points)

    type thingus = record
                      next: ^ thingus; { a pointer to a thingus }
                      length: integer;
                      string: packed array [0 .. length-1] of char;
                   end record
    

  2. Background: Consider this bit of SMAL code that defines a constant data structure in RAM:
    	X	=	.
    	.	=	. + 4
    	Y	=	.
    		ASCII	"ABCDE"
    	Z	=	.
    	.	=	X
    		W	Z - Y
    	.	=	Z
    
    Assume that the initial value of the location counter is 100016.

    a) What are the values of the symbols X, Y and Z? (0.5 points)

    X = 0
    Y = 4
    Z = 9
    

    b) Show the contents of the memory locations changed by this code. Give your answer in hexadecimal, showing memory as a sequence of consecutive 32-bit words. (0.5 points)

    000000: 00000005
    000004: 44434241
    000008: ??????45