Exam 3: Final

Solutions and Commentary

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

Grade Distributions

These distributions exclude students who did not take the final exam at the appointed time.

Exam III

Mean   = 5.22     X
Median = 4.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_X_____X_____
   0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10. 11. 12. 13. 14

Exams I, II and III

Mean   = 13.61        X
Median = 12.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_____X_____
   0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30

Machine Problems I to VIII

Note that MP4 and MP7 were second chances to finish MP3 and MP6 because so few turned in working solutions to MP3 and MP6. In offering a second chance, grading on MP3 and MP6 was done quickly and carelessly in order to offer rapid feedback. Because of this, the following rule was applied: Only one score was used for MP3 and MP4 and only one score for MP6 and MP7; if only one of the pair was submitted, that one was counted, but if both were submitted, only the final (more carefully graded) submission was counted.

Mean   = 15.36                        X
Median = 15.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_____
   0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30
          incompetent  |  some degree of competence

Note that those with machine problem scores below 10 never demonstrated significant competence on nontrivial programming assignments (MP3 and up).

Several students attempted to use ChatGPT for help, giving it appropriate credit. In general, it was a terrible helper; Chat GPT hallucinations generally made the code it "helped with" completely unusable.
 

Many students never collected their graded assignments from their teaching assistants. They would almost certainly have done better on later assignments if they saw the feedback offered by the graders for their previous work.

Homework

As stated at the start of the semester, only the top 10 out of 12 scores were used. These were all multiple-choice problem sets administered on-line by ICON, with students allowed 4 attempts with unlimited time per attempt.

Mean   = 27.31                                               XXX
Median = 28.6                                                XXX
                                                              XX
                                                            X X
                                                            X X
                                                        X   X X
                                                      X X   X X X
                                                      X X   X X X
                                                    X X X 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. 28. 30

Total Scores

Mean   = 56.27                    X
Median = 57.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_______
   28. 32. 36. 40. 44. 48. 52. 56. 60. 64. 68. 72. 76. 80. 84. 88
     F     |- -  D  + -|- -  C  + +|- -  B  + +|- -  A  + +

The grade scale was largely determined by the extent to which student programming assignments demonstrated any degree of competence; Those earning grades in the D range or lower never demonstrated significant competence on nontrivial programming assignments (MP3 and up).
 

Exam Solutions and Commentary

Please write legibly in the space provided. Illegible and verbose answers will be penalized! The expected answers are short. This exam is open book, open-notes, closed neighbor, no phones, no internet!

This exam is worth 20 points. Each question is worth 0.5 points, so allocate about 2.5 minutes of your time per question.

    bool testit( void * p ) {
        return (3 & (intptr_t)p) != 0;
    }
    
    TESTIT:___________________________
            TRUNC   R3,2
    __________________________________
            BZS     DONE
    __________________________________
            LIS     R3,1
    __________________________________
    DONE:
    __________________________________
            JUMPS   R1
    __________________________________
    
  1. What property of p does the C function on the right report?
     • returns true if p is not word aligned
    _________________________________________________

    8 got this, 5 left it blank; the remainder gave answers indistinguishable from nonsense.

  2. In the space to the right, write this function in SMAL Hawk assembly language. Use the standard Hawk programming conventions, minus comments. (Hint: More space is provided than is really needed.)

    1 got this, 4 forgot to limit the return value to true or false, 4 others forgot to return from the function. 15 left it blank; the remainder gave answers indistinguishable from nonsense.

  3. What addressing mode does JSR R1,TESTIT use?
     • PC relative addressing
    _________________________________________________

    40 got this, 2 left it blank; the remainder gave answers indistinguishable from nonsense.

  4. In the context of EXT TESTIT, why not write JSR R1,TESTIT?
     • This requires TESTIT be local, EXT makes it non-local
    _________________________________________________

    8 got this, 5 were vague but probably correct, 2 were more confused. 5 left it blank; the remainder gave answers indistinguishable from nonsense.

    __________________________________
            MOVE    R5,R6
    __________________________________
            SR      R5,5
    __________________________________
    

  5. Write code SMAL Hawk code to do R5 = R6 >> 5.
    (Answer in the space to the right.)

    4 got this, 6 used SRU instead of SR, 17 used left shifts instead of right shifts, one invented MOVESR (the Hawk only has MOVESL) and a few made various misspellings. 3 left it blank; the remainder gave answers indistinguishable from nonsense.

  6. Given that A is a local variable, write SMAL Hawk code to make R4 hold the address of A:
          LEA    R4,R2,A
    _________________________________________________

    6 got this, 15 omitted R2, 2 used LOAD instead of LEA, 2 used STORE. 3 left it blank; the rest gave answers indistinguishable from nonsense. The number of students who missed this elementary problem makes it clear that many in the class never took the programming assignments seriously.

  7. Given that R4 holds the address of an array A of words, and R5 holds an integer index i into that array, write SMAL Hawk code to to make R3 hold the address of array element A[i]:
          MOVE   R3,R5
          ADDSL  R3,R4,2
    _________________________________________________

    6 got this, 3 solved it for an array of bytes (with just one instruction, ADD), 3 more made small clerical errors. 3 left it blank; the remainder gave answers indistinguishable from nonsense. (The exam should have contained an extra blank line for the answer.)

  8. What is the difference between CMPI xxx,0 and TESTR xxx?
     • CMPI is a long instruction, TESTR is short
     • They also set the C condition code differently
    _________________________________________________

    3 clearly noted the long-short difference, 7 noted it vaguely. 3 othrs noted the condition code difference. 4 left it blank; the remainder gave answers indistinguishable from nonsense.

  9. Suppose, you find a local variable called R9SAVE described in the activation record of a Hawk subroutine. Where would you expect this variable to be referenced?
          STORE  R9,R2,R9SAVE in the receiving sequence
    _________________________________________________
          LOAD   R9,R2,R9SAVE in the return sequence
    _________________________________________________

    11 got this, 8 only mentioned the return sequence, 2 only mentioned the receiving sequence. 4 left it blank; the remainder gave answers indistinguishable from nonsense.

    __________________________________
            LIL     R5,X
    __________________________________
            LOADS   R5,R5
    __________________________________
    

  10. A SMAL Hawk program contains the line COMMON X,4. Write code in the spaces to the right that loads the variable X into R5.

    10 got this, 11 only mentioned the first line (loading the address of the variable. 12 only gave R5,X without giving the opcode. 3 left it blank; the remainder gave answers indistinguishable from nonsense. Again, this is the kind of elementary problem that any student who took the programming assignments seriously should have been able to answer with no difficulty.

  11. A naive implementation of BGT would make it branch if theresult of the previous subtraction was not negative and not zero. Why is this wrong?
     • The result sign will be wrong if there was an overflow.
    _________________________________________________

    16 got this, 1 was vague but not wrong. 4 left it blank; the remainder gave answers indistinguishable from nonsense.

            LOAD    R3,R2,A
    __________________________________
            LIS     R4,5
    __________________________________
            STORE   R4,R3,B
    __________________________________
    

  12. You find a->b = 5; in a C program, where a is a local variable. Translate this to SMAL Hawk code in the space to the right.

    2 got this, 20 got partial credit; many messed up the final store by imagining that there was a way to push a constant into memory without first loading a register. Another common error was to assume that the address of a was already in a register. 11 left it blank; the remainder gave answers indistinguishable from nonsense.

  13. Rewrite the expression a - b using only addition and logical operators:
     • a + ~b + 1
    _________________________________________________

    7 got this, 18 forgot the final + 1. 6 left it blank; the remainder gave answers indistinguishable from nonsense.

  14. After CMP R3,R4 on a Hawk computer, if C is set, what does this tell you about R3 and R4?
     • R3R4
    _________________________________________________

    An easy way to work this out is to work three little examples:
    1 - 0 = 1 + ~0 + 1 = 0001 + 1111 + 1 which definitely sets C
    0 - 0 = 0 + ~0 + 1 = 0000 + 1111 + 1 which definitely sets C
    0 - 1 = 0 + ~1 + 1 = 0000 + 1110 + 1 which does not set C

    6 got this, 5 said R3 = R4, 3 said R3 > R4. 3 left it blank; the remainder gave answers indistinguishable from nonsense.

                      MOVESL  R5,R4,12
    	          SL      R5,12
                      SRU     R3,8
                      SR      R4,8
    	          OR      R3,R5
    

  15. What does the code to the right do to 64-bit value v stored in R4-R3?
     • v = v >> 8
    _________________________________________________

    5 got this, 2 suggested rotate, 2 suggested left shift, 1 suggested an unsigned shift, 2 said it shrinks v to 48 bits, 3 only focused on the effect on the least significant halfword. 5 left it blank; the remainder gave (frequently very long) answers indistinguishable from nonsense.

  16. What addressing mode does LOAD R8,R2,R8SV use?
     • indexed
    _________________________________________________

  17. Testing MP8 included using hawklink mp8.o mp8test.o; the code of mp8test.a included a Hawk main program but no EXT connecting to MP8, and a good mpa.8 had no INT directives at all, yet code in mp8test did "call" code in mp8. How was this done?
     • The transfer from main program to MP8 is by trap
    _________________________________________________

    6 got this, one said by exception. 7 left it blank; the remainder gave answers that suggested that they might not have put much effort into MP8.

                      OTHER
              LCSAVE  =       .
    	  .       =       LOCATION
                      SOME
                      CODE
    	  .       =       LCSAVE
                      STUFF
    

  18. You have seen code like that to the right several times this semester. Where in memory does this put SOME CODE?
     • at and after LOCATION
    _________________________________________________

    18 got this. 2 left it blank; the remainder gave answers indistinguishable from nonsense. This suggests that many students saw this construction as boilerplate that they could use without understanding.

  19. How many bytes in the code to the right does the assembler skip between OTHER and STUFF.
     • none
    _________________________________________________

    8 got this. 2 left it blank; the remainder gave answers indistinguishable from nonsense; in many cases, they gave explicit and totally unjustified numbers such as 8, 10 or 12.

  20. The Sparrowhawk computer (with no long instructions) can run Hawk programs two ways: Assemble them using macros that build Hawk instructions from sequences of Sparrowhawk instructions, or install trap handlers to virtualize the missing instructions. Which approach would lead to faster execution on Sparrowhawk hardware?
     • macros are faster
    _________________________________________________

    14 got this. 4 left it blanik, while the remainder said, with great confidence, that exception handlers are faster.

    Note that those who put effort into MP8 would have noticed that exception handlers include blocks of code to save and restore the processor state and to decode the opcode the user tried to use, obviously slowing down execution considerably as part of the rpice of virtualization.

  21. Among the following, circle the ones that are long Hawk instructions:

    LIS   CMPI   JSR   MOVESL   COSET

    3 got this. 3 thought LIS was long despite the fact that the trailing S stands for short. 37 thought CMPI was short. 23 thought JSR was short. 38 thought MOVESL was long. 25 thought COSET was long.

    Difficulties on this problem are evidence that many students forget about instruction encoding and learn assembly language as if it were a (wretched) higher level language with no connection to underlying hardware.
     
     

    abcd
    0 0 1 0
     ____  ____   ____  ____ 
    0 1 0 0
     ____  ____   ____  ____ 
    1 0 1 0
     ____  ____   ____  ____ 
    1 0 1 1
     ____  ____   ____  ____ 
    logic diagram

  22. Complete the truth table to the right for point c in the above.

  23. Complete the truth table to the right for point d in the above.

    20 got each of the above. 15 lost credit for giving nonstandard order for the table rows (as indicated by the contents of the a and b columns. Only 3 earned no credit, and nobody left the truth table blank.

  24. What value of which input to the above causes output f to equal the other input?
     • b = 1 implies f = a
    _________________________________________________

    6 got this, 4 got partial credit for identifying input b. 5 left it blank; the remainder gave answers indistinguishable from nonsense.

  25. The above circuit implements what familiar function?
     • it is a type D latch
    _________________________________________________

    2 got this, 6 got partial credit for identifying it as some kind of flipflop. 4 left it blank; the remainder gave answers indistinguishable from nonsense.
     

          GETCHAR:
                  LIW     R4,KEYBOARD
          GETCLP:
                  LOAD    R3,R4,KBSTAT
                  BITTST  R3,KBREADY
                  BBR     GETCLP
    
                  LOAD    R3,R4,KBDATA
                  JUMPS   R1
    

  26. The code to the right (from the Hawk monitor) contains a loop. What kind of loop is it?
     • a polling loop
    _________________________________________________

    3 got this. 18 said it was a while loop, for partial credit. 4 left it blank; most of the remainder gave answers indistinguishable from nonsense. Some said it was a for loop, but that is nonsense because there is nothing in the code resembling incrementing or testing a loop control variable.
     

  27. The two LOAD instructions in the code to the right access what kind of memory locations?
     • device registers
    _________________________________________________

    1 got this; for partial credit, 4 said registers, 1 said I/O. Only 1 left it blank; the remainder gave answers indistinguishable from nonsense.

    __________________________________
            LOAD    R5,R3,NEXT
    __________________________________
            STORE   R5,R4,NEXT
    __________________________________
            STORE   R4,R3,NEXT
    __________________________________
    

  28. Consider a linked list where each item has a NEXT field. Given that R3 points to a given item in the list and R4 points to a new item, write SMAL Hawk code in the space to the right to splice the new item into the list after the given item. If you need temporary registers, use R5 to R7.

    5 got this. There was very little partial credit. 14 left it blank; the remainder gave answers indistinguishable from nonsense. Many gave answers using only LOAD instructions, therefore making no changes at all to the data structure, or even more bizarrely, using arithmetic or logic operations.

  29. What is the most likely cause of an "out of bounds" error when the SMAL assembler processes BR THERE? Assume THERE is defined as a label.
     •THERE is too far away
    _________________________________________________

    14 got this, 2 said vaguely that something was too big, 2 implied a range from 0 to 255 instead of -128 to +127. 3 left it blank; the remainder gave answers indistinguishable from nonsense.

  30. What code replaces BR THERE to repair the "out of bounds" error?
     •     JUMP   THERE
    _________________________________________________

    12 got this. 4 used JSR, 2 added an index register, 1 omitted the label THERE. 6 left it blank; the remainder gave answers indistinguishable from nonsense. Among the nonsense, a number offered the idea of replacing BR with BGT or some other conditional branch.
     

            BEQ     SKIP
    __________________________________
            JUMP    THERE
    __________________________________
    SKIP:
    
  31. Suppose it was not BR THERE but BNE THERE that caused the "out of bounds error." Use the space on the right to give replacement code.

    2 got this, 3 used BNE instead of BEQ, 2 put the label before the JUMP,. 15 left it blank; the remainder gave answers indistinguishable from nonsense. Again, many replaced BNE with random conditional branches that tested other condition codes.
     

  32. Assemble the SMAL Hawk code HERE: BR HERE and show the result below as a single halfword, in hexadecimal:
     • FF0016
    _________________________________________________

    3 got this, 13 gave 0000 (wrong displacement), 8 gave 00XX (byte order), 7 gave 00 (just the opcode without displacement). 11 left it blank; the remainder gave answers indistinguishable from nonsense.
     

       F0
    __________________________________
       30
    __________________________________
       FC
    __________________________________
       FF
    __________________________________
    

  33. Assemble the SMAL Hawk code HERE: JUMP HERE and show the result to the right as sequence of bytes, in hexadecimal.

    None got this fully. 11 earned partial credit; common errors included 6 forgot to give a displacement halfword and 3 who used an index register other than PC (R0). 15 left it blank; the remainder gave answers indistinguishable from nonsense; for example, giving assembly code or a single hex digit per byte.
     

  34. What is the decimal equivalent of the 32-bit IEEE format floating point number C120000016?
     • –10.0
    _________________________________________________

    9 got this, 5 earned partial credit where it was possible to identify a connection between their answer and the problem given. 13 left it blank; the remainder gave answers indistinguishable from nonsense.

    C120000016 = 110000010010 ...
    sign = 1 meaning negative
    exponent = 100000010 meaning +3 (recall that 01111111 = 0)
    mantissa = (1).010 ... (the hidden bit is in parentheses)
    so the absolute value is 1.010 ... * 23 = 1010.02 = 10.010
     

  35. Suppose your Hawk trap handler for the SLR instruction only explicitly used R1 to R6. It would be tempting to speed up the trap handler by not saving R7 to R15 in the trap save area. What problems would this create?
     • Virtuaized instructions could not reference
    _________________________________________________
        the user's values of R7 to R15
    _________________________________________________

    6 got this, 4 earned partial credit for vague hints at the answer. 4 left it blank; the remainder gave answers indistinguishable from nonsense. Many wrong answers referenced Hawk calling conventions, which do not apply to trap service routine entry.

    The optimization suggested here would actually be quite appropriate for speeding up any interrupt or trap handler that did not need to access user registers the way a virtualized instruction trap handler does.
     

                      TBIT    R4,0    
                      BBR     BIT0
                      SL      R3,1
              BIT0:
                      TBIT    R4,1
                      BBR     BIT1
                      SL      R3,2
              BIT1:
                      TBIT    R4,2
                      BBR     BIT2
                      SL      R3,4
              BIT2:
                      TBIT    R4,3
                      BBR     BIT3
                      SL      R3,8
              BIT3:
                      TBIT    R4,4
                      BBR     BIT4
                      SL      R3,16
              BIT4:
    

  36. In the code to the right, TBIT is a derived instruction that is really an alias for some other Hawk instruction. What is the actual Hawk instruction used to implement TBIT R4,0?
          ADDSR   R0,R5,1
    _________________________________________________

    2 got this. 11 identified my mistake, saying BITTST R5,1, but did not notice that BITTST is a derived instruction. 5 forgot to give the operands, just giving the opcode. 9 left it blank; the remainder gave answers indistinguishable from nonsense.

    This code was on midterm II, and a debugged version of it was incorporated into the solutions to MP6 and MP7.
     

  37. Similarly, what is the actual Hawk instruction used to implement BBR BIT0?
          BCR   BIT0
    _________________________________________________

    10 got this, 4 left off the operand, 2 gave BCS. 8 left it blank; the remainder gave answers indistinguishable from nonsense.
     

  38. Each level in th code to the right tests bit i of R4 and then shifts R3 by n bits if the bit is set. Give the relationship between n and i.
     • n = 2i
    _________________________________________________

    14 got this, 2 earned partial credit. 3 left it blank; the remainder gave answers indistinguishable from nonsense. The nonsense was truly odd, including many mathematical expressions that did not relate n to i or that gave relationships that had no connection to the trivial relationship in the given code.

    Again, it is worth emphasizing that this code was on midterm II, and a debugged version of it was incorporated into the solutions to MP6 and MP7.

  39. To take the absolute value of an IEEE floating point value in R3, you could use the Hawk floating-point coprocessor and use some COSET and COGET instructions, but there is a much much faster solution that does not need the coprocessor. Give the fastest SMAL Hawk code you can think of to do this in the space below:

    _________________________________________________
          SLU    R3,1
    _________________________________________________
          SR     R3,1
    _________________________________________________

    Alternatively (longer code so a bit slower and using more registers):
          LIW    R4,#7FFFFFFF
          AND    R3,R4

    8 had good solutions, 12 earned partial credit with errors such as using OR instead of and, using SR instead of SRU, switching the order of the shifts (to zero out the low bit instead of the high bit), or worse, trying to use integer NEG on a floating value. 15 left it blank; the remainder gave answers indistinguishable from nonsense.