36.9k views
5 votes
For the following C statement, what is the corresponding MIPS assembly code? Assume that the C variables f,g, and have already been placed in registers $s0,$s1,$s2, respectively. Use a minimal number of MIPS assembly instructions. f=g+(h∗5)

1 Answer

2 votes

Final answer:

The corresponding MIPS assembly code for the given C statement, f=g+(h*5), can be written using a few MIPS assembly instructions.

Step-by-step explanation:

The corresponding MIPS assembly code for the given C statement, f=g+(h*5), can be written as:

addi $t0, $s2, 0 # $t0 = h
sll $t0, $t0, 2 # $t0 = h * 4
add $t1, $t0, $s1 # $t1 = g + (h * 4)
add $t2, $s0, $t1 # $t2 = f = g + (h * 4)

This code assumes that the variables f,g, and h are stored in registers $s0, $s1, and $s2 respectively. The code first multiplies the value in register $s2 (containing the value of h) by 4 using the left shift logical operator 'sll'. The result is stored in register $t0. Then, it adds the values in registers $s1 (containing g) and $t0, and stores the result in register $t1. Finally, it adds the values in registers $s0 (containing f) and $t1, and stores the final result in register $t2.

User Exhuma
by
9.1k points