# Exam 1: Midterm

## Solutions and Commentary

### Exam 1

```                                           X
X
X
Mean   = 8.12                          X   X
Median = 8.5                     X     X   X
X   X X X X
X X   X X X X
X X   X X X X
X X   X X X X
X X   X X 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 Problem 1

```                         X
X
X
X
X
X
X
X
X
X
X
X
X
X
X X
X X
X X
Mean   = 4.36          X X
Median = 5.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__
0 . 1 . 2 . 3 . 4 . 5
```

### Homeworks 1 - 6

```                                                   X
Mean   = 12.92                             X       X
Median = 13.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 X
________X_________X_X___X_X_X_X___X_X_X_X_X_X_X_X_X_X_X_X_X_X______
3 . 4 . 5 . 6 . 7 . 8 . 9 . 10. 11. 12. 13. 14. 15. 16. 17. 18
```

### Total Scores

```                                                       X       X
X X     X X
Mean   = 25.40                                       X X X   X X X
Median = 26.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____
0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30. 32
A very very rough grade scale   F       D       C       B       A
```

## Solutions and Commentary

1. Fill in the following table; values in the blanks in each column derive from the given values in that column, all values in each row are derived from the given values using the same rules. (4 points)
```Bit Pattern x         001100     100011     010010     110011     011010

Unsigned decimal     ___12___   ___35___      18      ___51___   ___26___

1's complement of x  _110011_    011100    _101101_   _001100_   _100101_
(binary)
2's complement of x   110100    _011101_   _101110_   _001101_   _100110_

x as a 1's comp num  ___12___   __-28___   ___18___   __-12___      26
(signed decimal)
x as a 2's comp num  ___12___   __-29___   ___18___     -13      ___26___
```

```Bit Pattern x         001110     100111     010011     110001     011001

Unsigned decimal     ___14___   ___39___      19      ___49___   ___25___

1's complement of x  _110001_    011000    _101100_   _001110_   _100110_
(binary)
2's complement of x   110010    _011001_   _101101_   _001111_   _100111_

x as a 1's comp num  ___14___   __-24___   ___19___   __-14___      25
(signed decimal)
x as a 2's comp num  ___14___   __-15___   ___19___     -15      ___25___
```

There were two versions of this problem; the blanks for both versions are filled in above. In each version, one entry in each row and one entry in each column were worked in advance (those entries have no underlining in the above).

Most of the class earned full credit. There were scattered clerical errors, randomly distributed, but approximately 1/10 had serious difficulty with negative numbers, either reporting negative results for the positive values, failing to negate the negative values, or making even more severe errors.

2. Translate the following brief useless SMAL code to a sequence of bytes loaded in memory. The spaces to the right are labeled with a word and byte in word addresses; fill in the hexadecimal value that goes in that byte; leave uninitialized bytes blank. The result of assembling the first lines are given. (2 points)
```    . = 0                       ||  Address    3    2    1    0
A:  H     #4321             ||          |----|----|----|----|
B:  B     #65               ||  000000: |    | 65 | 43 | 21 |
; solved to here        ||          |----|----|----|----|
ALIGN 4                 ||  000004: |    |[02]|[0C]|[44]|
B     "D",#C            ||          |----|----|----|----|
C:  B     B                 ||  000008: |[00]|[02]|[06]|[0C]|
ALIGN 2                 ||          |----|----|----|----|
B     D,C,B,A           ||  00000C: |[00 | 0A]|[00 | 0C]|
ALIGN 4                 ||          |----|----|----|----|
D:  H     D,#A              ||  000010: |    |    |    |    |
END                     ||          |----|----|----|----|
```

```    . = 0                       ||  Address    3    2    1    0
A:  H     #4321             ||          |----|----|----|----|
B:  B     #65               ||  000000: |    | 65 | 43 | 21 |
; solved to here        ||          |----|----|----|----|
ALIGN 4                 ||  000004: |    |[0C]|[0C]|[42]|
B     "B",#C            ||          |----|----|----|----|
C:  B     D                 ||  000008: |[0C]|[06]|[02]|[00]|
ALIGN 2                 ||          |----|----|----|----|
B     A,B,C,D           ||  00000C: |[00 | 0A]|[00 | 02]|
ALIGN 4                 ||          |----|----|----|----|
D:  H     B,#A              ||  000010: |    |    |    |    |
END                     ||          |----|----|----|----|
```

There were two versions of this problem; the blanks for both versions are filled in above. In each version, the word at address zero was competely worked out in advance. Square brackets have been added to the solution to indicate which bytes stand alone and which bytes are grouped as halfwords (students were not expected to provide such markers).

Just under half the class earned full credit here, while about 1/20 earned no credit at all. 1/8 apparently did not notice the two halfword directives on line D (treating them as bytes). 1/5 did not have the correct values for one or more labels. Confusion between hexadecimal, decimal, quoted strings and labels (#A, "A" and A) was common, but it is difficult here to distinguish carelessness from cluelessness.

3. a) A small Hawk subroutine is given below to the right. Assemble this into the halfword memory locations given to the left below. The first instruction is worked for you. (1.5 points)
```    Addr:     Val        ||    .       =       #1000
1000:     00D6       ||
||    FUNCT:
1002:     01D5       ||            LIS     R6,0
||            LIS     R5,1
1004:  ___E3F0___    ||    FLOOP:
||            TESTR   R3
1006:  ___0502___    ||            BZS     FQUIT
1008:  ___5634___    ||            MOVE    R6,R5
||            MOVE    R5,R4
||            BR      FLOOP
100C:  ___F4F5___    ||    FQUIT:
||            MOVE    R3,R6
100E:  ___CF13___    ||            JUMPS   R1
||
1010:  ___F900___    ||
||
1012:  ___F6F3___    ||
||
1014:  ___B1F0___    ||
```

```    Addr:     Val        ||    .       =       #1000
1000:     00D4       ||
||    FUNCT:
1002:     01D5       ||            LIS     R4,0
||            LIS     R5,1
1004:  ___E3F0___    ||    FLOOP:
||            TESTR   R3
1006:  ___0502___    ||            BZS     FQUIT
1008:  ___5436___    ||            MOVE    R4,R5
||            MOVE    R5,R6
||            BR      FLOOP
100C:  ___F6F5___    ||    FQUIT:
||            MOVE    R3,R4
100E:  ___CF13___    ||            JUMPS   R1
||
1010:  ___F900___    ||
||
1012:  ___F4F3___    ||
||
1014:  ___B1F0___    ||
```

There were two versions of this problem, differing only in register use.

About 1/8 did perfect work, while about 1/12 earned no credit. The hardest part involved the PC-relative branch instructions. 1/2 had off by one errors on one or more branch displacements. 1/3 did not two's complement the displacement on the backward branch. 1/4 had at least one branch displacements that was off by more than one.

Among the less common problems, 1/12 had the wrong byte order, or even worse, an inconsistent byte order, and 1/20 had serious prxoblems with the ADDSI instruction. Several students made major clerical errors, leaving out entire instructions.

b) It is a subroutine. If FUNCT is called with the integer 3 in R3, what value is returned? (0.5 points)

```       ___2_____________________________________________________

```

The vast majority of the class got this correct. The most popular wrong answer was 3, given by 1/15 of the class. 1/20 of the class gave zero. 1/30 gave one.

4. Background: The following fragment of SMAL Hawk code related to MP2 might be the body of a MAKEROW routine.
```MRLOOP:
_TESTR___R12________
BGT     MRQUIT          ; while (n > 0) {

MOVE    R3,R8           ;   -- parameter x
MOVE    R4,R9           ;   -- parameter y

_LIL_____R1,SETAT___
_____JSRS____R1,R1__         ;   setat( x, y )

LIS     R3,'x'          ;   -- parameter 'x'
_____LIL_____R1,PUTCHAR
_JSRS____R1,R1______     ;   putchar( 'x' )

ADD     R8,R8,R10       ;   x = x + dx
ADD     R9,R9,R11       ;   y = y + dy

_____ADDSI___R12,-1_         ;   n = n - 1

BR      MRLOOP
MRQUIT:                         ; }
```

a) Three instructions are missing from the above (they have been replaced by underlined blank lines). Fill them in in the code above. (1 point)

There were two versions of this question, based on the same code. They had different instructions blanked out in the same code. The underlines above represent both questions, with the underlines shifted left or right to reflect one versions or the other.

Over 1/2 did perfectly. Only 1/30 earned no credit. A number of students made careless instructions such as using CMP R12,0 instead of CMPI R12,0. (Use of CMPI R12,0 instead of the more optimal TESTR R12 or using ADDI R12,R12,-1 instead of ADDSI R12,-1 was perfectly acceptable.)

Of far greater concern, 15 students had difficulty with the mechanics of the calls to monitor subroutines. Students who have successfully completed Homeworks 5 and 6 or even looked briefly at the solutions should not have had this difficulty.

b) (1 points) If parameters are passed in conformance with the usual Hawk conventions, using R3, R4, R5, R6 and R7, obviously, these values must be copied into R8, R9, R10, R11 and R12 before the above code. The usual Hawk conventions require that any subroutine using R8 through R15 restore the values of those registers before returning. Where should the MAKEROW routine save the ones it uses?

```       _In_the_activation_record________________________________
```

1/3 earned full credit. Almost full credit was offered to 1/12 more who gaven answers such as "where the stack pointer points". Half credit was given to the 1/12 who merely said "in memory" or to the 1/10 who said "in R2". No credit was given to those who merely suggested moving data back and forth between different registers. That cannot solve the problem.