我一直试图将用C语言编写的程序转换为MIPS。该程序计算用户输入范围内的友好数字。我已经在C语言中运行得不错,但是除了吸收用户输入之外,我还没有使其在MIPS中工作。如果输入正确,程序将运行而不会终止。我一直在尝试修复循环,因为我猜测它会卡在某个地方。我已经在下面写下了所有内容,并且一切正常,直到我的amicable_loop为止。对于我要去哪里的任何帮助,将不胜感激。
.text
main:
#--------------------------------------------------------------------------------
#for start
li $v0, 4 #print string syscall
la $a0, start_msg #loads addr of message into $a0
syscall
#reads in start
li $v0, 5 #gets start index from user
syscall
add $s0, $v0, $zero #sets $s0 = start index
add $a0, $s0, $zero #check if start < 0
jal error_check #jump to error
#for end
li $v0, 4 #print string syscall
la $a0, end_msg #loads addr of message into $a0
syscall
#reads in end
li $v0, 5 #gets end index from user
syscall
add $s1, $v0, $zero #sets $s1 = end index
add $a0, $s1, $zero #check if end < 0
jal error_check #jump to error
#check if end > start
slt $t0, $s1, $s0 #check if end index is less than start index
beq $t0, $zero, no_swap
li $v0, 4
la $a0, switch_msg #printing swap message
syscall
#swapping start and end values
add $t0, $s0, $zero
add $s0, $s1, $zero
add $s1, $t0, $zero
error_check: #check if start/end <0
#--------------------------------------------------------------------------------
slti $t0, $a0, 1
bne $t0, $zero, error_true
jr $ra
error_true: #start/end is less than zero
#--------------------------------------------------------------------------------
#print error message
li $v0, 4 #print string syscall
la $a0, err_msg #for error message
syscall
#print exit message
li $v0, 4
la $a0, exit_msg
syscall
#exit program
li $v0, 10
syscall
no_swap:
#--------------------------------------------------------------------------------
add $s3, $s0, $zero #to start loop; i = start index
amicable_loop:
#--------------------------------------------------------------------------------
bgt $s3, $s1, amicable_loop_end #if i <= end index
add $a0, $s3, $zero #for i
add $a1, $s1, $zero #for end
jal isAmicable
beq $v0, $zero, not_amicable #if not amicable
addi $s2, $s2, 1 #incrementing pairs
not_amicable:
#--------------------------------------------------------------------------------
addi $s3, $s3, 1 #i++
j amicable_loop
amicable_loop_end:
#--------------------------------------------------------------------------------
# print range
li $v0, 4
la $a0, range
syscall
#print start
add $a0, $s0, $zero
li $v0, 1
syscall
#print minus
li $v0, 4
la $a0, minus
syscall
#print end
add $a0, $s1, $zero
li $v0, 1
syscall
#print enter (\n)
li $v0, 4
la $a0, enter
syscall
#print number of pairs
li $v0, 4
la $a0, total
syscall
#print pairs integer
add $a0, $s2, $zero
li $v0, 1
syscall
#call for exiting
li $v0, 10
syscall
isFactor:
#find factors of number
#-------------------------------------------------------------------------------
rem $t5, $a1, $a0 #num % factor
beq $t5, $zero, factor_true #branch if factor true
add $v0, $zero, $zero
jr $ra
factor_true:
#-------------------------------------------------------------------------------
addi $v0, $zero, 1
jr $ra #return false if remainder != 0
sumFactors:
#-------------------------------------------------------------------------------
#$t0 - num, $t1 - result, $t2 - i
addi $sp, $sp, -4 #for return
sw $ra, 0($sp)
addi $sp, $sp, -4 #for num
sw $a0, 0($sp)
add $t0, $a0, $zero #$t0 = num
addi $t1, $zero, 1 #result = 1
addi $t2, $zero, 2 #i = 2 for loop
j sumLoop #might need to delete
sumLoop:
#-------------------------------------------------------------------------------
mul $t3, $t2, $t2 #$t3 = i*i
bgt $t3, $t0, sum_exit #i*i <= n
#call isfactor
add $a0, $t2, $zero #putting i into isFactor
add $a1, $t0, $zero #putting num into isFactor
jal isFactor
addi $t1, $t2, 1 #result+=k
div $t3, $t0, $t2 #$t3 = num/i
#check if div != k
bne $t3, $t2, other_factor
addi $t2, $t2, 1 #i++
j sumLoop
other_factor:
#-------------------------------------------------------------------------------
addi $t1, $t3, 1 #result+=div
addi $t2, $t2, 1 #i++
j sumLoop
sum_exit:
#-------------------------------------------------------------------------------
jr $ra #return result
isAmicable:
#-------------------------------------------------------------------------------
addi $sp, $sp, -4
sw $ra, 0($sp) #for return
addi $sp, $sp, -4
sw $a0, 0($sp) #for num
add $a0, $a1, $zero #putting num into sumFactors
jal sumFactors
add $t0, $v0, $zero
beq $t0, $t1, amicable_false
jal sumFactors
add $t1, $v0, $zero
jal sumFactors
add $t2, $v0, $zero
bne $t2, $t0, amicable_false #if sumfactors(sumfactors(sum)) != sum
bne $t1, $a0, amicable_false #sumfactors(sum)!= num1
blt $t1, $a0, amicable_true #sum < num1
j amicable_end #for end
amicable_false:
#-------------------------------------------------------------------------------
#return false
add $v0, $zero, $zero
amicable_true:
#-------------------------------------------------------------------------------
#$t1, $a0 are amicable
add $t1, $a0, $zero
li $v0, 1 #load pair
syscall
li $v0, 4
la $a0, and_msg #print and
syscall
add $a0, $a0, $zero
li $v0, 1 #load pair
syscall
li $v0, 4
la $a0, amicable_msg
syscall
amicable_end:
#-------------------------------------------------------------------------------
lw $ra, 0($sp) #for return
addi $sp, $sp, 4
jr $ra
#variable names for printing
.data
start_msg: .asciiz "Enter the start of the range:\n"
end_msg: .asciiz "Enter the end of the range:\n"
switch_msg: .asciiz "End of range < start of range -- swapping values\n"
err_msg: .asciiz "Unable to check non-positive values\n Exiting... "
swap_msg: .asciiz "End of range < start of range -- swapping values\n"
range: .asciiz "Range of numbers: "
minus: .asciiz " - "
enter: .asciiz "\n"
total: .asciiz "Pairs of amicable numbers: "
exit_msg: .asciiz "Unable to check non-positive values\n Exiting..."
and_msg: .asciiz " and "
amicable_msg: .asciiz " are amicable numbers."```