Home:ALL Converter>bubble sort using MIPS

bubble sort using MIPS

Ask Time:2017-10-17T17:23:45         Author:A.Lee

Json Formatter

This is a code for bubble sort in descending order using mips instructions. I keep getting the same error but I'm not sure what I am doing wrong.

.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  #address of input/output data
    la   $t1, Output_data
    addu $t2, $t1, 128    #address of output data last word
    li   $s0, 1           #index of input data
    li   $s1, 32          #index of output data

L1: #compare
    lw   $s2, 0($t0)      #load 2 words from input data
    lw   $s3, 4($t1)
    ble  $s2, $s3, L2     #compare s2, s3. If s2 is smaller, jump to L2
    li   $s4, 31
    beq  $s0, $s4, L3     #if the counter is 31, jump to L3
    addu $t0, $t0, 4      #input data pointer increment
    addu $s0, $s0, 1      #input index increment
    j    L1

L2: #swap orders of input data
    sw   $s2, 4($t0)
    sw   $s3, 0($t0)
    addu $t0, $t0, 4
    addu $s0, $s0, 1
    j    L1

L3: #store smallest value in output data
    lw   $s2, 4($t0)          
    sw   $s2, 0($t2)       #store smallest values in backwards order
    subu $t2, $t2, 4
    subu $s1, $s1, 1
    li   $s5, 1
    beq  $s1, $s5, end
    la   $t0, Input_data
    li   $s0, 1
    j    L1

end:
    j    end

Exception occurred at PC=0x00400064

Bad address in data/stack read: 0x10040000

Exception occurred at PC=0x0040003c

Bad address in data/stack read: 0x10040004

Exception occurred at PC=0x00400060

Bad address in data/stack read: 0x10040008

This is the error I keep get.

Author:A.Lee,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/46786798/bubble-sort-using-mips
yy