Assignment 6 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: As announced in class, and as recommended by the department of computer science, this question is offered as an incentive-reward for those who attended the University of Iowa Computing Conference:

    The following countries have populations between one and one-and-a-half million people (listed in order of increasing population): Timor-Leste, Swaziland, Bahrain, Mauritus, Estonia, and Trinidad and Tobago.

    a) One of these countries was mentioned during the Friday evening keynote address. Which one? (0.2 points)

    Estonia

    b) What subject was being discussed when that country was mentioned. (0.2 points)

    Internet voting

  2. Background: Consider the following fragment of high-level language code:
            a = b;
    

    For each of the following, write a fragment of SMAL Hawk code that is equivalent to the above.

    In each case, assume that registers 3 to 7 are currently unused and therefore available for use as scratch registers and that register 2 points to the base of the current activation record. You may assume that the assembly language symbol A has been defined appropriately to give the address of the high-level language variable a and that the symbol B has been defined appropriately to give the address or value of associated with the high-level language identifer b.

    a) The variable a is a statically allocated global variable. the variable b is a local variable of the current subroutine (0.3 points)

            LOAD    R3,R2,B
            LIL     R4,A
            STORES  R3,R4
    

    b) The variable a is a local variable of the current subroutine and the constant b has an unknown 32-bit value. (0.3 points)

            LIW     R3,B
            STORE   R3,R2,A
    

  3. Background: Here is a classical way to multiply two non-negative integers:
    int times( int a, int b ) {
            if (a == 0) {
                    b = 0;
            } else {
                    b = times( b, a-1 ) + b;
            }
            return b;
    }
    
    The following is an incomplete translation of this to SMAL Hawk code:
    ; activation record structure for TIMES
    ;RETAD  =       0       ; the return address
    A       =       4       ; multiplier
    B       =       8       ; multiplier
    ARSIZE  =       12
    
    TIMES:  ; expects R3 = A
            ;         R4 = B
            ; returns R3 = A * B
            ; uses    R4
            STORES  R1,R2
            -- missing definitions and code --
            TEST    R2,A
            BNE     TELSE           ; if (a == 0) {
            LIS     R3,0
            STORE   R3,R2,B         ;   b = 0
            BR      TENDIF
    TELSE:                          ; } else {
            LOAD    R3,R2,B         ;   -- parameter
            LOAD    R4,R2,A
            ADDSI   R4,-1           ;   -- parameter
            ADDI    R2,R2,ARSIZE
            JSR     R1,TIMES        ;   -- call
            ADDI    R2,R2,-ARSIZE
            LOAD    R4,R2,B
            ADD     R3,R3,R4
            STORE   R3,R2,B         ;   b = b + times( b, a-1 )
    TENDIF:                         ; }
            LOAD    R3,R2,B
            LOADS   R1,R2
            JUMPS   R1              ; return b
    

    a) The above assembly code is completely free of comments! Write clear concise comments that cleanly connect the assembly code to the high-level language code from which it was derived. (1 point)

    Comments have been added above, none were originally present.

    b) Provide the missing definitions and code. (1 point)

    Everything above the missing definitions and code line was added in answer to this part.