Exam 2: Midterm

Solutions and Commentary

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

 
 

Grade Distributions

 

Exam 2

Mean   = 3.88
Median = 3.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___________
  0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10

Exams 1 and 2

Mean   = 10.15   X
Median = 10.1    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

Homework 1 to 9

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

Machine Problems 1 to 4

Mean   = 11.70         X
Median = 12.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 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

Total Scores

mean   = 45.58                   X
median = 48.1                    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___
  20. 24. 28. 32. 36. 40. 44. 48. 52. 56. 60. 62
           D         C         B         A

If this was the end of the semester, the grade scale might look something like the above. The D-C division is largely decided by the fact that those with overall scores below 33 have generally failed to demonstrate competence in programming.

Note: We will only count the top 10 homework scores, no matter how many are assigned.

Solutions and Commentary



 HAWK EMULATOR
   /------------------CPU------------------\   /----MEMORY----\
   PC:  00000000                R8: 00000050   FFFFFC: --
   PSW: 0000FF03  R1: 000010B2  R9: 00000009   FFFFFE: --
   NZVC: 0 0 1 1  R2: 00010080  RA: 00000000 ->000000: LIL     R2,#010070
                  R3: 16710189  RB: 00000000   000004: JSR     R1,#00032C
                  R4: 00084F70  RC: 00000000   000008: LIL     R1,#00109E
                  R5: 00000050  RD: 00000000   00000C: JSRS    R1,R1
                  R6: 00000000  RE: 00000000   00000E: BR      #000000
                  R7: FF000000  RF: 00000000   000010: CPUSET  R2,#3

 **HALTED**  r(run) s(step) q(quit) ?(help)

Bus Trap.          Trap PC = #00001022
                   Trap MA = #16710188 invalid addr

+0000000C: F1  A2         25  PLOTIT: STORES  R1,R2
+0000000E: F8  22  0004   26          STORE   R8,R2,X
+00000012: F9  22  0008   27          STORE   R9,R2,Y
+00000016: F2  62  000C   28          ADDI    R2,R2,ARSIZE
+0000001A: F3  D8         29          LOADS   R3,R8
+0000001C: F4  D9         30          LOADS   R4,R9
+0000001E: 93  01         31          SR      R3,1
+00000020: 94  01         32          SR      R4,1
+00000022: F8  D3         33          LOADS   R8,R3
+00000024: F9  D4         34          LOADS   R9,R4
+00000026: E1 +000000     35          LIL     R1,PUTAT


  1. Give the line number of the instruction that directly caused the trap. ___33_____________

  2. Which register held the value that directly caused this trap? ___R3______________________

    The LOADS R3,R8 on line 33 is at address 2216, and R3 agrees with the trap MA.

    Most of the class got this right.

  3. The programmer used LOADS when they should have used what? ___MOVE_________________

    Every single LOADS in the above code shoul have been MOVE!

    Only 1/4 of the class got this.


     

  4. EXTB R3,R4,R0 does the same thing as MOVE R3,R4 followed by TRUNC R3,_8___________

    Only 1/5 of the class got this.

    MOVESL  R1,R3,5  
    ADDSL   R3,R1,3
    

  5. The code to the right multiplies by   _40____________

    1/3 got this, 1/4 got 288, 1/9 got 297. The wrong answers came from looking at shift counts without close attention to the use of R1.

    
    _ADDSL___R3,R2,2_
    
    _SL______R3,3____
    

  6. Can you multiply by the same without using R1? (answer to the right)

    1/2 the class were able to multiply by their answer to the previous question.


 

FUNC:   ; expects __R3_=_x_______________

        ;         __R4_=_y_______________

        ; uses    __R3,_R4,_R5_=_z_______

        ; returns __R3___________________
        TESTR   R4
        BZS     LABEL2          ; _if_(y_!=_0)_{______

LABEL1:                         ; ___do_{_____________
        MOVE    R5,R3
        AND     R5,R4           ; _____z_=_x_&_y______
        EQU     R3,R4
        NOT     R3              ; _____x_=_x_^_y______

        MOVESL  R4,R5,1         ; _____y_=_z_<<_1_____

        BZR     LABEL1          ; ___}_while_(y_!=_0)_

LABEL2:                         ; _}__________________

        JUMPS   R1              ; _return_x___________
  1. In the appropriate spaces above, identify and name (briefly and concisely) the registers used above for parameters, return values, and temporary values.

    1/10 did well here. Many students said that R4 was the return value, despite a) the fact that our convention is that R3 is always the return value and b) the fact that this code can only return when R4 is zero.

  2. In the appropriate spaces above, document all the if statements and loops. (use a C or Java-like style of comment, with appropriate indenting)

    Only 1/20 did well here. Most used if-goto style comments, or worse, comments that suggested calls to labels instead of just branches. Those who commented the loop as a loop rarely indicated the end of loop with a closing brace. Hardly anyone used indenting to set off the loop body.

  3. In the appropriate spaces above, document all the assignment statements. (use C or Java-ish notation and indenting)

    Perhaps 1/6 did well here. Many gave verbose English instead of boiling their comments down to assignment statements. Most either did not understand the EQU instruction or ignored it. Few correctly understood that the combination of EQU and NOT used her is an exclusive or, which is ^ in C, C++, Java and Python. Many used && when they should have used &. Most correctly understood the shift instruction.

  4. If the code above is called with parameters 310 and 510 what does it return? __8_______

    Only 1/20 got this. Here is a table of successive register values (in binary) through the computation:
    x y
    00110101
    01100010
    01000100
    00001000
    10000000

  5. How many iterations does it take to reach that value? __4______________________________

    1/11 did well here. Wild guesses seemed common.

  6. In the space below, draw the logic circuit that describes a bit slice of what the above code does to each bit of the two primary variables during each iteration:
    diagram of a half adder

    2 did well here. Most of the logic diagrams had an and gate and not gate with interconnections unrelated to the code given. Most only had one output, despite the explicit statement in the question that the circuit should update "the two primary variables." Many had input or output labels unrelated to either the registers used in the code or to names used for those variables in the comments written for problem 7.

    _xi__yi_ _xi_yi+1
     
     _0__  _0__   _0__  _0__ 
     
     _0__  _1__   _1__  _0__ 
     
     _1__  _0__   _1__  _0__ 
     
     _1__  _1__   _0__  _1__ 

  7. Label the columns in the truth table to the right to match the inputs and outputs of the logic circuit.

    1/4 gave labels that matched the logic circuit drawn in answer to problem 12. 1/4 earned partial credit, usually by adding spurious outputs. 1/6 gave answers with no discernible connection to the logic diagram. 1/5 left it blank.

  8. Fill in the values on the input side of the truth table.

    5/7 gave the standard arrangement of input values for a 2-input truth table. 1/6 gave no answer. The most common error leading to partial credit was to give the values in some kind of nonstandard order. Those who claimed the circuit had more than 2 inputs but only gave 4 rows earned no credit.

  9. Fill in the values on the output side of the truth table so they properly describe the circuit you have drawn for problem 12.

    1/3 gave output values that corresponded to what their answer to problem 12. 1/3 earned no credit, typically by filling in outputs with no discernible relationship to the logic diagram. would produce.

  10. What does FUNC do? ___It_adds__________________________

    2 got this. Most gave wild guesses. Greatest common divisor? Least common multiple? Definitely not.



; Fibonacci code to test ADD and SUB functions
;RETAD  =       0;
I       =       4;
ARSIZE  =       8;
FIB:
        ; expects R3 = i
        STORES  R1,R2
        ADDSI   R2,ARSIZE
        CMPI    R3,1
        BLE     FIBQ            ; if (i > 1) { // recursive case
        STORE   R3,R2,I-ARSIZE  ;   -- save i
        LIS     R4,1            ;   -- param
        JSR     R1,SUB          ;   -- sub( i, 1 )
        JSR     R1,FIB          ;   -- fib( i - 1 )
        LOAD    R4,R2,I-ARSIZE
        STORE   R3,R2,I-ARSIZE  ;   -- save fib( i - 1 )
        MOVE    R3,R4           ;   -- param i
        LIS     R4,2            ;   -- param
        JSR     R1,SUB          ;   -- sub( i, 2 )
        JSR     R1,FIB          ;   -- fib( i - 2 )
        LOAD    R4,R2,I-ARSIZE  ;   -- param fib( i - 1 )
        JSR     R1,ADD          ;   i = add( fib( i - 2 ), fib( i - 1 ) )
FIBQ:                           ; }
        LOADS   PC,R2           ; return i;


  1. When called from a test program, the above code fails by returning to location zero, sometimes after several recursive calls. What is the error?
     
    ___The_AR_gets_pushed_at_the_start,_but_never_popped_at_the_end____________

    1/5 did well. 1/9 understood that the problem involved something about the activation record. 1/2 gave answers unrelated to the problem, many of which could not possibly lead to the symptom described. 1/11 left it blank.

  2. Why does the code use I-ARSIZE to access its local variable instead of just I?
     
    ___Because_the_access_is_after_the_AR_is_pushed____________________________

    1/6 got this. 2/7 understood that the issue involved something about the activation record. Almost all of the rest gave answers that didn't seem to connect with the issue.

  3. The local variable I in the activation record is used for the parameter i, but it is also used for something else. What?
     
    ___It_is_used_to_save_the_result_of_fib(i-1)_during_the_call_to_fib(i-2)___

    1/9 got this, 1/9 earned good partial credit for saying it had something to do with recursive calls. 1/8 earned a bit of credit for saying something about needing to save something to compute the sum. 1/2 gave answers that didn't seem to indicate a useful level of understanding of the code.

  4. If ARSIZE was larger, what code in the above would need changing?
     
    ___ADDSI_R2,ARSIZE_would_change_to_ADDI_R2,R2,ARSIZE_______________________

    1/4 got this. 1/8 gave strong hints for strong partial credit. Most of the remainder earned no credit. The most common useless answer was that if ARSIZE was bigger, the definition of ARSIZE would need to be changed. (Yes, but that is trivially implied by the question.)

For the curious: The code used for problems 7 to 16 is the add function called by the code for problems 17 to 20. The entire program is here.