c-尝试使用函数编写一个基本的主程序



我正试图编写一个函数,将单词从源内存复制到目标内存。

我已经编写了函数,但执行代码时遇到了困难。它给了我execption 4作为错误

.data    
.text      
main:
.setnoreorder      
top: beq $a2,0,done 
lw $t1,($a0) 
sw $t1,($a1) 
add $a0,$a0,4 
add $a1,$a1,4 
j top     
sub $a2,$a2,1

done:   
jr  $ra     #return to the main program 
add $0, $0, $0  #nop 

我想写一个主程序,它调用这个函数从内存中的地址0x50000到0x90000。但是,当我添加$a0-$a2中的值并运行代码时,它不起作用。有人知道如何修复它吗?(我正在将C代码转换为MIPS,这就是为什么我包含了一个C标签

干杯

    .text                       # code starts here 
main:                           # required label
    la      $a0,dest            # point to destination
    la      $a1,srce            # point to source
    li      $a2,1000            # move this many words 
    jal     block_copy          # call the routine 
    nop
    li      $v0,10
    syscall
###############################################################################
#   block copy - moves $a3 words from $a1 to $a0
#
#   register usage:
#       $a0 address of destination 
#       $a1 address of source
#       $a2 block size (number of words to move)
#       $v0 return code (0 means no troubles)
#
block_copy:
    move        $v0,$a2         # counter/rc 
bc_loop:
    lw          $t0,0($a1)      # no DMA here
    sw          $t0,0($a0)      # we have to move a word at a time 
    addiu       $a0,$a0,4       # point to next word in destination
    addiu       $a1,$a1,4       # point to next word in source
    addiu       $v0,$v0,-1      # decrement counter 
    bgtz        $v0,bc_loop     # keep on moving if counter is positive 
    jr          $ra             # return to caller
###############################################################################
    .data 
dest:
    .word       9:1000          # destination 1000 words (all zeroes)
srce:
    .word       0:1000          # source 1000 words (all nines)

这不应该是:吗

sub $a2,$a2,1
j top

您在两个位置显示了一个延迟槽,这里是

j top     
sub $a2,$a2,1

这里是

done:   
jr  $ra     #return to the main program 
add $0, $0, $0  #nop

但显然不在这里:

top: beq $a2,0,done 
lw $t1,($a0) 

也许问题是,beq之后的加载实际上是一个延迟槽,即使$a2为零(并且分支被执行)也会被执行——即使计数为零,你也会在($a0)从内存加载——可能是访问了无效内存并导致了异常。