Sorting 气泡排序MIPS中的打印函数出错?

Sorting 气泡排序MIPS中的打印函数出错?,sorting,assembly,mips,Sorting,Assembly,Mips,很抱歉,这是一个很长的代码块,但是您在其中看到任何明显的语法错误吗?编译器告诉我有一个运行时执行错误:0x00400008处的运行时异常:地址超出范围0x00000000。如果可以的话,请帮忙 .data save: .word 1,2,4,2,5,6 size: .word 6 .text la $s6, save #load address of array la $t5, size #load address of size

很抱歉,这是一个很长的代码块,但是您在其中看到任何明显的语法错误吗?编译器告诉我有一个运行时执行错误:0x00400008处的运行时异常:地址超出范围0x00000000。如果可以的话,请帮忙

        .data
save:   .word 1,2,4,2,5,6
size:   .word 6
        .text

    la $s6, save #load address of array
    la $t5, size #load address of size 
    lw $t5, 0($t5) #load array size
    sw $t0, 0($s6) #save the first element of the array
    sw $t1, 4($s6) #save the second element of the array
    j sort #call sort function

swap:   sll $t1, $a1, 2 #shift bits by 2 
        add $t1, $a1, $t1 #set $t1 address to v[k]
    lw $t0, 0($t1) #load v[k] into t1 <- ERROR
    lw $t2, 4($t1) #load v[k+1] into t1 
    sw $t2, 0($t1) #swap addresses
    sw $t0, 4($t1) #swap addresses
    jr $ra #return 

sort:   addi $sp, $sp, -20 #make enough room on the stack for five registers
    sw $ra, 16($sp) #save the return address on the stack
    sw $s3, 12($sp) #save $s3 on the stack
    sw $s2, 8($sp) #save Ss2 on the stack
    sw $s1, 4($sp) #save $s1 on the stack
    sw $s0, 0($sp) #save $s0 on the stack

    move $s2, $a0 #copy the parameter $a0 into $s2 (save $a0) 
    move $s3, $a1 #copy the parameter $a1 into $s3 (save $a1)
    move $s0, $zero #start of for loop, i = 0
for1tst: slt $t0, $s0, $s3 #$t0 = 0 if $s0 S $s3 (i S n)
    beq $t0, $zero, exit1 #go to exit1 if $s0 S $s3 (i S n)
    addi $s1, $s0, -1 #j - i - 1
for2tst: slti $t0, $s1, 0 #$t0 = 1 if $s1 < 0 (j < 0) 
    bne $t0, $zero, exit2 #$t0 = 1 if $s1 < 0 (j < 0)
    sll $t1, $s1, 2 #$t1 = j * 4 (shift by 2 bits)
    add $t2, $s2, $t1 #$t2 = v + (j*4) 
    lw $t3, 0($t2) #$t3 = v[j]
    lw $t4, 4($t2) #$t4 = v[j+1]
    slt $t0, $t4, $t3 #$t0 = 0 if $t4 S $t3
    beq $t0, $zero, exit2 #go to exit2 if $t4 S $t3
    move $a0, $s2 #1st parameter of swap is v(old $a0)
    move $a1, $s1 #2nd parameter of swap is j
    jal swap #swap
    addi $s1, $s1, -1 
    j for2tst #jump to test of inner loop
    j print
exit2: 
    addi $s0, $s0, 1 #i = i + 1
    j for1tst #jump to test of outer loop

exit1: 
    lw $s0, 0($sp) #restore $s0 from stack
    lw $s1, 4($sp) #resture $s1 from stack
    lw $s2, 8($sp) #restore $s2 from stack
    lw $s3, 12($sp) #restore $s3 from stack
    lw $ra, 16($sp) #restore $ra from stack
    addi $sp, $sp, 20 #restore stack pointer 
    jr $ra #return to calling routine

    .data
space:.asciiz  " "          # space to insert between numbers
head: .asciiz  "The sorted numbers are:\n"
  .text
print:add  $t0, $zero, $a0  # starting address of array
      add  $t1, $zero, $a1  # initialize loop counter to array size
      la   $a0, head        # load address of print heading
      li   $v0, 4           # specify Print String service
      syscall               # print heading
out:  li   $v0, 1           # specify Print Integer service
      la   $a0, space       # load address of spacer for syscall
      li   $v0, 4           # specify Print String service
      syscall               # output string
      addi $t0, $t0, 4      # increment address
      addi $t1, $t1, -1     # decrement loop counter
      bgtz $t1, out         # repeat if not finished
      jr   $ra              # return
.data
保存:。word 1,2,4,2,5,6
大小:.word 6
.文本
la$s6,保存#数组的加载地址
la$t5,大小#大小的加载地址
lw$t5,0($t5)#加载阵列大小
sw$t0,0($s6)#保存数组的第一个元素
sw$t1,4($s6)#保存数组的第二个元素
j排序#调用排序函数
交换:sll$t1,$a1,2位移位2
添加$t1、$a1、$t1#将$t1地址设置为v[k]

lw$t0,0($t1)#将v[k]加载到t1查看您的
exit1
sub。您在末尾使用了
jr$ra
,这意味着它是一个子程序。您在哪里设置回信地址寄存器?你只需要分支到这些潜艇;您应该使用
jal
呼叫他们

或者,在
exit1
的末尾,使用
j
,就像使用
exit2
一样


编辑:您还需要使用
jal print
,而不是
j print
。始终对返回的任何sub使用
jr$ra
,如果sub没有使用
jal
(或任何链接指令)调用,则永远不要使用
jr$ra

根据定义,如果可以得到运行时错误,则没有语法错误。您认为我的输入有问题吗,要排序的数字?为什么不公布您得到的准确错误?@m00nbeam360:您的程序不完整。它缺少“主”例程,该例程应该设置寄存器(指向数组的指针等)并执行调用。这是怎么回事?我更新了代码以放入寄存器。它还缺什么吗?谢谢你的帮助,非常感谢。