Midterm I Solutions and Comments

Part of materials for 22C:50, Spring 2003
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Grade Distributions

                                        X X
                                        X X
                                        X X
                                        X X
                                      X X X
                                  X X X X X
                                  X X X X X
                                  X X X X X
  X                             X 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

Exam Soltuions

Note, there were two versions of this exam!

  1. Hand assemble the following EAL code, and show what goes in the symbol table. For those lines of code that cause data to be stored in memory, show the memory address that is changed and the value stored in memory. There are many extra blanks! Do not fill in anything unused symbol table entries or next to lines that assemble nothing into memory. (2 points)

    The results of machine assembly are shown for the two versions that were given, with * marking the lines that could not be properly passed on the first pass of a 2-pass assembler.

         1 0000: 0C    |     B    12
         2 0001: 21    |     B    #21
         3             |; Commentary
       * 4 0002: 03    |     B    A
       * 5 0003: 0A    |A:   B    B
       * 6 0004: 05    |     B    C
         7             |B    =    10
         8             |C:                 ; symbol table:
                                           A       =       #0003
         1 0000: 0D    |     B    13       B       =       #000A
         2 0001: 13    |     B    #13      C       =       #0005
         3             |; Commentary
       * 4 0002: 05    |     B    C
       * 5 0003: 0A    |A:   B    B
         6 0004: 03    |     B    A
         7             |B    =    10
         8             |C:
    

    There were several students who made clerical errors such as switching hexadecimal and decimal or starting at the wrong memory address. Among the more important errors were allocating memory for line 7, a definition, and showing memory contents on lines 4, 5 and 6 that differed from the symbol table contents. 5 or 6 made this mistake.

  2. Consider this bit of EAL code (2 points):

    The following shows the results of assembling these two strange bits of code using the SMAL assembler, with one error message deleted:

                     1       MACRO  FIDDLE P
                     2       OFFEND P)+(P
                     3       ENDMAC
                     4  
                     5       MACRO  OFFEND Q
                     6       B      (1-Q-1)
                     7       ENDMAC
                     8  
                     9       FIDDLE 5
                     9       OFFEND 5)+(5
    +000000: 00      9       B      (1-5)+(5-1)
    
                     1       MACRO  FIDDLE P
                     2       OFFEND P)-(P
                     3       ENDMAC
                     4  
                     5       MACRO  OFFEND Q
                     6       B      (1+Q+1)
                     7       ENDMAC
                     8  
                     9       FIDDLE 5
                     9       OFFEND 5)-(5
    +000000: 00      9       B      (1+5)-(5+1)
    

    a) What is the actual parameter to FIDDLE?

    5, in both versions. Most got this right, but a few gave the formal parameter P.

    b) What is the actual parameter to OFFEND?

    Either 5)-(5 or 5)+(5, depending on the exam version. This was harder; about 6 forgot the parentheses, while about 8 gave the parameter text from line 2, forgetting that OFFEND doesn't get called until the body of FIDDLE is expanded and therefore, that the formal parameter P should be replaced.

    c) What does the body of OFFEND expand to?

    Either B (1+5)-(5+1) or B (1-5)+(5-1), depending on the exam version. While several made miscellaneous more serious mistakes, about 13 made the same mistake, earning partial credit for forgetting the B directive and giving only the operand.

    c) What value gets assembled into memory?

    Zero! Most of the errors discussed above still allowed students to compute the correct value, but a few made fairly serious mistakes and came up with other values.

  3. Hand assemble the following EAL code, and show what goes in the symbol table.
         1 0000: 0005  |     W    5
         2             |X:
         3             |.    =    .+2        ; symbol table
         4 0004: 0002  |Y:   W    X          X       =       #0002
         5             |Z:                   Y       =       #0004
         6             |.    =    X          Z       =       #0006
         7 0002: 0004  |     W    Y
         8             |.    =    Z
         9 0006: 0006  |     W    Z
    
         1 0000: 0008  |     W    8
         2             |Z:
         3             |.    =    .+2        ; symbol table
         4 0004: 0002  |Y:   W    Z          X       =       #0006
         5             |X:                   Y       =       #0004
         6             |.    =    Z          Z       =       #0002
         7 0002: 0004  |     W    Y
         8             |.    =    X
         9 0006: 0006  |     W    X
    
    The results of assembly of the two versions of this problem with the EAL assembler from machine problem 2 are shown above; the most common errors were: Mismatch between the contents shown for the symbol table and the contents shown for memory, about 7 students; assembly of something into memory on lines 2 and 5, about 5 students; and misinterpreting the W directive as assembling one byte into memory, about 5 students.

    Most understood that all the labels here are relocatable.

  4. Hand assemble the following bit of EAL code, showing what goes in memory.

    Here are the results of assembling the two versions from the exam using the SMAL assembler, with 2 lines of code added to the macro body to make sure it lists only the lines actually assembled into memory:

                     1       MACRO X I
                     2         IF I > 1
                     3           X (I-1)
                     4         ENDIF
                     5         LIST 1000   ; added line
                     6         B I
                     7         LIST -1000  ; added line
                     8       ENDMAC
                     9  
                    10       X 0
    +000000: 00     10         B 0
                    11       X 1
    +000001: 01     11         B 1
                    12       X 2
    +000002: 01     12         B (2-1)
    +000003: 02     12         B 2
                    13       X 3
    +000004: 01     13         B ((3-1)-1)
    +000005: 02     13         B (3-1)
    +000006: 03     13         B 3
    
                     1       MACRO X I
                     2         LIST 1000   ; added line
                     3         B I
                     4         LIST -1000  ; added line
                     5         IF I > 1
                     6           X (I-1)
                     7         ENDIF
                     8       ENDMAC
                     9  
                    10       X 0
    +000000: 00     10         B 0
                    11       X 1
    +000001: 01     11         B 1
                    12       X 2
    +000002: 02     12         B 2
    +000003: 01     12         B (2-1)
                    13       X 3
    +000004: 03     13         B 3
    +000005: 02     13         B (3-1)
    +000006: 01     13         B ((3-1)-1)
    

    Most did well expanding the first 2 macro calls, shown here on lines 10 and 11. The problems, not surprising, were the recursive calls on lines 12 and 13, where at least 5 students had serious problems; several simply failed to do any recursion, so that each line of input assembled only one byte into memory. Others terminated the recursion at the wrong point.

  5. Consider the following fragment from the grammar for C:
    <declaration> ::= <type> <name> [ = <initializer> ] ;
    <initializer> ::= <value> | [ <initializer> { , <initializer> } ]
            void parse_declaration()
            {
                parse_type();
                parse_name();
                if (lex_this == '=') {
                    lex_scan();        /* scan over = */
                    parse_initializer();
                }
                if (lex_this != ';')
                    EXCEPT_RAISE( parse_error );
                lex_scan();        /* scan over ; */
            }
            
            void parse_initializer()
            {
                if (lex_this != '[') {
                    parse_value();
                } else {
                    do {
                        lex_scan;      /* scan over '[' or ',' */
                        parse_initializer();
                    while (lex_this == ',' );
                    if (lex_this != ']')
                        EXCEPT_RAISE( parse_error );
                    lex_scan();        /* scan over ']' */
                }
            }
    

    Most did well here. The most common problems, not surprisingly, had to do with the transformation of the grammar for <initializer> where two recursive calls in the EBNF were collapsed into one recursive call in the parser; this was possible because the curly braces in EBNF indicate zero or more repeats, while a post-test while loop in C requires 1 or more iteration. Student problems stemming from this typically rested on understanding what punctuation marks could occur in what context.

    The two versions of the exam differed in which lines of the above code were replaced with blanks.