I am trying to convert a C program into a MIPS assembly program. The following is my C code for the program: (Note: Bulbs[number] is an array initialized to all zero values for a "number" entered by the user)
for(int i = 1; i <= number; i++)
for(int j = 1; j <= number; j++)
if(j % i == 0)
Bulbs[j-1] = (Bulbs[j-1] + 1) % 2;
What I have so far is as follows:
li $t0, 0 #$t0 is set to 0 to be used as index for for loop1
li $t1, 0 #$t1 is set to 0 to be used as index for for loop2
li $s2, 4 #integer 4 is stored in s2
mult $s3, $s2 #input number($s3) is multiplied by 4
mflo $s4 #result of multiplication is stored in $s4
loop1:
bgt $t0, $s4, end_loop1 #if t$0 > $s4(input number*4), exit loop1,
#performing multiplication by 4 since word occupies 4 bytes
addi $t3, $t3, 1 #t3 is initialized to serve as "i" from for loop1
loop2:
bgt $t1, $s4, end_loop2 #if $t1 > (input number*4), exit loop2
addi $t4, $t4, 1 #t4 is initialized to serve as "j" from for loop2
div $t4, $t3
mfhi $t5 #$t4 % $t3 is stored in $t5
bne $t5, $zero, if_end #checking for if condition
if_end:
addi $t1, $t1, 4 #increment $t1 by 4 to move to next array element
j loop2 #jump back to top of loop2
end_loop2:
addi $t0, $t0, 4 #increment $t0 by 4
j loop1 #jump back to the top of loop1
end_loop1:
I think my for-loop implementation works and I have the if conditional accurately set-up (correct me if I am wrong), but I do not know how I can implement the 'Bulbs[j-1] = (Bulbs[j-1] + 1) % 2;' line after my if conditional. I am new to MIPS and would appreciate any help or feedback!