2016-08-13 23 views
-1

我需要在MIPS中构建一个程序(硬代码),该程序获取10个整数的数组,并查找数组中两个相邻数字之间的差异。这是我建:阵列中两个整数之间的MIPS差异

.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,arrendla $t2,0x10010024,它会工作,但我不知道如何在代码中编写它。

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

+0

#ממןבאוניברסיטההפתוחה –

回答

0

好的...所以,你再次迭代一次,然后你需要......在你分配数组的结束指针之后,从t2开始只需要从sub1开始。它也被称为bufferoverflow