Exam 1: Midterm

Solutions and Commentary

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

Grade Distributions

Exam 1

Mean   = 4.95    X       X
Median = 4.7     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_X_X_X_X_X_X_X_X___X___X____
  0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10

Homework 1 — 4

                                               X
Mean   = 9.01                                  X
Median = 9.6                                   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_X_X_X_X_X_X_X_X_X_X____
  0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10. 11. 12

Machine Problem 1

                    XXX
                    XXX
Mean   = 4.35       XXX
Median = 4.8        XXX
                    XXX
                     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

Total Scores

Mean   = 17.78                   X
Median = 17.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_X_X_X_X_X_X_X_X_X_X___X___
  0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 

Solutions and Commentary

  1. A Problem: Fill in the following table; values in the blanks in each column derive from the given value in that column. All values in each row are 8-bit binary representations of the given decimal number in the indicated number system. (2 points)
    A decimal number: -65 13 -87 27 -76
    Binary absolute value: 010000010000110101010111 0001101101001100
    Signed-magnitude: 110000010000110111010111 0001101111001100
    1's complement: 101111100000110110101000 0001101110110011
    2's complement: 101111110000110110101001 0001101110110100
    naturally biased: 001111111000110100101001 1001101100110100

    Boldface answers above were given on the exam

    Very few got full credit; even the best students make random careless errors. A large fraction of the class had severe difficulty with one's complement, two's complement and biased numbers. In many cases, the values given verged on random.

    In general, any student who had difficulty with this problem will have extreme difficulty completing this course. This is prerequisite material for essentially everything else in the course.

  2. Here is the tree traversal code from a solution to MP2, minus most comments
    TRAVERS:                        ; void travers( treenode * p ) {
            TESTR   R3
            BZS     TRAVQT          ;   __if_(p_!=_NULL)_{__________________
            STORES  R1,R2
            STORE   R8,R2,R8SAVE
            ADDSI   R2,ARSIZE
            MOVE    R8,R3
            LOAD    R3,R8,LEFT
            JSR     R1,TRAVERS      ;   _____travers(_p->left_)_____________
            LOAD    R3,R8,DATA
            LIL     R1,PUTSTR
            JSRS    R1,R1           ;   _____putstr(_p->data_)______________
            LOAD    R3,R8,RIGHT
            JSR     R1,TRAVERS      ;   _____travers(_p->right_)____________
            ADDSI   R2,-ARSIZE
            LOAD    R8,R2,R8SAVE
            LOADS   R1,R2
    TRAVQT:                         ;   __}_________________________________
            JUMPS   R1              ; } -- end of travers
    

    a) Fill in the comments above with well formatted C code to explain the assembly code. (0.5 points)

    1/4 did perfect work, 1/6 left it blank, while 1/4 earned no credit. The most common error was to use p. or *p instead of p->. Attempts to write this as a while loop were also common, as were attempts to return some kind of value at the end.

    b) What addressing mode is used to save and restore R8? (0.5 points) __indexed_______________

    Only 1/6 got this. By far the most common error, made by over 1/3, was to list an instruction instead of an addressing mode. I suspect that many of those who made this error did not read the readings in advance of the lectures. 1/6 gave PC-relative, a mode that had been discussed in class right before the exam.

    c) What addressing mode is used in the recursive calls? (0.5 points)   __PC_relative___________

    Over 1/3 got this, while 1/3 gave an instruction instead of an addressing mode, as above. Absolute and immediate modes were popular wrong answers, and some simply said relative, which is vague, since all forms of indexed addressing are relative to something.

    d) What is the value of ARSIZE? (0.5 points)                    __8_____________________

    About 1/2 got this right. Popular wrong answers, in order of decreasing popularity, were 12, 4, 16, and 20. Getting the right answere merely required reading the code and looking at how many distinct displacements were used for indexed addressing relative to R2.

  3. A problem: Here is brief useless bit of SMAL code. Show what this code puts into memory in the spaces to the right; use hexadecimal, one byte per block. (2 points)
    
    .       =       0
    A:      B       A,B,C,D
    B       =       'b'
            ALIGN   4
    C:      ASCII   "cd"
            ALIGN   4
    D       =       . - A
            W       B
            H       D
    
    Address   3    2     1    0  
    000000 0804 6200
    000004 6463
    000008 0000 0062
    00000C 0008
    000010

    1/10 did perfect work. About 1/12 made random clerical errors on each of the boxes, not a huge disappointment. 1/10 made so many errors that they earned no credit at all, and 1/15 left the problem blank. The most common error made by 1/2, involved the value of the symbol D. 1/3 had trouble with the values of B and C. 1/4 had problem with the byte order for the ASCII directive, but most of these did not alter the byte order elsewhere. 1/5 mistakenly assembled a byte into memory for the B='b' line.

    Like problem 1, this problem was entirely predictable to anyone who looked at previous exams. This is prerequisite material for the remainder of the class, since if you don't understand what the assembler is doing, it is extremely hard to understand assembly language in general.

  4. a) Here is a small Hawk subroutine. By hand, assemble this code into a sequence of halfwords as stored in memory, each expressed in hexadecimal. (2 points)
    SUBR:
            STORES  R1,R2
            ADDSI   R2,4
            TESTR   R4
            BNE     SUBQT
            ADDSI   R4,-1
            ADDSI   R3,1
            JSR     R1,SUBR
    SUBQT:
            ADDSI   R2,-4
            LOADS   R1,R2
            JUMPS   R1
    
             scratch space         
    
    
    
    ; int subr(int i, int j){
    
    
    
    ;   if (j != 0) {
    
    
    ;     i = subr( i+1, j-1 )
    ;   }
    
    ;   return i
    ; }
    
    Address    Value    
    000000 A  2  F  1
    000002 C  4  1  2
    000004 E  4  F  0
    000006 0  4  0  A
    000008 C  F  1  4
    00000A C  1  1  3
    00000C 3  0  F  1
    00000E F  F  F  0
    000010 C  C  1  2
    000012 D  2  F  1
    000014 B  1  F  0

    The boldface answer at address 0 above was given on the exam. The comments in the scratch space explain the intended meaning of the code. The boldface BNE instruction is a typo on the exam. It should have been BEQ; this had a potential impact on answers to part e); nobody pointed out the typo, although many students seem to have understood the intent of the code.

    None did perfect work here, as there were so many opportunities for clerical errors. Fully 1/4 of the class left this blank or filled in nonsense values with no obvious relationship to the given code.

    By far, the most common error was to omit the displacement for the PC-relative JSR at address E. Only 1 gave the right value for this, and only a few included this halfword. The BNE instruction at address 6 also caused trouble; 1/9 earned no credit here, and 1/6 had the wrong PC-relative branch displacement. 1/9 also had the wrong 4-bit representation of -1 and -4 in the ADDSI instructions at addresses 8 and 10.

    Students who had difficulty with negative numbers on problem 1 would certainly have trouble with negative branch displacements here.

    b) What register(s) does it expect parameters in? (0.5 points) __R3_R4_______________________

    1/9 did well here, while around 1/4 omitted R4, 1/4 counted R2 as a parameter and 1/6 counted R1 as a parameter.

    Counting R1 and R2 as parameters is natural based on a shallow reading of the code, but it ignores everything we have said in class and in the notes about our standard calling sequence, where R1 and R2 are part of the mechanism for call and return.

    c) What register(s) does it alter? (0.5 points)              __R3_R4_______________________

    1/8 omitted R3. 1/6 said the code alters R1, while 1/2 said it altered R2.

    While the code does use R1 and R2, it undoes all of the changes it makes to these registers, so that it returns leaving the exact values in those registers that where there when control reached SUBR.

    d) What register(s) does it return? (0.5 points)            __R3__________________________

    1/6 got this based on misreading the code the way it was intended. 1/6 said it returned R4, these were given credit because, with the mistake in the code, R4 could be counted as one of two (rather useless) return values. Over 1/3 said it returned R1, while 1/5 said it returned R2.

    It seems that those who said it returned R1 were confusing the idea of a value returned in a register with the register used as a return address.

    e) What does it do (in one line!)? (0.5 points) __R3_=_R3_+_R4___________________________

    1/12 got this, while 1/2 gave nonsense answers or left it blank. Partial credit was given for those who concluded that it computed some simple mathematical function such as subtraction or moving R4 to R3.

    After discovering the error noted above, I went through all of the answers people gave and did not find one that described anything like the function of the erroneous code.

    Note that this is a really stupid way to add two numbers; it takes time and stack space proportional to the initial value of R4.