33.3k views
4 votes
Assume that k corresponds to register $s0, n corresponds to register $s2 and the base of the array v is in $s1. What is the MIPS assembly code corresponding to this C segment

User Mcfinnigan
by
5.0k points

1 Answer

0 votes

Answer:

hello your question lacks the C segment so here is the C segment

while ( k<n )

{v[k] = v[k+1];

k = k+1; }

Answer : while:

bge $s0, $s2, end # while (k < n)

addi $t0, $s0, 1 # $t0 = k+1

sll $t0, $t0, 2 # making k+1 indexable

add $t0, $t0, $s1 # $t0 = &v[k+1]

lw $t0, 0($t0) # $t0 = v[k+1]

sll $t1, $s0, 2 # making k indexable

add $t1, $t1, $s1 # $t1 = &v[k]

sw $t0, 0($t1) # v[k] = v[k+1]

addi $s0, $s0, 1

j while

end:

Step-by-step explanation:

The MIPS assembly code corresponding to the C segment is

while:

bge $s0, $s2, end # while (k < n)

addi $t0, $s0, 1 # $t0 = k+1

sll $t0, $t0, 2 # making k+1 indexable

add $t0, $t0, $s1 # $t0 = &v[k+1]

lw $t0, 0($t0) # $t0 = v[k+1]

sll $t1, $s0, 2 # making k indexable

add $t1, $t1, $s1 # $t1 = &v[k]

sw $t0, 0($t1) # v[k] = v[k+1]

addi $s0, $s0, 1

j while

end:

User Manimino
by
4.7k points