# Tutorial 2, Week 6, 18-23 August, 1997

You should do the problems if you can before going to the tutorial classes. You must DO* problems 1, 2, 8, and hand in the solutions at the tutorial class. Tutorial hours are for asking questions and clarifying points of the tutorial problems. The number in parentheses ( ) refers to the question number in the textbook, ``Computer Organization & Design'', Patterson and Hennessy. The number in square brackets [ ] is a measure of the level of difficult of the problem. This set of problems come from the textbook page 155-157, Exercises 3.1 to 3.4, 3.8 to 3.10, and 3.12 to 3.17.
1. DO* (3.1)  In some cases a simple instruction set like MIPS can synthesize instructions found in richer instruction sets such as the VAX. The following VAX instruction decrements register \$5:
```decl \$5  # register \$5 = \$5 - 1
```
The operation is described in the comment of the instruction to help explain the operation. What is the single MIPS instruction, or if it cannot be represented in a single instruction, the shortest sequence of MIPS instructions, that performs the same operation?
2. DO* (3.2)  This is the same as Exercise 3.1, except this VAX instruction clears register \$5:
```clrl \$5  # register \$5 = 0
```
3. (3.3)  This is the same as Exercise 3.1, except this VAX instruction clears memory location 1000:
```clrl 1000  # memory = 0
```
4. (3.4)  This is the same as Exercise 3.1, except this VAX instruction adds 1 to register \$5, place the sum back in register \$5, compares the sum to register \$6, and then branches to L1 if \$5 < \$6:
```aoblss \$6, \$5, L1  # \$5 = \$5 + 1; if (\$5 < \$6) goto L1
```
5. (3.8)  The following program tries to copy words from the address in register \$4 to the address in register \$5, counting the number of words copied in register \$2. The program stops copying when it finds a word equal to 0. You do not have to preserve the contents of register \$3, \$4, and \$5. This terminating word should be copied but not be counted.
```loop: lw   \$3, 0(\$4)    # Read next word from source
addi \$2, \$2, 1    # Increment count words copied
sw   \$3, 0(\$5)    # Write to destination
bne  \$3, \$0, loop # Loop if word copied is not zero
```
There are multiple bugs in this MIPS program. Fix them and turn in a bugfree version of this program. Like many of the exercises in this chapter, the easiest way to write MIPS programs is to use the simulator described in Appendix A (The program can be evoked by xspim).
6. (3.9)  Using the MIPS program in Exercise 3.8 (with bugs intact), determine the instruction format for each instruction and the decimal values of each instruction field (Refer to Figure 3.16 for the MIPS machine language encoding).
7. (3.10)  Starting with the corrected program in the answers to Exercise 3.8, write the C code segment that might have produced this code. Assume variable source corresponds to register \$4, the variable destination corresponds to register \$5, and the variable count corresponds to register \$2. Show variable declarations, but assume that source and destination have been initialized to the proper addresses.
8. DO* (3.12-3.17)  There are six relative conditions between the values of two registers. Assuming that variable i corresponds to register \$19 and variable j to \$20, show the MIPS code for the condition corresponding to these C codes:
```if (i == j) goto L1;
if (i != j) goto L1;
if (i  < j) goto L1;
if (i <= j) goto L1;
if (i  > j) goto L1;
if (i >= j) goto L1;
```
Note that you should use only beq, bne and slt.