QtSpim中的Sort / Swap方法返回未知错误

时间:2018-05-01 15:39:56

标签: sorting assembly mips swap qtspim

这是我第一次使用装配,所以我不确定从故障排除的位置开始。下面的程序用于对数组进行排序。我相信我的交换和排序方法是正确的,但我确信主要方法是错误的。

.data
array1: .word 2, 1, 3, 5, 4

.text
.globl main
main:
    # Initialize Registers
    lw  $a0, x      # Reg $a0 = x
    lw  $a1, y      # Reg $a1 = y

    # Call function
    move    $a0, $a0
    jal     sort
    move    $a1, $v0

    # print mag1
    li  $v0, 4
    la  $a0, mag1
    syscall

    # print result (y)
    li  $v0, 1
    move    $a0, $a1
    syscall

    # print newline
    li  $v0, 4
    la  $a0, lf
    syscall

    # Exit
    li  $v0, 10
    syscall

sort:   
    addi $sp, $sp, -20
    sw $ra, 16($sp)
    sw $s3, 12($sp)
    sw $s2, 8($sp)
    sw $s1, 4($sp)
    sw $s0, 0($sp)

    move $s2, $a0
    move $s3, $a1
    move $s0, $zero

loop1:  
    slt $t0, $s0, $s3
    beq $t0, $zero, exit1
    addi $s1, $s0, -1

loop2:  
    slti $t0, $s1, 0
    bne $t0, $zero, exit2
    sll $t1, $s1, 2
    add $t2, $s2, $t1
    lw $t3, 0($t2)
    lw $t4, 4($t2)
    slt $t0, $t4, $t3
    beq $t0, $zero, exit2
    move $a0, $s2
    move $a1, $s1
    jal swap
    addi $s1, $s1, -1
    j loop2

exit2:  
    addi $s0, $s0, 1
    j loop1

exit1:  
    lw $s0, 0($sp)
    lw $s1, 4($sp)
    lw $s2, 8($sp)
    lw $s3,12($sp)
    lw $ra,16($sp)
    addi $sp, $sp, 20
    jr $ra

swap:   
    sll     $t1, $a1, 2
    add     $t1, $a0, $t1   

    lw      $t0, 0($t1)         
    lw      $t2, 4($t1)         

    sw      $t2, 0($t1)         
    sw      $t0, 4($t1)         

    jr      $ra     

然后代码返回以下错误:

Instructions reference undefined symbol at 0x00400024 [0x00400024] ;8: lw 

$a0, x # Reg $a0 = x 

这似乎是定义变量x的问题,这意味着变量y可能会发生同样的事情。什么似乎是问题,我该如何解决?

编辑:

Updated code
.data
x: .word 4
y: .word 5


.text
.globl main
main:
    # Initialize Registers
    lw  $a0, x      # Reg $a0 = x
    lw  $a1, y      # Reg $a1 = y

    # Call function
    move    $a0, $a0
    jal     sort
    move    $a1, $v0

    # print mag1
    li  $v0, 4
    la  $a0, mag1
    syscall

    # print result (y)
    li  $v0, 1
    move    $a0, $a1
    syscall

    # print newline
    li  $v0, 4
    la  $a0, lf
    syscall

    # Exit
    li  $v0, 10
    syscall

sort:   
    addi $sp, $sp, -20
    sw $ra, 16($sp)
    sw $s3, 12($sp)
    sw $s2, 8($sp)
    sw $s1, 4($sp)
    sw $s0, 0($sp)

    move $s2, $a0
    move $s3, $a1
    move $s0, $zero

loop1:  
    slt $t0, $s0, $s3
    beq $t0, $zero, exit1
    addi $s1, $s0, -1

loop2:  
    slti $t0, $s1, 0
    bne $t0, $zero, exit2
    sll $t1, $s1, 2
    add $t2, $s2, $t1
    lw $t3, 0($t2)
    lw $t4, 4($t2)
    slt $t0, $t4, $t3
    beq $t0, $zero, exit2
    move $a0, $s2
    move $a1, $s1
    jal swap
    addi $s1, $s1, -1
    j loop2

exit2:  
    addi $s0, $s0, 1
    j loop1

exit1:  
    lw $s0, 0($sp)
    lw $s1, 4($sp)
    lw $s2, 8($sp)
    lw $s3,12($sp)
    lw $ra,16($sp)
    addi $sp, $sp, 20
    jr $ra

swap:   
    sll     $t1, $a1, 2
    add     $t1, $a0, $t1   

    lw      $t0, 0($t1)         
    lw      $t2, 4($t1)         

    sw      $t2, 0($t1)         
    sw      $t0, 4($t1)         

    jr      $ra                 

0 个答案:

没有答案