Exam 3: Final

Solutions

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

Grade Distributions

Final Exam

                     X
mean   = 9.75        X
median = 9.0         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

Exams 1 through 3 (only those who took all exams)

mean   = 22.76             X
median = 22.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______
    10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30. 32. 34. 36. 38. 30

Machine Problems 1 through 6 (only those who turned in all 6)

                                             X
mean   = 24.96               X               X
median = 25.0                X               X
                           X X     X         X
                         X X X   X X       X X
 ____________X___________X_X_X_X_X_X___X_X_X_X__
    10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30

Top 10 of 12 homework assignments (only those who did 10)

                                   X
mean   = 24.78                   X X   X
median = 25.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____
    10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30

Grand totals (only those included in all of the above)

mean   = 74.61                     X   X       X
median = 75.6            X     X   X X X       X     X
 ______________X_____X___X_____X_X_X_X_X_X_X___X_X_X_X_______X______
    40. 44. 48. 52. 56. 60. 64. 68. 72. 76. 80. 84. 88. 92. 96. 100
Grades     D   + + - -   C   + + - -   B   + + - -   A   + +

Grand totals (including those with missing work)

median = 70.2                      X
                                   X
XX         X                   X   X   X       X
XX         X X           X     X   X X X       X     X
XX_________X_X_X_____X___X_____X_X_X_X_X_X_X___X_X_X_X_______X______
    40. 44. 48. 52. 56. 60. 64. 68. 72. 76. 80. 84. 88. 92. 96. 100
Grades     D   + + - -   C   + + - -   B   + + - -   A   + +

The 6 students who fell off the bottom of the curve are a mystery, having turned in little or no work and missed one or more exams. Why didn't they drop the course?

Exam solutions and commentary

  1. Here is a code fragment lifted from the solution to MP6 posted on line:
    11 INSTRTRAP =     #20
    12 LCSAVE  =       .
    13 .       =       INSTRTRAP
    14 
    15         CPUSET  R2,TSV        ; save R2
    16         LIL     R2,SAVEREGS   ; get the save area pointer
    17         JUMPS   R2            ; join common code for all traps
    18 
    19 .       =       LCSAVE
    20 
    21 SAVEREGS:
    22         LIL     R2,INSSAVEAREA
    23         STORE   R1,R2,svR1
    24         STORE   R3,R2,svR3
    25         CPUGET  R1,TSV
    26         CPUGET  R3,TPC
    27         STORE   R1,R2,svR2
    28         STORE   R3,R2,svPC
    

    Recall that each entry in the trap vector is occupies 16 bytes.

    a) How many bytes of the INSTRTRAP trap vector entry are unused?

    (0.8 points) __8____________________________

    1/4 got this, 1/3 earned a bit of credit for 16.

    b) What value is stored in the assembler's symbol LCSAVE?

    (0.8 points) __The_Location_Counter_________

    1/7 got this, 1/4 earned partial credit for saying "the program counter." Many who earned no credit gave off-the-wall numeric values.

    c) As originally written, lines 16 and 17 were just one instruction, JUMP SAVEREGS, but this had to be changed. Why?

    (0.8 points) __Displacement_more_than_32Kb__

    1/3 got this, there was little partial credit.

    d) As originally written, lines 22 and 23 were before the JUMP SAVEREGS just mentioned, but this had to be changed. Why?

    (0.8 points) __R2_was_needed________________

    almost 1/2 got this, and a few more got partial credit.

  2. Suppose you have the following 3 bits of code that do the same thing, written in a C-ish language like Java or C#:
    11  a = 0;
    12  while (b != 0) {                31  a = 0;
    13      if (b & 1) a = a + c;       32  if (b != 0) {
    14      c = c << 1;                 33      do {
    15      b = b >> 1;                 34          if (b & 1) a = a + c;
    16  }                               35          c = c << 1;
                                        36          b = b >> 1;
    21  a = 0;                          37          if (b == 0) break;
    23  if (b != 0) {                   38          if (b & 1) a = a + c;
    24      do {                        39          c = c << 1;
    25          if (b & 1) a = a + c;   3A          b = b >> 1;
    26          c = c << 1;             3B      } while (b != 0);
    27          b = b >> 1;             3C  }
    28      } while (b != 0);
    29  }
    

    Assume in all cases that R3, R4, and R5 hold the variables a, b and c, respectively.
     

    a) Lines 13, 25, 34 and 38 are the same. The comparison if(b&1) on these lines translates to one instruction followed by a conditional branch. What is that instruction?

    (0.8 points) __BITTST_R4,0__________________

    1/4 got this, 1/6 got partial credit for AND R4,4, code that would be illegal on the Hawk because the AND instruciton does not allow immediate operands.

    b) When written in optomized machine code, versions 11-16 and 21-29 take exactly the same number of instructions. The destination addresses on the branch instructins change, but only one opcode changes. In the first version, that opcode is BR, what does this change to in version 2?

    (0.8 points) __BNE__________________________

    3/7 got this right, and 2/7 got partial credit for inverting the branch sense. To fully understand this problem, you had to imagine the following SMAL Hawk code:

    ;       11-16                   ;       21-29
            LIS     R3,0                    LIS     R3,0
    LOOP:   TESTR   R4                      TESTR   R4
            BZS     ENDLP                   BZS     ENDIF
            BITTST  R4,0            LOOP:   BITTST  R4,0
            BBR     NOADD                   BBR     NOADD
            ADD     R3,R3,R5                ADD     R3,R3,R5
    NOADD:  SL      R5,1            NOADD:  SL      R5,1
            SR      R4,1                    SR      R4,1
            BR      LOOP                    BNE     LOOP
    ENDLP:                          ENDIF:
    

    c) When written in well optomized machine code, which version is faster, the one on lines 11-16 or the one on lines 21-29? How many instructions faster, per iteration?

    (0.8 points) __21-29_by_2_instructions______

    1/10 got this right, 1/3 said it was only 1 instruction faster, 1/7 said 3 instructions faster.

    d) Note that version 31-3C is the same as versiion 21-29 with an added if statement (line 37) and duplication of lines 25 to 27. The translation of version 2 of this code into version 3 has a name. What is it called?

    (0.8 points) __loop_unrolling_______________

    2/7 got this right, 1/4 got partial credit for saying optimization.

    e) The change from version 21-29 to version 31-3C can slow down the program if the I-cache is too ... (what?).

    (0.8 points) __small________________________

    4/7 got this right, there was little partial credit.

    f) What does this code do?

    (0.8 points) __a_gets_b_times_c_____________

    1/4 got it, 1/7 said multiplication without hinting at the operands

  3. Background The bitblit operator discussed in Homework 11 had an awful lot of parameters, twelve in total. Some operations require even more. If there are more parameters than there are registers, some of them must be passed in RAM. Consider copying them into the activation record of the called routine before the call. In the following example, we call ROUTINE passing two parameters; the values come from local variables of the caller. One parameter is passed in R3, the other is passed in RAM:
    ; copy extra parameters into AR of called routine
            LOAD    R3,R2,LOCAL1
            STORE   R3,R2,=????=
    ; set up parameters in registers
            LOAD    R3,R2,LOCAL2
    ; the call
            ADDI    R2,R2,ARSIZE
            JSR     R1,ROUTINE        ; routine( local1, local2 )
            ADDI    R2,R2,-ARSIZE
    

    a) Suppose the called routine has a local variable called MEMPARAM, defined the usual way for a local variable. What text would replace the =????= in the code above so that LOCAL1 is copied into the called routine's MEMPARAM?

    (0.8 points) __MEMPARAM+ARSIZE______________

    1/7 got this, 3/5 left off ARSIZE

    b) Suppose ROUTINE is assembled from a different source file. In that case, the JSR cannot be used. Instead, we must use JSRS R1,R1. What instruction must come before this?

    (0.8 points) __LIL____R1,ROUTINE____________

    6/7 got this; one wonders where the others were all semester!

  4. Background: Complete this macro to load a constant into a register. Parts a) to c) are "fill in the blanks" parts.
            MACRO LI =reg,=const
              IF (const < 128) & (const > -129)
    
    a)          __LIS_____reg,const_____                           (0.8 points)
              ELSEIF (const < #800000) & (const > -#800001)
    
    b)          __LIL_____reg,const_____                           (0.8 points)
              ELSE 
    
    c)          __LIW_____reg,const_____                           (0.8 points)
              ENDIF
            ENDMAC
    

    Parts a) and b) were equally easy; 4/7 got them; and 1/6 more made mistakes with the reg field, usually saying explicitly R3.

    d) You have written LIL R1,PUTCHAR many times. Why doesn't it work when you use LI R1,PUTCHAR?

    (0.8 points) __PUTCHAR_incomparable_w/_const

    Only 1 got this, but 1/5 got partial credit for sensible speculation. The fact is, external symbols such as PUTCHAR cannot be compared with constants because their values are not known until the linker processes the code.

    e) Suppose we wanted to write an AI macro to do either ADDSI or ADDI depending on the size of the operand. What values would replace 128 and -129 in the first if statement above?

    (0.8 points) __+9_and_-9____________________

    only a few got this right, but 3/7 got partial credit for answers such as "7 and -8" or "8 and -8". These answers included an off-by-one error (the difference between greater than or equal and just greater than), and (more subtle) missing out on the fact that the Hawk ADDSI instruction does allow both +8 and -8, but not zero.

    f) Suppose we wanted to write an AI macro to do either ADDSI or ADDI depending on the size of the operand. What values would replace #800000 and -#800001 in the second if statement above?

    (0.8 points) __32768_and_-32769_____________

    Again, only a few got this right, but 1/4 got partial credit for the off-by-one error "32767 and -32768".

    A few did notice that the question of what to do if the constant is outside this range is not at all obvious, but that wasn't an exam question.

  5. Background: Machinists frequently work to a precision of 1/1000 inch. The nearest binary approximation of this is 1/1024. A machine tool control system might be built using 32 bit fixed point binary numbers with 22 bits in the integer part and 10 bits in the fractional part.

    a) As a Hawk assembly language programmer, you want to load a dimension of one inch into R3. What constant do you use?

    (0.7 points) LIL R3,_1024___________________

    4/7 got this. A few insisted on giving the answer in binary instead of an obvious number base such as decimal or hex.

    b) As a Hawk assembly language programmer, you want to load a dimension of 10.5 inches into R3. What constant do you use?

    (0.7 points) LIL R3,_#2A00__________________

    1/5 got this. The variety of wrong answers was impressive.

    c) You just multiplied the height and width of a part to get its area, using the Hawk monitor TIMESU routine. How many bits are there in the fractional part of the result?

    (0.7 points) __20___________________________

    1/4 got this. There was little partial credit.

    d) R3 contains a dimension as an integer count of 64ths of an inch. You want to convert it so that it is in fixed-point fractional inches with 10 places after the point. This can be done with one Hawk machine instruction:

    (0.7 points) __SL______R3,4_________________

    Only a few got this; among those who got partial credit, 1/3 had the wrong shift instruction, while 3/5 had the wrong shift count.

  6. Background: Consider this bit of digital logic:
    a = b or c
    b = a and d

    a) Draw the logic diagram:

    (0.7 points)

    6/7 drew a good diagram.

    b) It is a flipflop. What inputs allow it to remember?

    (0.7 points) c = __0______ d = __1______

    1/2 got this, 1/4 got d right, but wanted c to be 1.

    b) How can you force the a output to 1

    (0.7 points) c = __1______ d = __1______

    1/3 got this; among those who got partial credit, 1/5 thought d was a "don't care" value and even more thought that either c or d could be zero.

    b) How can you force the a output to 0

    (0.7 points) c = __0______ d = __0______

    5/7 got this, while abgout 1/5 thought either c or d should be one.