# Homework #2 -- due Wed Feb 1

Do the following exercises. Remember to show your work.

1. The MIPS translation of the C segment:
```       while (save[i] == k)
i = i + 1;
```
on page 74 uses both a conditional branch and an unconditional jump each time through the loop. Only poor compilers would produce code with this loop overhead. Rewrite the assembly code so that it uses at most one branch or jump each time through the loop. How many instructions are executed before and after the optimization if the number of iterations of the loop is 10 (i.e. save[i], save[i+1], ..., save[i+9} equal k, but save[i+10} does not equal k)?
2. 2.29. Here is the raw code for you to comment.
3. 2.30. You can use the same sheet given in 2.29 above.
4. 2.37. A few hints:
• You are supposed to show what real MIPS instruction (or sequence of instructions) the compiler would produce for each pseudo-instruction. For instance, given the instruction
```      li \$t1, small
```
you look in the table and see that this instruction should have the effect of
```      \$t1 = small
```
where we know that 'small' is a number that fits within 16 bits ('li' stands for 'load immediate'). So your answer would be that the compiler might produce
```      addi \$t1, \$zero, small
```
• A "big" number needs 32 bits to be represented, as the instructions say. You will need some notation to talk about the upper (most significant) 16 bits and the lower (least significant) 16 bits of this number. I suggest UPPER(large) to refer to the most significant 16 bits, and LOWER(large) to refer to the other bits.
• To make your answers simpler, you may make use of the 'li' pseudo-instruction where helpful (except when defining 'li' itself).

General homework requirements

• Every assginment (homework/lab/project) must have a completed coversheet stapled to the front of your assignment. This is worth 10% of your grade.
• Collaboration is encouraged per course policy, but you are required to document who you worked with on your cover sheet.
• Show your work. A number alone is never an answer.
• Be sure to use the third edition of the textbook -- problems are similar but not the same as the second edition!