Assignment 9


P1. Problem A.2 <§A.6> in Hennessy and Patterson.

      Rewrite the code for fact to use fewer instructions.

 

P2. Problem A.6 <§A.9> in Hennessy and Patterson.

      Using SPIM, write and test an adding machine program that repeatedly reads in integers and adds them into a running sum. The program should stop when it gets an input that is 0, printing out the sum at that point. Use the SPIM system calls described on pages A-43 and A-45.

 

P3. Problem 2.6 <§2.5> in Hennessy and Patterson.

       Some computers have explicit instructions to extract an arbitrary field from a 32-bit register and to place it in the least significant bits of a register. The figure below shows the desired operation:

Find the shortest sequence of MIPS instructions that extracts a field for the constant values i = 5 and j = 22 from register $t3 and places it in register $t0. (Hint: It can be done in two instructions)

 

P4. Problem 2.34 <§§2.3, 2.6, 2.9> in Hennessy and Patterson.

       The following program tries to copy words from the address in register $a0 to the address in register $a1, counting the number of words copied in register $v0. The program stops copying when it finds a word equal to 0. You do not have to preserve the contents of registers $v1, $a0, and $a1. This terminating word should be copied but not counted

            addi   $v0, $zero, 0        # Initialize count

Loop:   lw      $v1, 0($a0)          # Read next word from source

            sw     $v1, 0($a1)           # Write to destination

            addi   $a0, $a0, 4           # Advance pointer to next source

            addi   $a1, $a1, 4           # Advance pointer to next destination

            beq    $v1, $zero, loop   # Loop if word copied !=zero

There are multiple bugs in this MIPS program; fix them and turn in a bug-free version. Like many of the exercises in this chapter, the easiest way to write MIPS program is to use the simulator described in Appendix A.

 

P5. Problem 2.38 <§§2.9, 2.10> in Hennessy and Patterson.

       Given your understanding of PC-relative addressing, explain why an assembler might have problems directly implementing the branch instruction in the following code sequence:

            Here:                beq       $s0, $s2, there

            ….

            There               add      $s0, $s0, $s0

Show how the assembler might rewrite this code sequence to solve these problems.