我正在开发一个程序,它接受20个整数的数组,打印数组中最小和最大的数组,并计算可被4整除的整数数量。但是,使用PCSpim,我遇到的问题是我的程序不会终止。
.data
array: .space 80
prompt: .asciiz "Please enter an integer: "
newLine: .asciiz "\n"
printS: .asciiz "\nThe smallest value is: "
printL: .asciiz "\nThe largest value is: "
printD: .asciiz "\nThe follow values are divisible by 4: "
.text
main: li $t0, 20 # counter for array
li $t1, 0 # small
li $t2, 0 # large
la $a1, array # array for 20 integers
loopR: la $a0, prompt # print prompt
li $v0, 4
syscall
li $v0, 5 # read input
syscall
sw $v0, 0($a1) # store input
add $a1, $a1, 4 # move index
add $t0, $t0, -1 # reduce number of counter
bgtz $t0, loopR # continue while counter != 0
li $t0, 19 # reinitialize
la $a1, array
la $a0, printS # small value text
li $v0, 4
syscall
jal smallestLargest # call smallestLargest function
move $a0, $v0 # print smallest value
li $v0, 1
syscall
la $a0, printL # large value text
li $v0, 4
syscall
move $a0, $v1 # print largest value
li $v0, 1
syscall
li $t0, 20 # reinitialize
li $v1, 0
la $a1, array
la $a0, printD # divisible by 4 text
li $v0, 4
syscall
jal divisible
move $a0, $v1 # print divisble by 4
li $v0, 1
syscall
li $v0, 10 # exit program
syscall
smallestLargest:
lw $v0, 0($a1) # smallest @ a[0]
lw $v1, 0($a1) # largest @ a[0]
loop: lw $t3, 4($a1) # load array at index 1
add $a1, $a1, 4
add $t0, $t0, -1
bge $v0, $t3, small # if smallest > a[i], branch
blt $v1, $t3, large # if largest < a[i], branch
check: bgtz $t0, loop # if counter is greater than 0
b end
small: move $v0, $t3 # store smallest to $v0
b check
large: move $v1, $t3 # store largest to $v1
b check
end: jr $ra # return
divisible: lw $t2, 0($a1)
add $a1, $a1, 4
add $t0, $t0, -1
rem $t1, $t2, 4 # if x % 4 = 0
beqz $t1, divCounter
check2: bgtz $t0, divisible # check if counter greater than 0
jr $ra # return
divCounter: add $v1, $v1, 1
b check2