Assignment 3 Solutions

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

  1. Background: A Java, C or C++ programmer could create a table of the squares of the integers from 0 to 8 by writing:
            int squares[]={0,1,4,9,16,25,36,49,64};
    

    Assume that integers are 32-bit values and assume that this table is a constant -- that is, that there will be no assignments to squares or to any of the table entries other than this initializer.

    The problem: Write SMAL code equivalent to this. (1 point)

                    ALIGN   4       ; recommended, no penalty if missing
            SQUARES:                ; may be on same line as first W
                    W       0
                    W       1       ; entries may be one per line
                    W       4       ; or multiple words per line (as below)
                    W       9       ; but should not be mixed like this
                    W       16,25,36,49,64
    

  2. Background: Consider maintaining the table of squares as a list, where each item in the list consists of a pointer to the next item in the list and an integer value. The last item in the list has a null pointer in its next tiem field.

    The problem: Write SMAL code to represent a constant list of the first four squares, in ascending order, starting with the square of zero. (1 point)

            NULL    =       0       ; recommended definition
                    ALIGN   4       ; recommended
                                    ; Specific label names are not determined
                                    ; but some kind of system should be used.
            HEAD:
                    W       SQUARE1 ; next
                    W       0       ; zero squared
            SQUARE1:
                    W       SQUARE2 ; next
                    W       1       ; one squared
            SQUARE2:
                    W       SQUARE3 ; next
                    W       4       ; two squared
            SQUARE3:
                    W       NULL    ; next
                    W       9       ; three squared
    

  3. Background: Consider making an array of days of the week. A C programmer could write
            const char dayname[][]={ "Sunday", "Monday", "Tuesday",
                    "Wednesday", "Thursday", "Friday", "Saturday" };
    

    This is actually a constant array of pointers to null-terminated strings, despite the fact that the declaration looks like a two-dimensional array of characters.

    The problem: Write SMAL code to represent this. (1 point)

            	ALIGN	4	; recommend
            			; After DAYNAME, label names are not
            			; determined by the assignment but some
                                    ; kind of system should be used.
            DAYNAME:
            	W	SUN,MON,TUE,WED,THU,FRI,SAT
            SUN:	ASCII	"Sunday",0
            MON:	ASCII	"Monday",0
            TUE:	ASCII	"Tuesday",0
            WED:	ASCII	"Wednesday",0
            THU:	ASCII	"Thursday",0
            FRI:	ASCII	"Friday",0
            SAT:	ASCII	"Saturday",0