Assignment 5, Solutions

Part of the homework for 22C:60 (CS:2630), Spring 2012
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

  1. Background: Consider the assignment for Machine Problem 2. That assignment requires that you output a sequence of integers, with a single space between successive integers.

    a) What Hawk monitor routines are relevant to this assignment? (0.5 points)

    PUTDECU - for output of the integer calculated
    PUTCHAR - for output of the space needed between integers

    b) What parameters must you pass to each of these routines? (For the purpose of this question, the integer you want to output is the variable i.) (0.5 points)

    PUTDECU(i,1)
    -- R3 = i, the integer to output
    -- R4 = 1, the field width (0 will also work)

    PUTCHAR(' ')
    -- R3 = ' ', the blank to output

    Note that PUTDECU (and also PUTDECU) will output the number, and if the number requires fewer than the indicate field width, will output extra blanks to pad the output to the indicated width. So, specifying 2 would always take at least 2 characters, outputting too many blanks between the lower integers in the sequence.

  2. Background: Consider this fragment of high-level language code:
    while (i > 1) do {
       if (i is even) {
          i = i / 2;
       } else {
          i = (3 * i) + 1;
       }
    }
    

    Note: To solve this, assuming that the variable i is in R3, you will need to use the BITTST R3,0 instruction to test the least significant bit of R3 and either BBR or BBS to do a conditional branch on the result of the test. You can divide R3 by two using SR R3,1 and you can multiply by three by repeated addition.

    A problem: Give an equivalent fragment of SMAL Hawk code. The total number of instructions required is approximately ten, with about 5 labels. (1.0 points)

    ;  --- begin aplication code ---
            LIS     R8,25	     ; i = 25 -- for example, to test the code
    LOOP:		             ; loop {
    	CMPI	R8,1
    	BLE	LOOPQT       ;   if (i <= 1) break
    
    	BITTST	R8,0
    	BBR	ELSE	     ;   if (i is odd) {
    
    	ADDSL	R8,R8,1	     ;     -- can also use 2 ADDs for *3 
    	ADDSI	R8,1	     ;     i = 3*i + 1
    
    	BR      ENDIF
    ELSE:			     ;   } else { -- i is even
    
    	SRU	R8,1	     ;     i = i/2
    
    ENDIF:			     ; }
    	MOVE	R3,R8		 -- optional code to
    	LIS	R4,4             -- observe the sequence
            LIL     R1,PUTDECU	 -- of values computed
            JSRS    R1,R1        ;   putdecu(i,4)
    	BR	LOOP         ; }
    LOOPQT:  
    ;  --- end aplication code ---
    

    Note: The above solution is exactly 10 instructions, if the optional instructions for testing the code are deleted.

  3. Background: When the Hawk monitor calls your main program, it passes parameters: the height and width of the display screen (this is documented in Chapter 5 of the notes). The instruction SR R3,1 divides R3 by two.

    A problem: Write the application code of a main program for the Hawk that uses PUTAT and PUTCHAR to display an X in the center of the screen. (1.0 points)

    You are welcome to substitute your code into the skeleton of a main program in order to try it, but please do not turn in the boilerplate code for the working program. Just turn in the 6 or so lines of code that do the work.

    ;  --- begin aplication code ---
    	SR	R3,1	    ; -- paramter: width/2
    	SR	R4,1	    ; -- parameter: height/2
    	LIL	R1,PUTAT
    	JSRS	R1,R1       ; putat( width/2, height/2 )
    
    	LIS	R3,'X'	    ; -- parameter: 'X'
    	LIL	R1,PUTCHAR
    	JSRS	R1,R1	    ; putchar( 'X' )
    ;  --- end aplication code ---