# Exam 2: Midterm

### Exam 2

```                     X X
mean   = 4.76        X X
median = 4.6         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
```

### Exam 1 plus 2

```                             X
mean   = 11.66               X
median = 12.4                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
```

### Machine Problems 1 to 5

```mean   = 19.84                                 X       X
median = 21.0                                  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
```

### Homework Problems 1 to 11

```mean   = 21.61
median = 22.1                  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. 32
```

### Total Scores

```mean   = 53.11                           X
median = 56.5      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. 64. 68. 72. 76. 80
Grade Scale?    + | -    C    + | -    B    + | -    A    + |
```

## Exam Solutions

1. Background: Consider the flipflop circuit given below. It consists of two identical sub-components and some control logic. The overall behavior of the flipflop is best understood by thoroughly understanding the left half and then using what you have learned to work out the impact of the right half. It may be useful to note that this circuit can be redrawn as a cascade of two 2-input multiplexers with outputs e and f, while the values c and d are internal to one of the multiplexers.

a) Complete this timing diagram: (2 points)

Intermediate point c was relatively easy, depending as it does only on inputs a and b. 15 did perfectly here.

Intermediate points d and e were harder. Most solutions appeared random, but there were 2 or 3 who had entirely correct solutions here.

Output f was hardest. Nobody had it exactly correct, but 2 came very close.

The most disturbing wrong answers were those where transitions on the intermediate and output points appeared at random times unrelated to the times at which the inputs changed.

The hint in the exam, that the circuit can be seen as a pair of multiplexors, allows you to redraw the circuit as shown here. This redrawing lets you arrive at the behavior of output e directly, without worrying about c and d, and once you solve for e it also lets you get f. Nobody took advantage of this.

b) Give the proper name (based on its behavior) for this kind of flipflop: (0.5 points)

It is a trailing-edge triggered master-slave type D flipflop

3 said "trailing edge." 6 said "edge triggered." 9 said "master slave." 4 said "type D." Saying either "master slave" or "edge triggered" was acceptable, but full credite required identifying the edge on which it was triggered "trailing" and identifying it as type D.

2. Background: Precision machine tools in the United States frequently have an accuracy of 0.0001 inches. As the designer of the software to control such a tool, your boss has decided to represent all dimensions using a fixed-point data format with 14 bits after the point. Assume x is a variable of this type.

Some of your input data is given in units of 0.0001 inches. Assume i is a variable of this type. The formula to convert between these types is:

x = i × (16384/10000)
16384/10000 = 1.1010 0011 0110 1110 0010 1110 1011 00012

Assume that the machine-tool controller uses a Hawk CPU, and that you have access to LONGMULU, a routine that takes two 32-bit unsigned integer operands in R3 and R4 and returns the 64-bit product in the double-word R3 (least significant bits) and R4 (most significant bits).

Your job in this assignment is to convert i in R3, to x, leaving the result in R3.

a) Give Hawk code to load the appropriate multiplier in R4. Note, the multiplier given above has 33 bits, so you must either load an approximation or find a clever trick. (1 point)

```        LIW     R4,#1A36E2EB    ; multiplier, 28 places after the point.
; 1.6384 = #1A36E2EB = 0001 1010 0011 0110 1110 0010 1110 1011
```

1 did this well, 3 gave ineffective code based on the right value or loaded the right value and then overwrote it with something else. 5 left it blank, the remainder gave irrelevant (but sometimes long). Many wrong answers involved attempting to somehow temporarily find more than 32 bits in a register.

2 came close to the first part of the trick solution:

```        LIW     R4,#A36E2EB1    ; 0.6384, 32 places after the point.
MOVE    R8,R3           ; keep a copy of the multiplicand.
```

b) Give Hawk code to call LONGMULU, assuming that there are some local variables on the stack. (0.5 points)

```        ADDI    R2,R2,ARSIZE
LIL     R1,LONGMULU
JSRS    R1,R1
```

This should have been easy, but only 3 got it perfectly. 4 forgot the activation record, 1 carelelessly messed up the JSRS instruction. 5 gave irrelevant answers, and the rest left it blank.

c) Give Hawk code to adjust the format of the result to match the problem requirements. The details of this code depend critically on the code you wrote for part a). Note that one way to do the adjustment is to move the bits of each register into the right place in that register, then zero the unneeded bits before oring the results into the result register. (1.0 points)

```	; the following code is a 2-bit left shift to get the
; integer part of the 64-bit result into R3.
; (the multiplier had 28 bits right of the point.)
SRU     R3,14
SRU     R3,14           ; a 28-bit right shift to the low word
SL      R4,4            ; a 4-bit left shift of the high word
OR      R3,R4           ; combine the results.
```

Nobody got it right. One person tried to use a long series of add and shift instructions, ignoring their own answers to the first two parts. 2 wrote lots of code that was apparently unrelated to the problem statement. 2 more wrote brief but irrelevant blocks of code, and the rest left it blank.

Three made significant errors that suggested that they were trying to use the tricky solution given below:

```        ; complete the trick suggested in the first part above
ADD     R3,R4,R8         ; 1.6384 i = 0.6384 i + i
```

3. A Problem: A Problem: Write SMAL Hawk code to multiply R3 by 105; if you need a scratch register, use R1. Your solution will be graded for efficiency, so you may want to explore both factoring and using a binary representation to see what works best. (3 points)
```V1:	ADDSL   R3,R3,1 ; times 3
NEG     R1,R3   ;          note that 3 x 5 x 7 = 105

V2:     MOVE    R1,R3
ADDSL   R3,R1,1 ; times 11         = times 3
ADDSL   R3,R1,2 ; times 1101       = times 13
ADDSL   R3,R1,3 ; times 1101001    = times 105

V3:     ADDSL   R3,R3,2 ; times 5
MOVE    R1,R3   ;          note that 5 x 21 = 105
ADDSL   R3,R1,2 ; times 101        = times 5
ADDSL   R3,R1,2 ; times 10101      = times 21
```

6 gave solution V1 above. 2 gave solution V2 and 2 gave solution V3. 4 made errors in attempting to give solution V1. 1 gave a correct variant of V1 with an extra instruction and 1 added 3 instructions. One tried a version based on the idea that 105 = 99 + 6, but made a major error. One gave strange code. This was the easiest problem on the exam.

4. Background: The notes show one exception implementation, but there are alternatives. Consider this one: The global variable for an exception, for example, constraint_error is a single word, the address of the current handler description record. The activation record of any routine that contains an exception handler must contain a three-word handler description record for that handler. The fields of a handler description record are:
• exar -- the address of the handler's activation record
• expc -- the address of the entry point for the handler
• exprevious -- the saved address of the previous handler description record.

a) Write SMAL Hawk code to raise (or throw) a constraint-error exception. (1 point)

```        LIL     R3,CONSTRAINT_ERROR
JUMPS   R1              ; go there
```

Nobody did perfectly, 12 left out the LOADS used to follow the pointer from the exception variable to the handler description and 2 made random small errors. 2 added extra instructions with no clear purpose, 4 gave strange code, and 2 left the problem blank. It is fairly clear that almost nobody read the problem closely enough to understand the difference between the exception model given here and the model used in the notes.

b) Write SMAL Hawk code for the entry point of a constraint-error handler that restores the previous handler. This code can be tightly integrated with the above. Assume the handler description record (a local variable) for this handler is called my_HDR. (1 point)

```SOME_HANDLER:
LOAD    R2,R4,EXAR      ; * Restore the handlers activation record
LOAD    R1,R4,EXPREVIOUS; get pointer to previous handler description
STORES  R1,R3           ; make it be the current handler
```

The above code is tightly optimized, assuming that every jump to the handler will leave R3 pointing to the current exception variable, and R4 pointing to the current handler description. The first instruction, marked with *, can be moved before the jump to the handler, as it was in most student solutions, but it is better done as shown here.

Nobody did well here. 1 came close but made significant errors. 8 gave essentially the code from the notes, which indicated no understanding of the distinction between the exception model given here and that given in class. 5 gave strange code with no apparent relationship to the problem. 5 left the problem blank.