递归反向阵列MIPS

时间:2019-02-14 22:47:59

标签: arrays mips

因此,我尝试递归地反转数组,但由于某种原因,它仅打印原始数组。我需要它来打印反向数组,并将其反转。我的代码如下。任何帮助将不胜感激。

.data
list:     .word     1 2 3 4 5 6
size:     .space      6
head:     .asciiz  "The Original Array is:\n"
back:     .asciiz  "The Reverse Array is:\n"

.text
.globl  main
main:
    li      $v0,4                   # print syscall
    la      $a0,list
    syscall

    li      $v0,8                   # read array
    la      $a0,list
    li      $a1,1000
    syscall

    la      $a0,list
    jal     arrlen                  

    move    $a1,$v0                 
    la      $a0,list
    jal     reverse

    li      $v0,4                   
    la      $a0,list
    syscall

    li      $v0,10                
    syscall

reverse:
    move    $t0,$ra                 

    addu    $a1,$a0,$a1             
    addiu   $a1,$a1,-1              
    j       reverse_next            

reverse_loop:
    jal     swap
    addiu   $a0,$a0,1               
    addiu   $a1,$a1,-1              

reverse_next:
    ble     $a0,$a1,reverse_loop    
    jr      $t0                     

swap:
    lb      $v0,0($a0)              
    lb      $v1,0($a1)              

    sb      $v1,0($a0)              
    sb      $v0,0($a1)              

    jr      $ra                     

arrlen:
    move    $v0,$a0                 

arraylen_loop:
    lb      $t1,0($a0)              
    addi    $a0,$a0,1               
    bnez    $t1,arraylen_loop         

    subu    $v0,$a0,$v0             
    subi    $v0,$v0,1               
    jr      $ra                     

0 个答案:

没有答案