Exam 1: Midterm

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

Exam I

                          X
Mean   = 5.84             X         X
                      X   X     X X X
              X X   X X   X   X X X X
              X X   X X   X   X X X X
            X X X X X X X X   X X X X     X
          X X X X X X X X X X 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 I and II

                                          X
Mean   = 7.66                             X
                                          X
                                          X
                                          X X
                                          X X
                                          X X
                                          X X
                          X               X X
                          X               X X
    X                   X X               X X
    X                   X X               X X
    X   X             X X X       X X   X X X
    X X X       X     X X X     X 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

Weekly Quizlets I to V

                                                                X
Mean   = 12.75                                                  X
                                                  X       X   X X
                                                  X     X X X X X
                                                  X     X X X X X
                                                  X   X X X X X X
                                              X X X   X X X X X X
                                            X X X X   X X X X X X
                                      X     X 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. 15

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 10 points so allocate 5 minutes of your time per point.

  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: -59 81 -19 44 -68
    Binary absolute value: 001110110101000100010011 0010110001000100
    Signed-magnitude: 101110110101000110010011 0010110011000100
    1's complement: 110001000101000111101100 0010110010111011
    2's complement: 110001010101000111101101 0010110010111100
    naturally biased: 010001011101000101101101 1010110000111100

    1/2 did perfect work, clerical errors were common, but some numbers caused far more trouble than others. 1/14 got none of the binary versions of 14, 1/10 got none of the naturally biased values. 1/8 generally had trouble with the one's and two's complement systems, for both positive and negative values.

  2. A Problem: Here is a big part of the code from MP2, minus most comments:
            LIL     R8,MP2DATA      ; char ** p = mp2data
    
    LOOP:                           ; for_(;;)_{________________________
    
            LOADSCC R3,R8           ; ___char_*_s_=_p___________________
            TESTR   R3
            BZS     ENDLOOP         ; ___if_(s_==_NULL)_break___________
            LIL     R1,PUTSTR
            JSRS    R1,R1           ; ___putstr(_s_)____________________
    
            ADDSI   R8,4            ; ___p++____________________________
            BR      LOOP
    ENDLOOP:                        ; }_________________________________
    

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

    1/24 did perfect work (not necessarily the comments given above). A similar number left this blank. 1/5 had trouble documenting the loop, 1/4 had trouble documenting the loop exit, 1/2 had trouble with what the LOADSCC did, or with the call, or with the pointer increment. Not surprisingly, pointers caused problems. Some wanted to use array notation, and extra or missing asterisks were common.

    b) What addressing mode does LOADSCC use with R8? (0.5 points)     __short_indexed____

    1/4 got this (or register indirect, which means the same thing). 1/3 said just indexed, for partial credit. Among the remainder, unrelated addressing modes such as immediate, pc-relative or register dominated.

    c) What addressing mode does the BZS instruction use? (0.5 points) __pc_relative______

    Over 1/2 got this. Among the remainder, some gave unrelated addressing modes; immediate was more popular than indexed, but close to 1/10 gave answers that weren't addressing modes.

    d) What is the displacement on the BR instruction? (0.5 points)      ___-8______________

    1/5 got this. Almost 1/2 forgot that the displacement should be negative for backward branches. 1/5 had off-by-1 errors for the magnitude (and a few gave 10, off by 2). A few students each gave magnitudes of 1, 4 and 6. Strange values such as 5, 11, 12, 14 and 20 also showed up.

  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:	ASCII	"bcd"
    B	=	.
    	ALIGN	2
    C:	B	A,B,C,D
    	ALIGN	4
    D	=	'a' + 2
    	W	A
    	H	B
    
    Address   3    2     1    0  
    000000 646362
    000004 63040300
    000008 00000000
    00000C 0003
    000010

    scratch space
                A = 0
                B = 3
                C = 4
                D = 'c' = 6316

    1/4 did perfectly, and 3 left it blank. Only a few had difficulty with the ASCII characters in the string "bcd", although a few added a null terminator. 1/6 had trouble with the value of D. 1/6 has a wrong value for B. 1/10 has wrong values for A and C.

  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. You can check your work using the given value for halfword zero of the result. Halfword zero of the result is solved for you to help check your work. (2 points)
    SUBR:
            MOVE    R5,R3       ; -- move p1
            LIS     R3,0        ; acc = 0
            ADDSI   R4,-1       ; p2--
            BNS     DONE        ; if (p2 >= 0) {
    LOOP:                       ;   do {
            ADD     R3,R3,R5    ;     acc = acc + p1
            ADDI    R4,R4,-1    ;     p2--
            BNR     LOOP        ;   while (p2 >= 0)
    DONE:                       ; }
            JUMPS   R1          ; return acc
    
                                     scratch space        
    
    Address    Value    
    000000 F  3  F  5
    000002 0  0  D  3
    000004 C  F  1  4
    000006 0  4  0  1
    000008 3  5  3  3
    00000A 6  4  F  4
    00000C F  F  F  F
    00000E F  C  0  9
    000010 B  1  F  0
    000012
    000014

    The comments added in the scratch space above are there to help with the answers to parts b to e below.

    Only 1/35 gave perfect answers. 1/14 gave nonsense answers and 1/5 left the problem blank. This was an open-book exam, and everyone was told on the first day of class and again during the week before the exam that they would need their Hawk CPU manual; during the exam, it was obvious that a fair number had not brought appropriate reference material.

    Almost 1/4 had problems with the (positive) PC-relative branch displacement for the BNS instruction, many with off-by-one errors. 1/3 had problems with the negative branch displacement on the BNR instruction, mostly leaving the displacement positive.

    Over 1/4 forgot to add an extra halfword for the ADDI and of those who included this word, perhaps 1/10 got the wrong value for the constant -1. Of those who forgot the halfword, many tried to stuff the constant -1 into some part of the first halfword of the instruction.

    1/10 added halfwords with strange values for the lines holding the labels LOOP and DONE. These frequently had values like FFF0. Were they PC relative displacements for the preceeding conditional branches? Were they related to the blank lines in the code? I couldn't tell.

    b) What register(s) does it use as parameters? (0.5 points) __R3,R4_____________________

    Almost 1/2 got this. Almost 1/2 added R5 as a parameter, despite the fact that the first thing the code does is overwrite R5 with R3. About 1/7 missed either or both R3 and R4, despite the fact that the result of the code clearly depends on both of these.

    c) What register(s) does it alter? (0.5 points)             ____R3,R4,R5___________________

    1/3 got this. All you needed to do for this was look at the set of destination registers modified by any instruction in the code. Almost 1/2 missed R5, 1/10 missed R3 and 1/15 missed R4.

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

    Over 1/3 got this right. To get this, you really had to look at what the code does. It always returns with R5 as an unchanged copy of the original R3, hardly an interesting value. If R4 was non negative on entry, it always ends up -1 on exit, only slightly more interesting. The big changes happen to R3.

    Alomost 1/3 included R4 as a return value, for a small penalty. Almost the same number omitted R3 despite the fact that it is the target of the most computation in the code. 1/6 included R5.

    e) What does it do (in one line!)? (0.5 points) _______________R3_=_R3_*_R4_(for_R4>0)___

    1/10 got this. To do this, they needed to understand the consequences of the loop in the code. 1/10 left this blank. 1/15 said R3=R3+R4 for a little credit and 1/15 said R3=R3+R5 for less credit) Most of the remainder earned little or no credit, giving long sequences of code with no real attempt to explain what it did. Those who gave loops frequently omitted loop initialization. Many focused on what the loop did to just one of R3 or R4.