83.1k views
1 vote
Consider the following C statement. f = (g + h) + (g - i) + j Assume that the variables f, g, h, i, and j are assigned into the registers $s0, $s1, $s2, $s3, and $s4 respectively. a. Convert into MIPS code. Show your work.

1 Answer

1 vote

Final answer:

The C statement f = (g + h) + (g - i) + j is converted into MIPS code by using add and sub instructions with temporary registers to store intermediate results before computing the final value into the register for f.

Step-by-step explanation:

The C statement provided can be converted into MIPS assembly language using registers for the variables as specified. The operations performed in the expression are addition and subtraction which can be directly translated into MIPS instructions add and sub.

Here is the MIPS code for the given C statement:


  • add $t0, $s1, $s2 # temp0 = g + h

  • sub $t1, $s1, $s3 # temp1 = g - i

  • add $t2, $t0, $t1 # temp2 = temp0 + temp1

  • add $s0, $t2, $s4 # f = temp2 + j

The first two instructions use temporary registers to store the intermediate results of the addition and subtraction. Then, the intermediate results are added together, and finally added to the fourth variable (j) to produce the result into the register designated for f.

User Mark Sherretta
by
7.8k points