Name: Box: Date:

HW2 solution

  1. (20 points) Convert the following C code to MIPS assembly instructions. Use the minimum number of instructions necessary. Assume that variables f, g and h are 32- bit integers stored in registers $t0, $t1 and $t2 respectively and that the base address of arrays A and B are in registers $t8 and $t9 respectively. A and B are arrays of 4-byte integers (this is important).

    Be careful not to modify the variables unintentionally. If you need to store temporary values, use one of the other t registers.

    a. f = g + h;

    add $t0, $t1, $t2

    b. f = g - (h - 5);

    addi $t5, $t2, -5
    sub  $t0, $t1, $t5

    c. Remember, the elements of A are 4-bytes big!
    f = A[3] - g;

    lw   $t5, 12($t8)
    sub  $t0, $t5, $t1

    d. A[0] = f + B[1];

    lw   $t5, 4($t9)
    add  $t5, $t0, $t5
    sw   $t5, 0($t8)