202k views
4 votes
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)
lw $t0,0($t0)
add $s0,$t1,$t0

1 Answer

2 votes

Answer:

f = 2 * (&A[0])

See explaination for the details.

Step-by-step explanation:

The registers $s0, $s1, $s2, $s3, and $s4 have values of the variables f, g, h, i, and j respectively. The register $s6 stores the base address of the array A and the register $s7 stores the base address of the array B. The given MIPS code can be converted into the C code as follows:

The first instruction addi $t0, $s6, 4 adding 4 to the base address of the array A and stores it into the register $t0.

Step-by-step explanation:

If 4 is added to the base address of the array A, then it becomes the address of the second element of the array A i.e., &A[1] and address of A[1] is stored into the register $t0.

C statement:

$t0 = $s6 + 4

$t0 = &A[1]

The second instruction add $t1, $s6, $0 adding the value of the register $0 i.e., 32 0’s to the base address of the array A and stores the result into the register $t1.

Step-by-step explanation:

Adding 32 0’s into the base address of the array A does not change the base address. The base address of the array i.e., &A[0] is stored into the register $t1.

C statement:

$t1 = $s6 + $0

$t1 = $s6

$t1 = &A[0]

The third instruction sw $t1, 0($t0) stores the value of the register $t1 into the memory address (0 + $t0).

Step-by-step explanation:

The register $t0 has the address of the second element of the array A (A[1]) and adding 0 to this address will make it to point to the second element of the array i.e., A[1].

C statement:

($t0 + 0) = A[1]

A[1] = $t1

A[1] = &A[0]

The fourth instruction lw $t0, 0($t0) load the value at the address ($t0 + 0) into the register $t0.

Step-by-step explanation:

The memory address ($t0 + 0) has the value stored at the address of the second element of the array i.e., A[1] and it is loaded into the register $t0.

C statement:

$t0 = ($t0 + 0)

$t0 = A[1]

$t0 = &A[0]

The fifth instruction add $s0, $t1, $t0 adds the value of the registers $t1 and $t0 and stores the result into the register $s0.

Step-by-step explanation:

The register $s0 has the value of the variable f. The addition of the values stored in the regsters $t0 and $t1 will be assigned to the variable f.

C statement:

$s0 = $t1 + $t0

$s0 = &A[0] + &A[0]

f = 2 * (&A[0])

The final C code corresponding to the MIPS code will be f = 2 * (&A[0]) or f = 2 * A where A is the base address of the array.

User Jytesh
by
5.1k points