97.2k views
4 votes
Translate the following two separate C Codes into MIPS code

a)y=h+g-1 (y, g and h are in $50,$S1 and $S2)
b)B=A[2]+4 (Base address of A is in $50, B is in $S1)
c)for(i =0 i <8, i++){
Sum =Sum-i
Result = Result +i * 4
}
d)Assume that g, c, i are in $s0, $s1, $s2 registers. base of B is in $s3.
i)c=B[i]x2
ii)g=g-C+2
iii)if (i ==1)
i++;
else
i--;
e) Translate the following two separate C Codes into MIPS code If (g

User Leompeters
by
9.5k points

1 Answer

6 votes

Final answer:

The C code expressions have been translated into MIPS assembly code, addressing simple arithmetic operations, array access, looping constructs, and conditional branching, with the use of various MIPS instructions and registers.

Step-by-step explanation:

Translating the given C code into MIPS assembly language involves understanding basic MIPS instructions and register usage.

  1. For the expression y = h + g - 1, assuming y is in $s0, h is in $s2, and g is in $s1:
  2. add $s0, $s2, $s1 # y = h + g
  3. addi $s0, $s0, -1 # y = y - 1
  4. For accessing the array element B = A[2] + 4, where the base address of A is in $s0 and B in $s1:
  5. lw $s1, 8($s0) # Load word from array A at index 2 into $s1
  6. addi $s1, $s1, 4 # Add 4 to the value
  7. For the loop that iterates 8 times modifying Sum and Result:
  8. Loop starts with setting i to 0.
  9. End condition checks if i is less than 8.
  10. Sum and Result are updated according to C code logic in the loop body.
  11. Finalized with incrementing i by 1.
  12. For the complex expressions involving accessing an array, updating values, and conditional branching:
  13. Each part i, ii, and iii are translated into corresponding MIPS instructions handling the array access, arithmetic operations, and branching logic as per the C code logic.
  14. The conditional expression involving < operator and an assignment is translated into a MIPS assembly branch instruction followed by the relevant assignments.
User Khurram Shehzad
by
8.8k points