Assignment 5 Solutions

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

  1. Background: In the notes for Chapter 5, a call to the PUTS monitor routine was given as:
    ;  --- begin aplication code ---
            LIL     R3,HELLO
            LIL     R1,PUTS
            JSRS    R1,R1        ; puts(HELLO)
    ;  --- end aplication code ---
    

    In class, the following alternative code was given that works equally well:

    ;  --- begin aplication code ---
            LIL     R3,HELLO
            LIL     R4,PUTS
            LIL     R1,RET
            JUMPS   R4           ; puts(HELLO)
    RET:
    ;  --- end aplication code ---
    

    A Problem: Define a macro called MYJSRS that uses the combination of LIL and JUMPS that was given in class, so that this code would work:

    ;  --- begin aplication code ---
            LIL     R3,HELLO
            LIL     R4,PUTS
            MYJSRS  R1,R4        ; puts(HELLO)
    ;  --- end aplication code ---
    

    Feel free to test your result, but all you need to turn in is the 4 lines of SMAL code that define the macro. Handwriting is good enough, if it is legible. (0.5 point)

    This is the obvious solution:

            MACRO   MYJSRS =dst,=src
              LIL   dst,.+6
              JUMPS src
            ENDMAC
    

    This also works:

            MACRO   MYJSRS =dst,=src
              LEA   dst,.+6
              JUMPS src
            ENDMAC
    

    In either case, the .+6 is there so that the return from the called subroutine will be to the instruction immediately after the JUMPS. The LIL and LEA instructions are 4 bytes long, while the JUMPS instruction is 2 bytes long.

  2. Background: The Hawk monitor passes two parameters to the main program when it calls it: It passes the width of the terminal window in R3, and the height in R4 (or at least, the height of what's left in the bottom of the window after it filled the top with the registers and whatnot that it displays).

    You can take for granted the fact that the instruction SR R5,1 will divide the value stored R5 by 2, and this works for any register,

    A Problem: Write SMAL Hawk code to output an x at approximately the center of the screen. This will require calls to two monitor routines. One to set the screen coordinates and one to output the character. This takes a total of about 7 machine instructions, excluding the 'boilerplate' code from the main program. Turn in just those 7 instructions (legible handwriting is good enough), but you are free to substitute your code into the Hello World program in order to test it. (0.7 point)

    ; begin applications code
            SR      R3,1	; parameter
            SR      R4,1	; parameter
    	LIL	R1,SETAT
    	JSRS	R1,R1	; call setat( width/2, height/2 )
    
            LIS     R3,'x'	; parameter
    	LIL	R1,PUTCHAR
    	JSRS	R1,R1	; call putchar( 'x' )
    ; end applicatoins code
    

  3. Background: Look at the last example given in Chapter 5 of the notes. There are two branch instructions there, BGE ENDLOOP and BR LOOP

    A Problem: Give the binary code for these two instrucitons. (0.8 points, 0.4 each)

    Suggestion: Do it by hand first! Then check your work by assembling the code.

    First, carefully counting the halfwords and then looking up the instructions in the Hawk reference manual gives this:

    0000001100001101 or 030D   BGE     ENDLOOP ; goes forward 3 halfwords
    1111000100000000 or F100   BR      LOOP    ; goes back 15 halfwords
    

    Second, pasting the code from the notes into an empty file and then adding the required USE directives lets you assemble it. Here are the key results of that effort.

    +0000001A: 0D  03               18          BGE     ENDLOOP
                                    19
    +0000001C: 18  C1               20          ADDSI   R8,1
    +0000001E: 19  C1               21          ADDSI   R9,1
    +00000020: 00  F1               22          BR      LOOP
    

    The first solution given above shows the result as a pair of 16-bit halfwords. The second shows the consecutive bytes separately, least significant byte first. They show the same values for the two branch instructions.

  4. Background: Consider this bit of code in a high-level language:
    	if (a < b) {
    		c = a;
    		a = b;
    		b = c;
    	}
    

    A Problem: Give an equivalent fragment of SMAL Hawk code, assuming that the variable a is R3, b is R4, and c is R5. this code takes about 5 machine instructions. (0.5 points)

            CMP     R3,R4
    	BGE     ENDIF   ; if (a < b) {
    	MOVE    R5,R3   ;   c = a
    	MOVE    R3,R4   ;   a = b
    	MOVE    R4,R5   ;   b = c
    ENDIF:                  ; }
    

  5. Background: There are 4 commonly used Hawk instructions for loading registers from memory: LOADS, LOADSCC, LOAD and LOADCC. Broadly speaking, these four alternatives reflect just two features that may either be used or unused.

    a) One feature has to do with the addressing mode. What are the two alternatives? (0.3 points)

    Short indexed addressing, which uses the contents of the index register as the effective address, and (full) indexed addressing, which adds a displacement to the contents of the index register to compute the effective address.

    b) What is the other feature? What are the two alternatives? (0.2 points)

    Either do not set the condition codes to report on the result, leaving them unchanged, or set the condition codes to tell about the result.