In: Computer Science
Complete the corresponding assembly language fragments by selecting the correct instruction, register, or value: (Choices in bold)
a) Consider the following fragment of C code:
if(a == b) {
x += 10;
A[50] = A[50] +x;
}
else {
y += 10;
A[50] = A[50] + y;
}
Assume that variables a, b, x and y are assigned to $s0, $s1, $s2 and $s3 respectively and the base address for array A is in $s4. Only register $t0 is used for storing results temporarily.
bne $s0, ($s0 / $s1 / $s2 / $s3 / $s4 / $t0), else
addi $s2, $s2, 10
lw $t0, (0 / 10 / 50 / 200 / $s1 / $t0) ($s4)
add $t0, $t0, $s2
sw $t0, 200($s4)
j Exit
else: addi $s3, $s3, (0 / 10 / 50 / 200 / $s1 / $t0)
lw $t0, 200($s4)
add $t0, 200($s4)
add $t0, $t0, ($s0 / $s1 / $s2 / $s3 / $s4 / $t0)
(beq / bne / j / add / addi / sll / lw / sw) $t0, 200($s4)
Exit:
b) Consider the following fragment of C code:
while (A[i] == 0) {
A[i] = A[i] + 10;
i += 1;
}
Assume that variable i is assigned to $s0 and the base address for array A is in $s1. A is an array of words. Registers $t0 and $t1 are used for storing results temporarily.
Loop: sll $t0, $s0, (0 / 1 / 2 / 4 / 10)
add $t0, $t0, $s1
lw $t1, 0( ($s0 / $s1 / $t0 / $t1 / $zero) )
bne $t1, $zero, (Loop / Exit)
addi $t1, ($s0 / $s1 / $t0 / $t1 / $zero), 10
sw $t1, 0($t0)
addi ($s0 / $s1 / $t0 / $t1 / $zero), $s0, 1
j Loop
Exit;
The answer for the above mentioned question is explained below ::
Explanation is as follows ::