数组中两个整数之间的MIPS差异

时间:2016-08-13 11:30:31

标签: mips

我需要在MIPS中构建一个程序(硬编码),它获取一个包含10个整数的数组,并找出数组中两个附近数字之间的差异。这就是我的构建:

    .data
array:      .word       23,-2,45,67,89,12,-100,0,120,6
arrend: 

comma: .asciiz ", "

    # array = {23,-2,45,67,89,12,-100,0,120,6}
    # Algorithm being implemented to find the difference between nearby elements of the array
    # difference = 0 (use $t0 for difference)
    # loop i = 0 to length-1 do (use $t1 for i)
    # difference = array[i]-array[i+1]
    # end loop (use $t3 for base addr. of array)

    # registers:
    #   t0 -- difference
    #
    #   t3 -- pointer to current array element (e.g. arrptr)
    #   t2 -- pointer to end of array
    # 
    #   t4 -- current value fetched from array (i)
    #   t5 -- value fetched from array (i+1)

    .text

main:
    li      $t0,0                   # difference = 0

    la      $t3,array               # load base addr. of array
    la      $t2,arrend              # load address of array end
    j       test 

loop:
    lw      $t4,0($t3)              # load array[i]
    addi    $t3,$t3,4               # increment array pointer
    lw      $t5,0($t3)              # load array[i+1]   
    sub     $t0, $t4, $t5           # the difference of two nearby elements

    # print value of difference
    li      $v0,1
    addi    $a0,$t0,0
    syscall

    # print comma
    li      $v0,4
    la      $a0,comma
    syscall

test:
    blt     $t3,$t2,loop            # more to do? if yes, loop

输出应该是: 25, -47, -22, -22, 77, 112, -100, -120, 114,

但我得到了输出25, -47, -22, -22, 77, 112, -100, -120, 114, -8230,

我发现如果我将la $t2,arrend更改为la $t2,0x10010024,它会起作用,但我不知道如何在代码中编写它。

此外,我如何改进我的代码?

1 个答案:

答案 0 :(得分:0)

好的......所以,你需要再次迭代,然后你需要......在你指定数组的结束指针之后,从t2开始只需要1。

它也被称为bufferoverflow