用户询问您要采取多少输入,然后以mips显示其总和

时间:2017-10-26 08:24:13

标签: loops mips

我想用mips制作一个程序,用户将在其中输入他想要的输入数量。最后,程序将打印输入的总和。

这是我的代码:

 .data

    myMessage:  .asciiz "ENTER numbers you want to sum\n"
    value:      .asciiz "ENTER  Value \n"

    sum :       .word 0


 .text

    li $v0, 4
    la $a0, myMessage
    syscall

    li $v0, 5
    syscall

    move $t0, $v0         #num of time user will enter num

    la $t1, 0         #count value first initiallize to 0

see:
    bne $t1,$t0,add         #checking if  count is less than the num of value 



    li  $v0, 1              #printing sum finally
    la $a0, ($s2)

add:
    li $v0,4
    la $a0,value
    syscall

    li $v0,5
    syscall

    move $t3,$v0


    la $a1, sum     #load address of 'bal' in '$a1'
    lw $s3, 0($a1)      #load sum from '$a1' to '$s2' (initially 0)
    add $s3, $s3, $t3   #adding the sum 
    sw $s2, 0($a1)      #load latest sum ('$s2') in .word balance ('$a1')

    addi $t1,$t1,1                    inc in count

    j see

问题是程序在输入所需数量后不会停止,并继续要求新输入。

1 个答案:

答案 0 :(得分:-1)

.data 
myMessage:.asciiz "ENTER numbers you want to sum\n" 
value:.asciiz "ENTER  Value: "
show: .asciiz "\nSum is: "
.text 
li $v0,4 
la $a0,myMessage 
syscall 
li $v0,5 
syscall
move $t0,$v0    #num of time user will enter num
la $t1, 0   #count value first initiallize to 0
la $t5, 0
see:
bne $t1,$t0,add #checking if  count is less than the num of value
li $v0, 4
la $a0, show
syscall     #print message "Sum is: "
li $v0, 1
move $a0, $t5
syscall     #print the result
j end
add: 
li $v0,4 
la $a0,value 
syscall
li $v0, 5
syscall
add $t5, $t5, $v0
sub $t0, $t0, 1
j see
end: