MIPS尝试使用堆栈复制数组

时间:2014-12-06 09:32:07

标签: arrays assembly stack mips

我一直在尝试使用堆栈在MIPS中复制一个单词数组,由于某种原因它不起作用,我无法弄清楚为什么......请帮忙,谢谢! :)

     .data 
       array:    .word 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 
                 .word 21, 23, 25, 27, 29, 31, 33, 35, 37, 39 
                 .word 41, 43, 45, 47, 49, 51, 53, 55, 57, 59
       length:   .word 30
       array2:   .space 120
       text:        .asciiz "\n\nArray 1 is: \n\n"
       text2:    .asciiz "\n\nArray 2 is: \n\n"

    .text
    .globl main
    main:
     la $t0, array
     li $t1, 0
     lw $t2, length
     la $t3, array2

    loopToPushOntoStack:
     lw $t4, ($t0)      #load first element of the array onto t4
     sub $sp, $sp, 4        #move stack pointer down by 4
     sw $t4, ($sp)      #store t4 onto stack
     add $t0, $t0, 4        #increment array pointer by 4
     add $t1, $t1, 1        #increment counter by 1
     blt $t1, $t2, loopToPushOntoStack

    Reset:
     la $t0, array
     li $t1, 0
     lw $t2, length

    loopToPopStack:
     lw $t4 ($sp)
     move $a0, $t4
     li $v0, 1
     syscall
     addu $sp, $sp, 4
     sw $t4, ($t0)
     sw $t4, ($t3)
     add $t0, $t0, 4
     add $t3, $t3, 4
     add $t1, $t1, 1
     blt $t1, $t2, loopToPopStack

    la $s3, text
    move $a0, $s3
    li $v0, 4
    syscall

    li $t1, 0

    printArrayOne:
      lw $s4, ($t0)
      move $a0, $s4
      li $v0, 1
      syscall
      add $t0, $t0, 4
      add $t1, $t1, 1
      blt $t1, $t2, printArrayOne

    la $s4, text2
    move $a0, $s4
    li $v0, 4
    syscall


    li $t1, 0

    printArrayTwo:
       lw $s5, ($t3)
       move $a0, $s5
       li $v0, 1
       syscall
       add $t3, $t3, 4
       add $t1, $t1, 1
       blt $t1, $t2, printArrayTwo

    exit: 
       li $v0, 10
       syscall

P.S。我认为问题出现在以下函数中:

    loopToPopStack:
       lw $t4 ($sp)
       move $a0, $t4
       li $v0, 1
       syscall
       addu $sp, $sp, 4
       sw $t4, ($t0)
       sw $t4, ($t3)
       add $t0, $t0, 4
       add $t3, $t3, 4
       add $t1, $t1, 1
       blt $t1, $t2, loopToPopStack

但是我无法弄清楚它究竟是什么问题哈哈。

1 个答案:

答案 0 :(得分:1)

没关系,修好了!在打印它之前,我需要将$ t0重置为数组,并将$ t3重置为数组2,或者它们指向超出数组起始地址的120个字节。