I am trying to convert code that I have written in java into MIPS Assembly, however there seems to be an issue where I am losing the values calculated in a function when I return. Logically what I have written seems like it should work (to me at least).
For some context, a range will be entered by a user. In $s0 I store the smaller, $s1 the larger. I am checking for amicable numbers in the range.
Currently I am getting an infinite loop, I think it is because $s6 is getting reset to zero after a function call but I do not know how to resolve it. Any input would be greatly appreciated.
isAmicable:
li $s5, 0 #counter for number of amicable pairs
add $s6, $s0, $zero
amicableLoop:
bge $s6, $s1, final_output
jal isFactor
add $s3, $s4, $zero
move $s6, $s3
jal isFactor
bne $s6, $s4, increment
bge $s6, $s3, increment
bge $s3, $s1, increment
j amicablePair
increment:
addi $s6, $s6, 1
j amicableLoop
isFactor:
li $s4, 0 # => The sum of all proper divisors of A
li $s2 , 1 # => start here with checks for devisors
sumLoop:
bge $s2, $s6, return_sum # while $s2 < $s1
rem $t0, $s6, $s2 # $t0 = $s1 % $s2
bne $t0, $0, while
addu $s4, $s4, $s2 # $s1 += $s2
while:
addi $s2, $s2, 1 # $s2++
j sumLoop
return_sum:
jr $ra