MIPS气泡排序

时间:2018-10-28 11:32:44

标签: assembly mips bubble-sort spim qtspim

我在使用MIPS完成气泡排序代码时遇到了一些麻烦。我不确定如何解决我得到的异常错误。这些是我在QtSpim中遇到的例外。

在PC = 0x00400064处发生异常

读取的数据/堆栈中的错误地址:0x10040000

在PC = 0x0040003c处发生了异常

读取的数据/堆栈中的错误地址:0x10040000

在PC = 0x00400060处发生异常

数据/堆栈中的错误地址读取:0x10040004

在PC = 0x00400064处发生异常

读取的数据/堆栈中的错误地址:0x10040000

.data
.align 4
Input_data: .word 2, 0, -7, -1, 3, 8, -4, 10
            .word -9, -16, 15, 13, 1, 4, -3, 14
            .word -8, -10, -15, 6, -13, -5, 9, 12
            .word -11, -14, -6, 11, 5, 7, -2, -12


Output_data: .word 0, 0, 0, 0, 0, 0, 0, 0
             .word 0, 0, 0, 0, 0, 0, 0, 0
             .word 0, 0, 0, 0, 0, 0, 0, 0
             .word 0, 0, 0, 0, 0, 0, 0, 0

.text
.globl main
.align 4

main:
    la   $t0, Input_data        # Load address of Input_data into t0
    la   $t1, Output_data       # Load address of Output_data into t1
    addu $t2, $t1, 128          # Get address of last word in output data (32 * 4 = 128)
    li   $s0, 1                 # Store index for Input_data
    li   $s1, 32                # Store index for Output_data

sort_main:
    lw   $s2, 0($t0)            # Load word from Input_data
    lw   $s3, 4($t1)
    ble  $s2, $s3, sort_swap    # If s2 is less than s3, branch to sort_swap
    li   $s4, 31                # Set s4 counter to 31
    beq  $s0, $s4, sort_store   # For loop, if the counter is 31, branch to sort_store
    addu $t0, $t0, 4            # Increment t0 by 4 bytes to get the next item
    addu $s0, $s0, 1            # Increment the index
    j    sort_main              # Jump to sort_main

sort_swap:
    sw   $s2, 4($t0)
    sw   $s3, 0($t0)
    addu $t0, $t0, 4
    addu $s0, $s0, 1
    j    sort_main

sort_store:
    lw   $s2, 4($t0)
    sw   $s2, 0($t2)
    subu $t2, $t2, 4
    subu $s1, $s1, 1
    li   $s5, 1
    beq  $s1, $s5, end
    la   $t0, Input_data
    li   $s0, 1
    j    sort_main

end:
    j    end

QtSpim:

User Text Segment [00400000]..[00440000]
[00400000] 8fa40000  lw $4, 0($29)            ; 183: lw $a0 0($sp) # argc 
[00400004] 27a50004  addiu $5, $29, 4         ; 184: addiu $a1 $sp 4 # argv 
[00400008] 24a60004  addiu $6, $5, 4          ; 185: addiu $a2 $a1 4 # envp 
[0040000c] 00041080  sll $2, $4, 2            ; 186: sll $v0 $a0 2 
[00400010] 00c23021  addu $6, $6, $2          ; 187: addu $a2 $a2 $v0 
[00400014] 0c100009  jal 0x00400024 [main]    ; 188: jal main 
[00400018] 00000000  nop                      ; 189: nop 
[0040001c] 3402000a  ori $2, $0, 10           ; 191: li $v0 10 
[00400020] 0000000c  syscall                  ; 192: syscall # syscall 10 (exit) 
[00400024] 3c081001  lui $8, 4097 [Input_data]; 19: la $t0, Input_data # Load address of Input_data into t0 
[00400028] 3c011001  lui $1, 4097 [Output_data]; 20: la $t1, Output_data # Load address of Output_data into t1 
[0040002c] 34290080  ori $9, $1, 128 [Output_data] 
[00400030] 252a0080  addiu $10, $9, 128       ; 21: addu $t2, $t1, 128 # Get address of last word in output data (32 * 4 = 128) 
[00400034] 34100001  ori $16, $0, 1           ; 22: li $s0, 1 # Store index for Input_data 
[00400038] 34110020  ori $17, $0, 32          ; 23: li $s1, 32 # Store index for Output_data 
[0040003c] 8d120000  lw $18, 0($8)            ; 26: lw $s2, 0($t0) # Load word from Input_data 
[00400040] 8d330004  lw $19, 4($9)            ; 27: lw $s3, 4($t1) 
[00400044] 0272082a  slt $1, $19, $18         ; 28: ble $s2, $s3, sort_swap # If s2 is less than s3, branch to sort_swap 
[00400048] 10200006  beq $1, $0, 24 [sort_swap-0x00400048] 
[0040004c] 3414001f  ori $20, $0, 31          ; 29: li $s4, 31 # Set s4 counter to 31 
[00400050] 12140009  beq $16, $20, 36 [sort_store-0x00400050]
[00400054] 25080004  addiu $8, $8, 4          ; 31: addu $t0, $t0, 4 # Increment t0 by 4 bytes to get the next item 
[00400058] 26100001  addiu $16, $16, 1        ; 32: addu $s0, $s0, 1 # Increment the index 
[0040005c] 0810000f  j 0x0040003c [sort_main] ; 33: j sort_main # Jump to sort_main 
[00400060] ad120004  sw $18, 4($8)            ; 36: sw $s2, 4($t0) 
[00400064] ad130000  sw $19, 0($8)            ; 37: sw $s3, 0($t0) 
[00400068] 25080004  addiu $8, $8, 4          ; 38: addu $t0, $t0, 4 
[0040006c] 26100001  addiu $16, $16, 1        ; 39: addu $s0, $s0, 1 
[00400070] 0810000f  j 0x0040003c [sort_main] ; 40: j sort_main 
[00400074] 8d120004  lw $18, 4($8)            ; 43: lw $s2, 4($t0) 
[00400078] ad520000  sw $18, 0($10)           ; 44: sw $s2, 0($t2) 
[0040007c] 254afffc  addiu $10, $10, -4       ; 45: subu $t2, $t2, 4 
[00400080] 2631ffff  addiu $17, $17, -1       ; 46: subu $s1, $s1, 1 
[00400084] 34150001  ori $21, $0, 1           ; 47: li $s5, 1 
[00400088] 12350004  beq $17, $21, 16 [end-0x00400088]
[0040008c] 3c081001  lui $8, 4097 [Input_data]; 49: la $t0, Input_data 
[00400090] 34100001  ori $16, $0, 1           ; 50: li $s0, 1 
[00400094] 0810000f  j 0x0040003c [sort_main] ; 51: j sort_main 
[00400098] 08100026  j 0x00400098 [end]       ; 54: j end 

Kernel Text Segment [80000000]..[80010000]
[80000180] 0001d821  addu $27, $0, $1         ; 90: move $k1 $at # Save $at 
[80000184] 3c019000  lui $1, -28672           ; 92: sw $v0 s1 # Not re-entrant and we can't trust $sp 
[80000188] ac220200  sw $2, 512($1)           
[8000018c] 3c019000  lui $1, -28672           ; 93: sw $a0 s2 # But we need to use these registers 
[80000190] ac240204  sw $4, 516($1)           
[80000194] 401a6800  mfc0 $26, $13            ; 95: mfc0 $k0 $13 # Cause register 
[80000198] 001a2082  srl $4, $26, 2           ; 96: srl $a0 $k0 2 # Extract ExcCode Field 
[8000019c] 3084001f  andi $4, $4, 31          ; 97: andi $a0 $a0 0x1f 
[800001a0] 34020004  ori $2, $0, 4            ; 101: li $v0 4 # syscall 4 (print_str) 
[800001a4] 3c049000  lui $4, -28672 [__m1_]   ; 102: la $a0 __m1_ 
[800001a8] 0000000c  syscall                  ; 103: syscall 
[800001ac] 34020001  ori $2, $0, 1            ; 105: li $v0 1 # syscall 1 (print_int) 
[800001b0] 001a2082  srl $4, $26, 2           ; 106: srl $a0 $k0 2 # Extract ExcCode Field 
[800001b4] 3084001f  andi $4, $4, 31          ; 107: andi $a0 $a0 0x1f 
[800001b8] 0000000c  syscall                  ; 108: syscall 
[800001bc] 34020004  ori $2, $0, 4            ; 110: li $v0 4 # syscall 4 (print_str) 
[800001c0] 3344003c  andi $4, $26, 60         ; 111: andi $a0 $k0 0x3c 
[800001c4] 3c019000  lui $1, -28672           ; 112: lw $a0 __excp($a0) 
[800001c8] 00240821  addu $1, $1, $4          
[800001cc] 8c240180  lw $4, 384($1)           
[800001d0] 00000000  nop                      ; 113: nop 
[800001d4] 0000000c  syscall                  ; 114: syscall 
[800001d8] 34010018  ori $1, $0, 24           ; 116: bne $k0 0x18 ok_pc # Bad PC exception requires special checks 
[800001dc] 143a0008  bne $1, $26, 32 [ok_pc-0x800001dc] 
[800001e0] 00000000  nop                      ; 117: nop 
[800001e4] 40047000  mfc0 $4, $14             ; 119: mfc0 $a0 $14 # EPC 
[800001e8] 30840003  andi $4, $4, 3           ; 120: andi $a0 $a0 0x3 # Is EPC word-aligned? 
[800001ec] 10040004  beq $0, $4, 16 [ok_pc-0x800001ec]
[800001f0] 00000000  nop                      ; 122: nop 
[800001f4] 3402000a  ori $2, $0, 10           ; 124: li $v0 10 # Exit on really bad PC 
[800001f8] 0000000c  syscall                  ; 125: syscall 
[800001fc] 34020004  ori $2, $0, 4            ; 128: li $v0 4 # syscall 4 (print_str) 
[80000200] 3c019000  lui $1, -28672 [__m2_]   ; 129: la $a0 __m2_ 
[80000204] 3424000d  ori $4, $1, 13 [__m2_]   
[80000208] 0000000c  syscall                  ; 130: syscall 
[8000020c] 001a2082  srl $4, $26, 2           ; 132: srl $a0 $k0 2 # Extract ExcCode Field 
[80000210] 3084001f  andi $4, $4, 31          ; 133: andi $a0 $a0 0x1f 
[80000214] 14040002  bne $0, $4, 8 [ret-0x80000214]; 134: bne $a0 0 ret # 0 means exception was an interrupt 
[80000218] 00000000  nop                      ; 135: nop 
[8000021c] 401a7000  mfc0 $26, $14            ; 145: mfc0 $k0 $14 # Bump EPC register 
[80000220] 275a0004  addiu $26, $26, 4        ; 146: addiu $k0 $k0 4 # Skip faulting instruction 
[80000224] 409a7000  mtc0 $26, $14            ; 148: mtc0 $k0 $14 
[80000228] 3c019000  lui $1, -28672           ; 153: lw $v0 s1 # Restore other registers 
[8000022c] 8c220200  lw $2, 512($1)           
[80000230] 3c019000  lui $1, -28672           ; 154: lw $a0 s2 
[80000234] 8c240204  lw $4, 516($1)           
[80000238] 001b0821  addu $1, $0, $27         ; 157: move $at $k1 # Restore $at 
[8000023c] 40806800  mtc0 $0, $13             ; 160: mtc0 $0 $13 # Clear Cause register 
[80000240] 401a6000  mfc0 $26, $12            ; 162: mfc0 $k0 $12 # Set Status register 
[80000244] 375a0001  ori $26, $26, 1          ; 163: ori $k0 0x1 # Interrupts enabled 
[80000248] 409a6000  mtc0 $26, $12            ; 164: mtc0 $k0 $12 
[8000024c] 42000018  eret                     ; 167: eret 

非常感谢您的帮助。

0 个答案:

没有答案