Hi I have a little exercise that I was assigned. This is what needs to be done:

- Read 10 numbers from keyboard.
- Sort them by Insertion Sort algorithm
- Print the sorted numbers in ascending order

This is for MIPS and the program I use to run it is PCSPIM. I have little knowledge of this language but I have attempted to write a code that sorts three numbers. I am hoping if I can get that to work then adjusting it to 10 numbers should be okay.

main:
    .data    0x10010000        
    .asciiz "\nType 1st double digit number and press enter:\n"
    .data    0x10010100        
    .asciiz "\nType 2nd double digit number and press enter:\n"
    .data    0x10010200        
    .asciiz "\nType 3rd double digit number and press enter:\n"
    .data    0x10010300        
    .asciiz "\nAfter Sorting you get: "
    
    

    .text

    ori $v0, $zero, 4        
    lui $a0, 0x1001    
    ori $a0, $a0, 0                #prompt for 1st number
    syscall

    ori $v0, $zero, 5            #read first number
    syscall
    or $s0, $zero, $v0

    ori $v0, $zero, 4        
    lui $a0, 0x1001    
    ori $a0, $a0, 0x0100            #prompt for 2nd number
    syscall

    ori $v0, $zero, 5            #read 2nd number
    syscall
    or $s1, $zero, $v0

    ori $v0, $zero, 4        
    lui $a0, 0x1001    
    ori $a0, $a0, 0x0200            #prompt for 3rd number
    syscall

    ori $v0, $zero, 5            #read 3rd number
    syscall
    or $s3, $zero, $v0

    j    sort

fin:    ori $v0, $zero, 4        
    lui $a0, 0x1001    
    ori $a0, $a0, 0x0300            #End msg part2
    syscall

    ori $v0, $zero, 1            #print result
    or  $a0, $zero, $s0
    syscall

    ori $v0, $zero, 1            #print result
    or  $a0, $zero, $s1
    syscall

    ori $v0, $zero, 1            #print result
    or  $a0, $zero, $s2
    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

for1:    slt    $t0, $s0, $s3
    beq    $t0, $zero, exit1

    addi    $s1, $s0, -1

for2:    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    for2

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

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
    

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

i know there could be an issue with my loop but I havent been able to fix it. If my method is too long or doesnt make sense please suggest another if possible. I am running out of ideas.

Hi I have a little exercise that I was assigned. This is what needs to be done:

- Read 10 numbers from keyboard.
- Sort them by Insertion Sort algorithm
- Print the sorted numbers in ascending order

This is for MIPS and the program I use to run it is PCSPIM. I have little knowledge of this language but I have attempted to write a code that sorts three numbers. I am hoping if I can get that to work then adjusting it to 10 numbers should be okay.

main:
    .data    0x10010000        
    .asciiz "\nType 1st double digit number and press enter:\n"
    .data    0x10010100        
    .asciiz "\nType 2nd double digit number and press enter:\n"
    .data    0x10010200        
    .asciiz "\nType 3rd double digit number and press enter:\n"
    .data    0x10010300        
    .asciiz "\nAfter Sorting you get: "
 
 
 
    .text
 
    ori $v0, $zero, 4        
    lui $a0, 0x1001    
    ori $a0, $a0, 0                #prompt for 1st number
    syscall
 
    ori $v0, $zero, 5            #read first number
    syscall
    or $s0, $zero, $v0
 
    ori $v0, $zero, 4        
    lui $a0, 0x1001    
    ori $a0, $a0, 0x0100            #prompt for 2nd number
    syscall
 
    ori $v0, $zero, 5            #read 2nd number
    syscall
    or $s1, $zero, $v0
 
    ori $v0, $zero, 4        
    lui $a0, 0x1001    
    ori $a0, $a0, 0x0200            #prompt for 3rd number
    syscall
 
    ori $v0, $zero, 5            #read 3rd number
    syscall
    or $s3, $zero, $v0
 
    j    sort
 
fin:    ori $v0, $zero, 4        
    lui $a0, 0x1001    
    ori $a0, $a0, 0x0300            #End msg part2
    syscall
 
    ori $v0, $zero, 1            #print result
    or  $a0, $zero, $s0
    syscall
 
    ori $v0, $zero, 1            #print result
    or  $a0, $zero, $s1
    syscall
 
    ori $v0, $zero, 1            #print result
    or  $a0, $zero, $s2
    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
 
for1:    slt    $t0, $s0, $s3
    beq    $t0, $zero, exit1
 
    addi    $s1, $s0, -1
 
for2:    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    for2
 
exit2:    addi    $s0, $s0, 1
    j    for1
 
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
 
 
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

i know there could be an issue with my loop but I havent been able to fix it. If my method is too long or doesnt make sense please suggest another if possible. I am running out of ideas.

It could be an alignment issue.... is an allignment exception being thrown? Also, when you load the most significant bit (lui) the leas significat bit are zeroed out. Last, the t* registeres aren't preserved across a prodcedure call. I'm not certain, but those are some things I could think of.

Good luck anyway, LamaBot;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.