我会尽可能简单地解释我的问题:
- 用户必须输入一个数字(K)
- 输入时必须输入!= 0整数(所以序列)
例如:
1,2, 3,4,5 ,6,0
如果k = 3
程序将对所有组元素进行求和(参见序列中较强的数字)
输出将是:6,9,12,15
我试图编码那个结果:
.globl main
.data
.eqv $K, $a1
.eqv $S, $t1
.text
main:
li $v0, 5
syscall
move $K, $v0
move $t0, $zero
#carico vettore
for:
beq $t0, $K, stampa_somma
li $v0, 5
syscall #eseguo la somma una volta messo in input l elemento
beqz $v0, end
subi $sp, $sp, 4
sw $v0, ($sp)
beqz $t0, memorizza
add $S, $S, $v0
addi $t0,$t0,1
j for
memorizza:
move $t2, $sp #memorizzo il valore iniziale
add $S, $S, $v0
addi $t0,$t0,1
j for
stampa_somma:
move $a0, $S #stampo la somma
li $v0,1
syscall
li $a0, '\n'
li $v0, 11
syscall
seq $t0, $t0, 0
move $sp, $t2
j shift
shift:
beq $t0,$K, fine_shift
lw $t3, ($sp)
addi $sp,$sp, 4
sw $t3, ($sp)
subi $sp, $sp, 8
addi $t0, $t0, 1
j shift
fine_shift:
addi $sp, $sp, 4
li $v0, 5
syscall
beqz $v0, end
sw $v0, ($sp)
move $sp, $t2
seq $t0, $t0,0
seq $S, $S, 0
j somma
somma:
beq $t0, $K, stampa_somma
lw $a3, ($sp)
add $S, $S, $a3
addi $t0, $t0, 1
subi $sp,$sp, 4
j somma
end:
li $v0, 10
syscall
简单的数字编程工作,但高K和高数字最后输出整数之间存在差异,我的代码出了什么问题?谢谢!