I have a problem in converting my C code to MIPS code. The original C code is below:
int A[4];
int i;
int diff;
for(i=0; i<3; i++){
diff = A[i+1] - A[i];
if (diff > 0)
A[i] = 5*A[i];
else
A[i+1] = -5*A[i];
}
And here is my MIPS Code below:
### MIPS PROJECT PART 1: ARRAY USING FOR LOOPS
.data
# allocate 16 bytes memory for 4 integer array
A: .space 16
.text
# Store array values in registers
addi $s0,$zero,2
addi $s1,$zero,4
addi $s2,$zero,6
addi $s3,$zero,8
# Index = $t0
addi $t0,$zero,0
# Store the first index and then store others by increasing $t0 by 4 bytes
sw $s0,A($t0)
addi $t0,$t0,4
sw $s1,A($t0)
addi $t0,$t0,4
sw $s2,A($t0)
addi $t0,$t0,4
sw $s3,A($t0)
main:
li $s4,0 # counter i = $s4
li $t2,3 # constant $t2
loop:
beq $s4, $t2, end # if t1 == 3 we are done
# diff = A[i+1] - A[i]
lw $t3,A($zero) # $t3 = A[i]
addi $t6, $zero,4 # next element A[i+1]
lw $t4,A($t6) # $t4 = A[i+1]
sub $t5,$t4,$t3 # dif = $t5 = A[i+1] - A[i]
# if (diff > 0)
bltz $t5, Else
# A[i] = 5*A[i]
add $t5,$zero,$t5
add $t5,$zero,$t5
add $t5,$zero,$t5
add $t5,$zero,$t5
sw $t5 A($zero)
Else:
# else A[i+1] = -5 * A[i];
addi $zero, $zero,4 #next element A[i+1]
#Loop body
addi $s4, $s4, 1 # add 1 to $s4
j loop # jump back to the top
end:
li $v0,10
syscall
The thing is that program does not update array values. Additionally I am restricted to use multiplication commands (mult or mull), that is why I am trying just to add it 5 times in control statement.
See Question&Answers more detail:os