## CS 2011, A Term 1999 Professor Sergio A. Alvarez HW 6

1. Write an assembly language procedure named addthreebyref that accepts three singleword integer arguments x, y, z by reference on the stack and returns the sum x + y + z of the arguments in x, again by reference. The procedure addthreebyref should do its own stack clean-up.

2. A C++ program contains the following call to an external assembly procedure:
```   cryptic(x,y,z);
```
Assume that the parameters x, y, z are short integers that occupy two bytes of memory each. Assume also that x and y are passed by reference, whereas z is passed by value. Standard C naming and calling conventions are assumed to be in effect. The C++ prototype of the assembly procedure is:
```   extern "C" void cryptic(int& x, int& y, int z);
```

• (a) Give the full proc / endp directives for the assembly procedure. Do not include any explicit parameter declarations.

• (b) Write assembly instructions that set up a stack frame within the assembly procedure and then proceed to copy the *values* (not the addresses) of the parameters x, y, z to registers ax, bx, cx, respectively. Previous values of ax, bx, cx should be saved for future use.

• (c) Where is stack clean-up performed in the present context? Give assembly-level code that performs this task.

• (a) Using only NAND gates, construct a logic circuit that computes the Boolean function
```   f(A,B,C) = ABC' + C
```

• (b) Repeat (a) using only NOR gates.

• (c) Use a Karnaugh map to simplify the Boolean expression
```   A'B'C'D + A'B'CD + A'BC'D + A'BCD
```

• (d) Repeat (c) for the Boolean expression
```   A'B'C'D + A'B'CD + AB'C'D + AB'CD
```

3. Consider the Boolean function AtMostTwo(A,B,C,D) that returns TRUE whenever at most two (none, one, or exactly two) of its input variables A,B,C,D are TRUE, and returns FALSE otherwise.

• (a) Construct the truth table for AtMostTwo.

• (b) Give the disjunctive normal form (standard sum of products expression) for AtMostTwo, based on its truth table.

• (c) Draw a Karnaugh map for AtMostTwo, and use it to find a simplified equivalent expression for AtMostTwo.

4. Consider the latch circuit discussed in the lectures, containing two NOR gates, the first having output Q and inputs Q~, R, the second having output Q~ and inputs Q, S.

• (a) Give the output values Q, Q~ if the input values are R=0, S=1.

• (b) Give the output values Q, Q~ if the input values are R=1, S=0.

• (c) Give the output values Q, Q~ if the input values are R=1, S=1.

• (d) Describe any state changes that occur if the initial values are as in (a), (b), (c) and if the input values then simultaneously drop to 0 (consider cases (a), (b), (c) separately).

• (e) Show how to build a bistable latch circuit using only two NAND gates. Explain carefully what the inputs and outputs of the circuit are, how the NAND gates are connected, and what input values should be used to set and to reset the latch.