Exam 1: Midterm

Solutions and Commentary

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

Grade Distributions

Midterm Exam 1

Mean   = 7.39                          X
Median = 7.8                     X     X X X
                                 X     X X X
                                 X     X X X
                               X X     X X X
                     X   X X X X X   X X X X
                   X X   X X X X X   X X X X
   ____________X___X_X_X_X_X_X_X_X_X_X_X_X_X_X__
    0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10. 

Machine Problems 1 - 2

Mean   =  8.29                               XXXX
Median = 10.0                                XXXX
                                             XXXX
                                             XXXX
                                             XXXX
                         X               X   XXXX
                         X               X   X
                         X               X   X
     X                   X X             X   X
     X                   X X X           X   X
   __X_X_X___________X___X_X_X_X_______X_X___X__
    0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10. 

Homeworks 1 - 6

Mean   = 12.34                                               X
Median = 13.4                                  X X         X X
               X   X       X             X     X X   X   X X X   X
               X   X   X   X   X   X   X X     X X   X   X X X   X
   ____X_______X_X_X___X___X___X___X_X_X_X_X___X_X_X_X_X_X_X_X_X_X_X__
    . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10. 11. 12. 13. 14. 15. 16. 17. 18.

Total Scores

Mean   = 27.57                                                     X
Median = 29.3                                                      X
                                                                   X
                                                         X   X     X
                                                         X   X     X
                                         X X     X X     X   X     X
             X                   X     X X X     X X   X X   X X X X   X
   ________X_X___X___________X___X_X_X_X_X_X_X___X_X___X_X_X_X_X_X_X_X_X_______
    4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30. 32. 34. 36. 38. 40
Midterm Grade Scale        F    |    D    |    C    |    B    |    A    |

Solutions and Commentary

Note: There were two versions of this exam that differed in Problems 1, 2 and 4.

  1. Fill in the following table; values in the blanks in each column derive from the given values in that column, all values in each row are derived from the given values using the same rules. (4 points)
    Bit Pattern x         110010      001001      100101     010111     110011
    
    Unsigned decimal     ___50___    ____9___    ___37___   ___23___   ___51___
    
    1's complement of x  _001101_    _110110_    _011010_   _101000_   _001100_ 
       (binary)
    2's complement of x  _001110_    _110111_    _011011_   _101001_   _001101_
    
    x as a 1's comp num  __-13___    ____9___    __-26___   ___23___   __-12___
       (signed decimal)                                      
    x as a 2's comp num  __-14___    ____9___    __-27___   ___23___   __-13___
    
    

    1 in 4 did perfect work here. A number of students made various clerical errors. About 1 in 4 had serious problems with signed number representations. The most common error in this category involved the mindless negation of all signed values, even if the sign was positive, but many students gave apparently random results.

    The solution presented above is version B of the exam. In the original, one entry in each row and column was pre-solved so that students had one last chance to learn how to do it correctly while working the exam.

  2. Translate the following brief useless SMAL code to a sequence of bytes loaded in memory. The spaces to the right are labeled with a word and byte in word addresses; fill in the hexadecimal value that goes in that byte; leave uninitialized bytes blank. The result of assembling the first lines are given. (2 points)
        . = 0                       ||  Address    3    2    1    0
        A:  H     #4321             ||          |----|----|----|----|
        B:  B     #65,#87           ||  000000: | 87 | 65 | 43 | 21 |
            ; solved to here        ||          |----|----|----|----|
            ALIGN 2                 ||  000004: |    | 0D | 08 | 42 |
            B     "B",C             ||          |----|----|----|----|
            B     #D                ||  000008: | 00 | 0A | 00 | 00 |
            ALIGN 4                 ||          |----|----|----|----|
        C:  H     A,#A              ||  00000C: | 0C | 08 | 02 | 00 |
            ALIGN 4                 ||          |----|----|----|----|
        D:  B     A,B,C,D           ||  000010: |    |    |    |    |
            END                     ||          |----|----|----|----|
                                    ||  000014: |    |    |    |    |
                                    ||          |----|----|----|----|
                                    ||  000018: |    |    |    |    |
                                    ||          |----|----|----|----|
                                    ||  00001C: |    |    |    |    |
                                                |----|----|----|----|
    
    
    

    1 in 5 did perfect work here. 1 in 8 gave answers that were total nonsense, and 1 in 6 gave answers that earned almost no credit. There were two major classes of problems: First, students who did not understand labels. For example students looking at the above code associated the value #4321 with A instead of the address zero. Second, some students did not understand the ALIGN directive. For example, some students assumed that forced all successive lines to be aligned, as opposed to inserting a one-time padding to force the indicated alignment.

    The solution presented above is version A of the exam. In both versions, the solution to (bytes 00 to 03 of the answer) was given on the exam, so that students had a last-minute chance to learn something about how the assembler worked.

  3. a) A small Hawk subroutine is given below to the right. Assemble this into the halfword memory locations given to the left below. The first instruction is worked for you. (1.5 points)
        Addr:     Val        ||    .       =       #1000
        1000:  ___A2F1___    ||    
                             ||    FUNCT:
        1002:  ___3420___    ||            STORES  R1,R2
                             ||            CMP     R3,R4
        1004:  ___0306___    ||            BLE     FUNXT
                             ||            ADD     R3,R3,R4
        1006:  ___3433___    ||            SUB     R4,R3,R4
                             ||            SUB     R3,R3,R4
        1008:  ___3424___    ||    FUNXT:
                             ||            LOADS   R1,R2
        100A:  ___3423___    ||            JUMPS   R1
                             ||
        100C:  ___D2F1___    ||
                             ||
        100E:  ___B1F0___    ||
                             ||   
        1010:  __________    ||
                             ||   
        1012:  __________    ||
                             ||   
        1014:  __________    ||
    
    
    

    1 in 6 gave perfect answers. Small clerical answers were quite common, and 1 in 10 left the entire problem blank. (It was an open book exam, perhaps those students forgot to bring their book? But note, students who knew anything about the Hawk architecture should have been able to fill in the register fields of the instructions without a book, earning roughly half credit). The most common errors had to do with computing the branch displacement for the BLE instruction. 1 in 5 who did the rest of the problem reasonably well could not do this. The second most common problem among those who generally did well was to reverse the byte order. 1 in 12 who did the rest reasonably made this error.

    b) It is a subroutine. If FUNCT is called with the integers i in R3 and j in R4, what values are returned? (The answer is very short.) (0.5 points)

    __It_sorts_things_so_i_<=_j_(R3<=R4)____
    

    1 in 12 got this. 1 in 15 concluded that the code swaps the values of i and j, earning 0.3 points. 1 in 6 left earned no credit while giving odd and confusing answers. 1 in 10 left this blank.

    Admittedly, this code uses a trick for exchanging the values of two variables without using a third, temporary variable. Aside from this, though, the body of the routine is directly based on that from the solution to problem 4 on homework assignment 5, so it should have looked at least marginally familiar.

  4. The following SMAL Hawk code solves MP3 for a different version of the data structure than the one assigned for the problem -- the version where each tree node looked like this:
    DATA    =       0       ; word 0, points to the string in this node
    COUNT   =       4       ; word 1, how many array elements
    ARRAY   =       8       ; word 2, the first array element
                            ; additional words if count requires them
    

    The following program was somewhat optimized before parts of it were carefully cut out and replaced by blanks. Fill in the missing parts. (2 points)

            SUBTITLE "Traverse the tree and print in preorder"
    ; activation record
    ;RETAD  =       0       ; return address
    SAVER8  =       4       ; saved copy of R8
    SAVER9  =       8       ; saved copy of R9
    ARSIZE  =       12
    
    TRAVERSE:       ; expects R3 = p, a pointer to a tree node
                    ; may use R4-R7
            STORES  R1,R2           ; -- save return address    (blank in A)
            STORE   R8,R2,SAVER8    ; -- save R8
            STORE   R9,R2,SAVER9    ; -- save R9
            ADDI    R2,R2,ARSIZE    ; -- push activation record (blank in B)
            MOVE    R8,R3           ; -- from here on, R8 is p
            LOAD    R3,R8,DATA      ; -- parameter
            LIL     R1,PUTS
            JSRS    R1,R1           ; puts( p->data )
            LOAD    R9,R8,COUNT     ; c = p->count
            LEA     R8,R8,ARRAY     ; p = &p->array[0]
    TRAVLP:                         ; for (;;) {
            ADDSI   R9,-1           ;   c = c - 1               (blank in B)
            BLE     TRAVQT          ;   if (c <= 0) break       (blank in A)
            LOADS   R3,R8           ;   -- parameter
            JSR     R1,TRAVERSE     ;   traverse( *p );
            ADDSI   R8,4            ;   p = p + 4               (blank in A)
            BR      TRAVLP          ; }                         (blank in B)
    TRAVQT:
            ADDI    R2,R2,-ARSIZE   ; -- pop activation record  (blank in A)
            LOAD    R8,R2,SAVER8    ; -- restore R8
            LOAD    R9,R2,SAVER9    ; -- restore R9
            LOADS   R1,R2           ; -- restore return address (blank in B)
            JUMPS   R1              ; return
    

    1 in 5 did perfect work here. Branch instructions posed werious problems for 1 in 3 students, while load and store instructions psed serious problems for 1 in 8.

    Both versions of the exam were based on the above code, with 4 blank lines added as indicated by the comments to the right. In each case, the instructions omitted were carefully chosen so that filling in the blanks would be equal in difficulty.

    The code here is fairly tightly optimized, but the fill-in-the-blanks questions did not involve the optimization. The purpose of this is to avoid providing a template for a solution to Machine Problem 3, since untangling the difference in problem specification between the version solved by this code on the exam and the version assigned for the machine problem is significantly complicated by the optimizations. Nonetheless, those students studied the machine problem before the exam, as was suggested, should have done better on this problem than those who came at it cold. In addition, the size and top-level structure of the code presented on this exam question should serve as a hint about the size of the code that can solve the assigned machine problem.