0

私は過去 5 時間、この問題を解決しようとしてきました。最初のインデックスと最後のインデックスを比較する必要があります。最初の < 最後の場合、それらは交換されます。配列の要素が 5 つ以下の場合、これは正常に機能します。それより大きいと、2 回の反復後にスワッピングが停止します。私は何を間違っていますか?

配列を逆にする私のサブルーチン:

ReverseArray:
    add $s0, $a0, $0

    li  $t2, 1
    la  $s0, array
    li  $s1, 4
    sub $s2, $t0, 1         #lines 177 - 180 finds the last index of the array
    mult    $s2, $s1
    mflo    $s2
    add $t3, $s2, $s0

    swap:
        lw  $t5, 0($s0)
        lw  $t4, 0($t3)     #stores the element of last index into $t4

        sle $t1, $t5, $t4
        beq $t1, $0, finish

        li  $v0, 1          #only in the loop for debug purposes
        add $a0, $t1, $0
        syscall

        add $t6, $t5, $0
        sw  $t4, 0($s0)
        sw  $t6, 0($t3)

        sub $s2, $s2, 4
        add $t3, $s2, $s0
        add $s0, $s0, 4
        j   swap
    finish:

        add $v1, $s0, $0
        jr  $ra

これらの数値を配列に入力するとします。

3 6 9 12 15 18

私が得るものは次のとおりです。

18 15 9 12 6 3
4

1 に答える 1

1

ループの終了条件は、最初の要素が 2 番目の要素より大きいことのようです。これは、配列を逆にしようとしている場合には正しくありません (アドレスまたは inidices を比較する必要があります)。更新の方法$t3も奇妙に見えます。

作業ループは次のとおりです。

swap:
    lw  $t5, 0($s0)
    lw  $t4, 0($t3)     #stores the element of last index into $t4

    sw  $t4, 0($s0)
    sw  $t5, 0($t3)

    sub $t3, $t3, 4
    add $s0, $s0, 4
    sle $t1,$t3,$s0
    beq $t1,$0,swap    # repeat until $t3 <= $s0
于 2013-10-17T07:56:13.920 に答える