Translate the following C code to MIPS assembly code. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds the base address of the array D and its value is 0x10 (1016). Thus, D[j] contains the value at address 0x10 + 4 * j.for(i=0; i<a; i++)for(j=0; j<b; j++)D[j] = i + j;
Question
Translate the following C code to MIPS assembly code. Assume that the values of a, b, i, and j are in registers s1, t1, respectively. Also, assume that register $s2 holds the base address of the array D and its value is 0x10 (1016). Thus, D[j] contains the value at address 0x10 + 4 * j.for(i=0; i<a; i++)for(j=0; j<b; j++)D[j] = i + j;
Solution
Here is the MIPS assembly code for the given C code:
# Initialize i to 0
addi $t0, $zero, 0
# Outer loop
outer_loop:
# Check if i < a
slt $t2, $t0, $s0
beq $t2, $zero, end_outer_loop
# Initialize j to 0
addi $t1, $zero, 0
# Inner loop
inner_loop:
# Check if j < b
slt $t3, $t1, $s1
beq $t3, $zero, end_inner_loop
# Calculate i + j
add $t4, $t0, $t1
# Calculate the address of D[j]
sll $t5, $t1, 2
add $t5, $s2, $t5
# Store i + j into D[j]
sw $t4, 0($t5)
# Increment j
addi $t1, $t1, 1
# Jump back to the start of the inner loop
j inner_loop
end_inner_loop:
# Increment i
addi $t0, $t0, 1
# Jump back to the start of the outer loop
j outer_loop
end_outer_loop:
This code first initializes i to 0 and then enters the outer loop. It checks if i is less than a, and if it is not, it jumps to the end of the outer loop. Then it initializes j to 0 and enters the inner loop. It checks if j is less than b, and if it is not, it jumps to the end of the inner loop. It then calculates i + j and the address of D[j], stores i + j into D[j], increments j, and jumps back to the start of the inner loop. After the inner loop finishes, it increments i and jumps back to the start of the outer loop.
Similar Questions
For the following C statement, what is the correspondingMIPS assembly code? Assume that the variables f, g, h, i, and j are assigned toregisters $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base addressof the arrays A and B are in registers $s6 and $s7, respectively.B[8] = A[i−j];2.4 [5] <§§2.2, 2.3> For the MIPS assembly instructions below, what is thecorresponding C statement? Assume that the variables f, g, h, i, and j are assignedto registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base addressof the arrays A and B are in registers $s6 and $s7, respectively.sll $t0, $s0, 2 # $t0 = f * 4add $t0, $s6, $t0 # $t0 = &A[f]sll $t1, $s1, 2 # $t1 = g * 4add $t1, $s7, $t1 # $t1 = &B[g]lw $s0, 0($t0) # f = A[f]addi $t2, $t0, 4lw $t0, 0($t2)add $t0, $t0, $s0sw $t0, 0($t1)2.5 [5] <§§2.2, 2.3> For the MIPS assembly instructions in Exercise 2.4, rewritethe assembly code to minimize the number if MIPS instructions (if possible)needed to carry out the same function.2.6 The table below shows 32-bit values of an array stored in memory.Address Data24 238 432 336 640 1
If the following registers have the specified values,R0 = 0x00000000R1 = 0x00000005R2 = 0x00000002Then the result of MULL R0, R1, R2 will be ___________ and the result is stored in _______.a.0x00000010 & R0b.0x00000010 & R1c.0x0000000A & R1d.0x0000000A & R0
The variables f and g are assigned to the registers X3 and X4, respectively in these ARM instructions. Loop: SUBS XZR, X3, X4 B.GE Exit LSL X3, X3, 1 B LoopExit: What are the corresponding statements in the C language?
In the MIPS assembly language, which of the following is a valid register name?$t0$s1$a2$x4$v0Group of answer choicesII, III, IVIV, VI, II and IIII, IV, V
<§2.11> Write the MIPS assembly code to implement the following Ccode:lock(lk);shvar=max(shvar,x);unlock(lk);Assume that the address of the lk variable is in $a0, the address of the shvarvariable is in $a1, and the value of variable x is in $a2. Your critical section shouldnot contain any function calls. Use ll/sc instructions to implement the lock()operation, and the unlock() operation is simply an ordinary store instruction.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.