在阵列MIPS组件中移动元素

时间:2012-09-24 16:00:39

标签: arrays assembly mips

所以我一直在研究一个程序,它将删除数组中的重复值。我有它所以它打印出所有重复的值,我只是无法弄清楚如何实际删除它们(将它们向下移动)。这是我到目前为止所拥有的。 注意:我的问题是“壁球”功能

        .text
test:
    la  $a0,info    # put address of array in $a0
    li  $a1,8       # put length of array in $a1 (words, not bytes)
    jal squash      # call the function

    la      $s0,info    # get address of array
    move    $s1,$v0     # save size of array
loop:
    ble $s1,$0,done
    lw  $a0,0($s0)  # get the next value from the result array
    li  $v0,1       # print it
    #syscall
    la  $a0,eol     # print end of line
    li  $v0,4
    syscall
    addi    $s1,$s1,-1  # decrement count
    addi    $s0,$s0,4   # advance pointer to next value
    b   loop        # print the next value (if one)

done:
    li  $v0,10      # exit
    syscall

# squash out duplicates
#
# $a0 = address of the first element of the array
# $a1 = the number of values (not bytes) in the array
#
# $v0 set to the number of values after duplicates have been removed
squash:
    move    $s0, $a0 #move address to s0
    move    $s1, $a1 #move size to s1
read:
    ble $s1, $0, donea  # (while x > 0) keep looping
    lw  $t0,0($s0)  # get the next 2 value from the result array
    lw  $t1,4($s0)  
    beq $t0, $t1, shift #Branch if two values are equal 
    b   end_if
end_if:
    addi    $s1,$s1,-1  # decrement count (size of arary)
    addi    $s0,$s0,4   # advance pointer to next value
    b   read

shift:
    lw  $a0, 0($s0)
    li  $v0, 1
    syscall
    addi    $s1,$s1,-1  # decrement count (size of arary)
    addi    $s0,$s0,4   # advance pointer to next value
    b   read

donea:              # end of 1st loop

doneb:              #end of 2nd loop    



    jr  $ra

    .data
eol:    .asciiz "\n"
info:
    .word   1
    .word   2
    .word   2
    .word   4
    .word   7
    .word   7
    .word   7
    .word   9

0 个答案:

没有答案