这是我的代码,错误在于读取或写入数组。这已经困扰了我很长一段时间,慢慢变得更好但不完美。
一个示例输出如下:
请输入一个数字来测试它的完美程度,或输入0来退出 8
因素是:
124665658
总和是:
7
此值为
请输入一个数字来测试它的完美程度,或输入0来退出
.data
isPerfect: .asciiz "\nThis Value is Perfect!\n"
isNotPerfect: .asciiz "\nThis value is not perfect :(\n"
negNum: .asciiz "\nPlease only enter positive values \n"
enterNum: .asciiz "\nPlease enter a number to test it's perfectness, or enter 0 to quit \n"
factors: .asciiz "\nFactors are: \n"
sum: .asciiz "\n The sum is: \n"
intVal: .word 0
.text
#DOCUMENTATION OF REGISTERS
#######################################################################################
#s0- user input
#s1- iterator
#s2 - iteration amount
#s3 - s0/2
#s4 - Remainder
#s5 - total
#Find the factors and store them in array add them up and compare them to $s0
#######################################################################################
#main
#prompts the user then goes to loop
#Also declares variables that should be reintialized upon a new user value
.globl main
main: move $s6, $zero
la $t0, factorArray
#variables declared prior to loops
li $s2, 1
li $s3, 1
li $s5, 0
li $v0, 4
la $a0, enterNum
syscall
###########################################
#post main Values
# $s6 - 0
# $t0 - address of factorArray
# s2, s3, s5 look at code
###########################################
loop1:
#retrieve input from user and store it in $s0
li $v0, 5
li $s1, 1
syscall
move $s0, $v0
div $s3, $s0, 2
li $s1, 0
#Branch on greater than zero(Breaks down factors)
bgtz $s0, factor
#Brench on less than zero (Tells user they are bad and jumps back to loop)
bltz $s0, negative
#Branch on zero (End)
b end
###########################################
#Post loop1 Values
#S1 - 1
#s0 - user input number
###########################################
factor:
add $s1, $s1, $s2 #iterate
rem $s4, $s0, $s1 #Get the remainder of the iterator and the user number
beq $s4, $zero, storeValue #if remainder is 0 store the value in the array
ble $s1,$s3, factor #branch if iterator is less than s0/2
b print
###########################################
#post factor values
#$s4 is remainder of User inputer and iterator $s1
#$s3 Division User input, $s0 and 2 because no factors are < 2
###########################################
#shove factors in array
storeValue:
add $s5, $s1, $s5
sw $s1, 100($t0)
addi $t0, $t0, 4
addi $s6, $s6, 1
b factor
###########################################
#post Storage values
#s5 is our total
#store iteration from $s1 in array
#incriment $t0 and %s6 both being address and place holder incrementations
###########################################
#to print the Array
print:
la $t0, factorArray
li $v0, 4
la $a0, factors
syscall
###########################################
#post print values
#$t0 - address of array
#drop our factors prompt
###########################################
for: blt $s0, $s1, printSum
li $v0, 1
lw $a0, 100($t0)
syscall
addi $t0, $t0, 4
addi $s6, $s6, 1
addi $s1, $s1, 1
b for
###########################################
#checks if $s0 and $s1 are equal
#Increments t0 and s6 and s1
###########################################
# Prints sum and decides if the number is perfect and branches accordingly #
printSum:
li $v0, 4
la $a0, sum
syscall
#print total here
li $v0, 1
sw $s5, intVal
lw $a0, intVal
syscall
#perfect branch
beq $s5, $s0, isPerfectImpl
#not Perfect Branch
b isNotPerfectImpl
###########################################
#post printSum values
#Loads contents of $t2 into inval
#needs re worked
###########################################
isPerfectImpl:
li $v0, 4
la $a0, isPerfect
syscall
b main
isNotPerfectImpl:
li $v0, 4
la $a0, isNotPerfect
syscall
b main
#Done
#Tels the user they are bad and jumps back to main to re prompt the user
negative:
li $v0, 4
la $a0, negNum
syscall
b main
end: li $v0, 10
syscall
我的输出如下,我不确定为什么