MIPS用户输入Int数组仅查找第一个数组元素

时间:2018-03-13 04:02:23

标签: assembly mips

所以我有一个用于类的MIPS程序集的程序,我的意思是让用户创建一个给定大小的数组,然后输出最大的元素。但是,在尝试输出时,我无法访问除第一个数组元素之外的任何内容。我已经调试了几个小时了,我似乎无法找到问题。我的代码如下:

.

data
array: .space 40
size: .asciiz "Enter the size of the array (0 to 10): "
get: .asciiz "\nEnter an integer: "
error: .asciiz "\n!!!Invalid! Please input a size between 0 and 10: "
answer: .asciiz "\nThe largest element is: "

.text
main:
    #get the size
    li $t2, 10
    li $v0, 4
        la $a0, size
        syscall
        #get a size int, put in $t0
    li $v0, 5
        syscall
        move $t0, $v0
        slt $t3, $t2, $t0
    #$t3 equals zero if they are not equal
    slt $t3, $t2, $t0
    getsize: beq $t3, $zero, store
        li $v0, 4
            la $a0, error
            syscall
        li $v0, 5
            syscall
            move $t0, $v0
            slt $t3, $t2, $t0
        j getsize

        store:  
            #initialize a loop counter
            li $t1, 0
            #initialize a register with the address of array
            la $s1, array
            #loop method, with beq
            initialize: beq $t1, $t0, read
                #print the get integer message
                li $v0, 4
                la $a0, get
                syscall
                #get the input
                li $v0, 5
                syscall
                move $t4, $v0

                #move the input to register $t4
                #sw $t4, 0($s1)
                sw $t4, 0($s1)
                addi $s1, $s1, 4
                #store word in the array
                #increment the loop's counter
                addi $t1, $t1, 1
                j initialize

        read: #initialize array address
            la  $s0, array
            #array counter
            li $t1, 0
            #largest found
            lw $t2, 0($s0)
            addi $s0, $s0, 4
            find: beq $t1, $t0, printResult
                #load the value of the array element
                lw $t4, ($s0)
                #increment the array
                addi $s0, $s0, 4
                #set less than to judge the current greatest against the new element
                #1 if the new element is less than the old element
                #0 if the new element is >= the old element 
                slt $t3, $t4, $t2
                #if the new one is less than, we jump back to the top
                bne $t3, $zero, find
                #otherwise, we move make the new one the greatest and increment $t1
                move $t4, $t2
                addi $t1, $t1, 1
                #jump to the top of the loop
                j find

        printResult:
            li $v0, 4
            la $a0, answer
            syscall

            li $v0, 1
            move $a0, $t2
        syscall

    #end program
    end:li $v0, 10
        syscall

1 个答案:

答案 0 :(得分:0)

您可以访问数组的所有元素,实际上是在数组末尾访问更多值,因此访问不是您真正的问题。

您的问题只是结果打印的第一个值而不是最大值。

之所以发生这种情况,是因为当您找到一个值时,您不会将较大的值存储在任何位置,因此您的t2寄存器将保留第一个元素值,直到循环最终意外结束(由于错误{{ 1}}循环计数器逻辑),然后打印出第一个元素作为结果。

最大值更新只需要切换t1的参数,但你应该重新设计循环计数器逻辑,并确保你没有达到数组后的元素(因为你跳过第一个数组元素,所以循环应该只在size-1元素上运行,或者你可以初始化t1 = 1来获得大小为1的迭代,一旦你修复了它的递增逻辑)。

相关问题