Practice Exam 2

The exam will be open-book, so that you don't have to memorize the ASCII table or the details of the Pep/7 architecture.

  1. Convert the following C++ program to Pep/7 assembly language:
    int a, b, limit;
    
    int main() {
      a = 0;
      b = 1;
      cin >> limit;
      while (b < limit) {
        b += a;
        a = b - a;
      }
      cout << a << endl;
    }
    

  2. Consider the boolean formula (a + b') . (b' + c') . (a' + c).
    1. Construct a truth table for this formula.

    2. Draw a circuit using AND, OR, and NOT gates with inputs a, b, and c, whose output is the value of this formula.

    3. Draw an equivalent circuit using as few gates as possible.

  3. Convert the following Pep/7 program to an equivalent program in C++:
            BR      main
    n:      .BLOCK  d#2
    fact:   .WORD   d#1
    
    main:   LOADA   d#7, i
            STOREA  n, d
    L1:     COMPA   d#0, i
            BREQ    L2
            JSR     mul
            LOADA   n, d
            SUBA    d#1, i
            STOREA  n, d
            BR      L1
    L2:     DECO    fact, d
            CHARO   h#0a, i
            STOP
    
    i:      .EQUATE d#0
    p:      .EQUATE d#2
    mul:    ADDSP   d#-4, i
            LOADA   d#0, i
            STOREA  p, s
            STOREA  i, s
    L3:     COMPA   n, d
            BREQ    L4
            LOADA   p, s
            ADDA    fact, d
            STOREA  p, s
            LOADA   i, s
            ADDA    d#1, i
            STOREA  i, s
            BR      L3
    L4:     LOADA   p, s
            STOREA  fact, d
            ADDSP   d#4, i
            RTS
            .END
    

  4. Modify the above program so that the subroutine mul doesn't use the global variables n and fact; instead, it should take the values of n and fact as parameters, and produce the new value of fact as a return value. Show both the modifications necessary to mul and to main.