In: Computer Science
4.Translate the following C code to MIPS assembly code. Assume that the value of i is in register $t0, and $s0 holds the base address of the integer MemArray
if (i > 10)
MemArray[i] = 0;
else
MemArray[i] = -MemArray[i];
6.Translate the following C code to MIPS assembly code. Use a minimum number of instructions. 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.
for(i=0; i<a; i++)
for(j=0; j<b; j++)
D[j] = i + j;
I considered it as word directive; if any other accordingly offset will change
4)
li $t1, 10
sll $t2, $t0, 2
#offset for i
add $t3, $s0, $t2 #address of
MemArray[i]
bgt $t0,$t1,IF_CON #condition
ELSE:
#else body
lw $t4,0($t3)
sub $t4, $0, $t4 #changing sign
sw $t4,0($t3)
j NEXT
IF_CON:
#if body
li $t4,0
sw $t4,0($t3) #storing
zero
NEXT:
6)
li $t0,0
#i=0
OUTER_FOR:
bge $t0,$s0,EXIT_OUTER_FOR
#outer loop condition
li $t1,0
#j=0
INNER_FOR:
bge $t1,$s1,EXIT_INNER_FOR
#outer loop condition
sll $t2, $t1, 2
#offset for i
add $t3, $s2, $t2
#address of D[j]
add $t4, $t0,$t1 #
i+j
sw $t4,0($t3)
# D[j] = i+j
addi $t1,$t1,1
#j++
j INNER_FOR
EXIT_INNER_FOR:
addi $t0,$t0,1
#i++
j OUTER_FOR
EXIT_OUTER_FOR: