Appendix A describes the MIPS simulator, which is helpful for these
exercises. Although the simulator accepts pseudoinstructions, try not
to use pseudoinstructions for any exercises that ask you to produce
MIPS code. Your goal should be to learn the real MIPS instruction
set, and if you are asked to count instructions, your count should
reflect the actual instructions that will be executed and not the
pseudoinstructions.
There are some cases where pseudoinstructions must be used (for
example, the la instruction when an actual value is not known at
assembly time). In many cases, they are quite convenient and result
in more readable code (for example, the li and move instructions). If
you choose to use pseudoinstructions for these reasons, please add a
sentence or two to your solution stating which pseudoinstructions
you have used and why.
2.1 [5] <§2.2> For the following C statement, what is the
corresponding MIPS assembly code? Assume that the C
variables f, g, and h, have already been placed in registers $s0,
$s1, and $s2, respectively. Use a minimal number of MIPS
assembly instructions.
f = g + (h – 5);
2.2 [5] <§2.2> Write a single C statement that corresponds to the
two MIPS assembly instructions below.
add f, g, h
add f, i, f
2.3 [5] <§§2.2, 2.3> For the following C statement, write the
corresponding MIPS assembly code. Assume that the variables
f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and
$s4, respectively. Assume that the base address of the arrays A
and B are in registers $s6 and $s7, respectively.
B[8] = A[i–j];
2.4 [5] <§§2.2, 2.3> For the MIPS assembly instructions above,
what is the corresponding C statement? Assume that the
, variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2,
$s3, and $s4, respectively. Assume that the base address of the
arrays A and B are in registers $s6 and $s7, respectively.
2.5 [5] <§2.3> Show how the value 0xabcdef12 would be
arranged in memory of a li le-endian and a big-endian
machine. Assume the data are stored starting at address 0 and
that the word size is 4 bytes.
2.6 [5] <§2.4> Translate 0xabcdef12 into decimal.
2.7 [5] <§§2.2, 2.3> Translate the following C code to MIPS.
Assume that the variables f, g, h, i, and j are assigned to
registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that
the base address of the arrays A and B are in registers $s6 and
$s7, respectively. Assume that the elements of the arrays A and
B are 8-byte words:
B[8] = A[i] + A[j];
2.8 [10] <§§2.2, 2.3> Translate the following MIPS code to C.
Assume that the variables f, g, h, i, and j are assigned to
registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that
the base address of the arrays A and B are in registers $s6 and
$s7, respectively.
addi $t0, $s6, 4
add $t1, $s6, $0
sw $t1, 0($t0)