Assignment 6, due Feb 27

Solutions

Part of the homework for CS:2630, Spring 2015
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

  1. Background: Suppose A is a constant, declared with a SMAL statement like:
         A       =       #12345
    

    Suppose B is a constant, declared with a SMAL statement like:

         B:      W       #1234567
    

    Suppose C is a global variable, declared with a SMAL statement like:

                 COMMON  C,4     ; an integer
    

    Suppose D is a local variable, declared with a SMAL statement like:

         D       =       8       ; an integer
    

    A problem: Write SMAL Hawk code to compute the sum of the values of A, B, C and D. If you were writing this in C or Python, the solution might be as simple as A+B+C+D because the language keeps track of the types of the operands. In assembly language, you might well need to use a different addressing mode for each operand. (1.2 points)

            LIL     R3,A            ; now R3 = A
            LOAD    R4,B
            ADD     R3,R3,R4        ; now R3 = A+B
            LIL     R4,C
            LOADS   R4,R4
            ADD     R3,R3,R4        ; now R3 = A+B+C
            LOAD    R4,R2,D
            ADD     R3,R3,R4        ; now R3 = A+B+C+D
    

  2. Background: The code for the Fibonacci function discussed on Feb. 18 is only partly developed. See this code. Even though this code is only beginning to be fleshed out, it contains one clearly sub-optimal bit of code.

    a) Identify the sub-optimal code. You may do this by quoting the code, along with the lines immediately before and after the less than optimal part. (0.5 points)

            BGTU    ELSE            ; if (i <= 1) {
    ;       ------- then clause -------
    	BR      ENDIF           ; } else {
    

    b) Optimize the suboptimal code. Show your result by giving the text you improved along with the lines immediately before and after it. (0.5 points)

            BLEU    ENDIF           ; if (i > 1) {
    

  3. Background: Refer to Homework 5, problem 1. That problem, in parts a) through e), walked you through writing the code to do puts(array[i]->text working with the data structure from machine problem 1. The note said that we have not covered enough to finish writing mp1.a because we have not yet discussed how to load and store halfwords.

    As it turns out, the keys to this problem are in Chapter 7, which you should start reading before the due date for this assignment. To help motivate that, consider the following question, which would have made a nice part f) for Homework 5, problem 1.

    a) Given that R10 points to one record, that is, R10 holds the value of array[i], write SMAL Hawk code to load the value of the X field of that record (array[i]->x) into R3. (0.4 points)

    First, here is the solution that follows naturally from Chapter 7:

            LEA     R4,R10,X        ; get the address of X
            LOADS   R3,R4           ; load the word holding X
    	EXTH    R3,R3,R4        ; get X
    

    We can optimize this because we know that X is zero:

            LOADS   R3,R10          ; load XY into R3
    	EXTH    R3,R3,R10       ; get the low 16 bits
    

    As is always the case, there are other solutions, consider this:

            LOADS   R3,R10          ; load XY into R3
    	TRUNC   R3,16           ; get the low 16 bits
    

    b) Given that R10 points to one record, as above, write SMAL Hawk code to load the value of the Y field of that record (array[i]->y) into R4. (0.4 points)

    Here is the solution that follows naturally from Chapter 7:

            LEA     R5,R10,Y        ; get the address of Y
            LOADS   R4,R5           ; load the word holding Y
    	EXTH    R4,R4,R5        ; get Y
    

    Optimized solutions are left to the reader.

    Having completed both of the above, you could now write a call to putat() in order to set things up to output the string at the right place on the screen.