# Midterm I Solutions and Comments

### 22C:50 Section 2, Fall 2000

Douglas W. Jones

```    Median = 7.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_
0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10
A grade scale:        D       C       B       A
```
Note on grade scales: One exam is not sufficient to assign a very detailed grade scale or well defined cutoffs between grades.
1. The solution to MP2 assembles the given code as follows:
```     1 0000: 16    |    B   22    ; 11 had big hexadecimal problems
2 0001: 22    |X:  B   #22
3 0002: 01    |    B   X
4             |; commentary
5 0003: 0A    |    B   Y     ; 12 had big problems with this
6 0004: 05    |    B   Z     ; 11 had big problems with this
7             |Y   =   10
8             |Z:
9             |              ; 11 had perfect scores

; symbol table:
X       =       #0001
Y       =       #000A
Z       =       #0005             ; many had trouble with this
```
For a bit more credit, most correctly identified lines 5 and 6 above as containing forward references.

2. Consider this bit of C (or equally, C++) code:
```#define twice(x) x * 2
#define next(x) x + 1
int i = twice(next(4));
```

a) 4 is the actual parameter to next. 7 missed this entirely.

b) 4 + 1 is the actual parameter to twice. 10 missed this entirely. 5 prematurely evaluated the expression, passing 5.
20 suggested next(4), and were incorrectly penalized. Those who received a 0.2 point penalty for this answer should see me!

c) 4 + 1 * 2 replaces twice(next(4)). 5 missed this entirely. about half tried to evaluate the expression, giving the value 10, and many gave a parenthesized expression, with no hint of how the parentheses got introduced.

c) The initial value of i is 6!

3. The EAL assembler (the solution to MP3) gives the following:
```     1 0000: 02    |    B   X
2             |Z:
3             |.   =   .+1    ; many ignored this line!
4 0002: 0F    |X:  B   15
5 0003: 0F    |Y:  B   15     ; 4 had no credit at all
6             |.   =   Z
7 0001: 03    |    B   Y      ; 10 had perfect scores here
8             |.   =   Y+1

; symbol table:
X       =       #0002
Y       =       #0003
Z       =       #0001

```
For a bit more credit, 12 correctly noted that all of the symbols in the symbol table are relocatable.

4. The following parser will handle the grammar; 9 had perfect answers.
```void parse_s_exper()
{
if (lex_this != '(') {
parse_atom();                    /* 21 got this */
} else {
lex_scan();
if ( lex_this != ')' ) {         /* 12 got essentially this */
parse_s_expr();
while (( lex_this != '.' )   /* 16 got essentially this */
&&     (lex_this != ')')) {
parse_s_expr();      /* 27 got this */
}
if (lex_this == '.') {
lex_scan();              /* 31 got this */
parse_s_expr();
}
}
if (lex_this != ')') EXCEPT_RAISE( parse_error );
lex_scan();
}
}
```
5. 31 students agreed with the macro version of EAL, which gave the following:
```     1             |    MACRO X I
2             |      IF I > 0
3             |        B I
4             |        X (I-1)
5             |      ENDIF
6             |    ENDMAC
7             |
8             |    X 0
9 0000: 01    |    X 1
10 0001: 02    |    X 2
10 0002: 01
11 0003: 03    |    X 3
11 0004: 02
11 0005: 01
```