Trouble Looping Through Array Bubble Sort MIPS












0














I'm having some trouble getting my bubble sort to work correctly in MIPS. It sorts the first two numbers but doesn't touch the rest for example, if I put in the numbers 3,2,1 it will print out 2,3,1. I imagine this is due to some kind of looping mistake and is likely easily fixed but I cannot for the life of me figure out how to fix it. The program asks the user to input numbers then puts them in an array and sorts them.



The problem is in the sort procedure.



.data
vals: .space 40
numberOfNumPrompt: .asciiz "Enter amount of numbers to store (1-10): "
enterNumPrompt: .asciiz "Enter number to store: "
errorMessege: .asciiz "Number must be between 1 and 10. Press Enter to try again."
commaSeperator: .asciiz ", "

.text
.globl main
main:

la $a1, vals # base address of the array
li $a2, 9 # store number 9 in register $a2

li $t0, 0 # set increment variable
addi $s2, $s2, 1
j prompt
inputError: # display input error message

la $a0, errorMessege
li $v0, 4
syscall
li $v0, 5
syscall
j prompt

prompt: # diplay prompt for how many numbers to sort

la $a0, numberOfNumPrompt
li $v0, 4
syscall
li $v0, 5
syscall
move $s0, $v0 # move input before it gets changed
bge $s0, 11, inputError # if number input is > 10 go to inputError procedure
ble $s0, 1, inputError # if number input is < 1 go to inputError procedure

loop: # get numbers and put them into the array

bgt $s2, $s0, sort
la $a0, enterNumPrompt
li $v0, 4
syscall
li $v0, 5
syscall
sw $v0, ($a1)
addi $a1, $a1, 4 # move the array over by 1 element
addi $s2, $s2, 1
j loop

sort:

la $t4, vals #t0 is number up to outter loop
la $t1, vals #t1 is number comparing to inner loop

addi $t1,$t1,4
la $t8,vals
add $t8,$t0,$t8
la $t9,vals
add $t9,$t0,$t9
addi $t9,$t9,-4
loops:
lw $t2,($t4) #get number 1 outter loop
lw $t3,($t1) #get number 2 inner loop
blt $t2,$t3,next #don't need to swap
sw $t3,($t4) #swap
sw $t2,($t1)
next:
addi $t1,$t1,4
blt $t1,$t8,loops #check inner loop
addi $t4,$t4,4 #increment outter loop
move $t1,$t4
addi $t1,$t1,4
blt $t4,$t9,loops #check outter loop
j printArray

printArray:
la $a1,vals
add $t0, $t0, $s0
loop1:
blez $t0, done
li $v0, 1
lw $a0, 0($a1)
syscall
la $a0, commaSeperator
li $v0, 4
syscall
addi $a1, $a1, 4
addi $t0, $t0, -1
j loop1
done:
jr $ra
j done









share|improve this question
























  • See my answer here: stackoverflow.com/questions/33991923/bubble-sorting-using-mips
    – Craig Estey
    Apr 15 '16 at 4:56










  • Try a debugger, it helps to visualise what your code is doing
    – S.Klumpers
    Apr 16 '16 at 6:24
















0














I'm having some trouble getting my bubble sort to work correctly in MIPS. It sorts the first two numbers but doesn't touch the rest for example, if I put in the numbers 3,2,1 it will print out 2,3,1. I imagine this is due to some kind of looping mistake and is likely easily fixed but I cannot for the life of me figure out how to fix it. The program asks the user to input numbers then puts them in an array and sorts them.



The problem is in the sort procedure.



.data
vals: .space 40
numberOfNumPrompt: .asciiz "Enter amount of numbers to store (1-10): "
enterNumPrompt: .asciiz "Enter number to store: "
errorMessege: .asciiz "Number must be between 1 and 10. Press Enter to try again."
commaSeperator: .asciiz ", "

.text
.globl main
main:

la $a1, vals # base address of the array
li $a2, 9 # store number 9 in register $a2

li $t0, 0 # set increment variable
addi $s2, $s2, 1
j prompt
inputError: # display input error message

la $a0, errorMessege
li $v0, 4
syscall
li $v0, 5
syscall
j prompt

prompt: # diplay prompt for how many numbers to sort

la $a0, numberOfNumPrompt
li $v0, 4
syscall
li $v0, 5
syscall
move $s0, $v0 # move input before it gets changed
bge $s0, 11, inputError # if number input is > 10 go to inputError procedure
ble $s0, 1, inputError # if number input is < 1 go to inputError procedure

loop: # get numbers and put them into the array

bgt $s2, $s0, sort
la $a0, enterNumPrompt
li $v0, 4
syscall
li $v0, 5
syscall
sw $v0, ($a1)
addi $a1, $a1, 4 # move the array over by 1 element
addi $s2, $s2, 1
j loop

sort:

la $t4, vals #t0 is number up to outter loop
la $t1, vals #t1 is number comparing to inner loop

addi $t1,$t1,4
la $t8,vals
add $t8,$t0,$t8
la $t9,vals
add $t9,$t0,$t9
addi $t9,$t9,-4
loops:
lw $t2,($t4) #get number 1 outter loop
lw $t3,($t1) #get number 2 inner loop
blt $t2,$t3,next #don't need to swap
sw $t3,($t4) #swap
sw $t2,($t1)
next:
addi $t1,$t1,4
blt $t1,$t8,loops #check inner loop
addi $t4,$t4,4 #increment outter loop
move $t1,$t4
addi $t1,$t1,4
blt $t4,$t9,loops #check outter loop
j printArray

printArray:
la $a1,vals
add $t0, $t0, $s0
loop1:
blez $t0, done
li $v0, 1
lw $a0, 0($a1)
syscall
la $a0, commaSeperator
li $v0, 4
syscall
addi $a1, $a1, 4
addi $t0, $t0, -1
j loop1
done:
jr $ra
j done









share|improve this question
























  • See my answer here: stackoverflow.com/questions/33991923/bubble-sorting-using-mips
    – Craig Estey
    Apr 15 '16 at 4:56










  • Try a debugger, it helps to visualise what your code is doing
    – S.Klumpers
    Apr 16 '16 at 6:24














0












0








0







I'm having some trouble getting my bubble sort to work correctly in MIPS. It sorts the first two numbers but doesn't touch the rest for example, if I put in the numbers 3,2,1 it will print out 2,3,1. I imagine this is due to some kind of looping mistake and is likely easily fixed but I cannot for the life of me figure out how to fix it. The program asks the user to input numbers then puts them in an array and sorts them.



The problem is in the sort procedure.



.data
vals: .space 40
numberOfNumPrompt: .asciiz "Enter amount of numbers to store (1-10): "
enterNumPrompt: .asciiz "Enter number to store: "
errorMessege: .asciiz "Number must be between 1 and 10. Press Enter to try again."
commaSeperator: .asciiz ", "

.text
.globl main
main:

la $a1, vals # base address of the array
li $a2, 9 # store number 9 in register $a2

li $t0, 0 # set increment variable
addi $s2, $s2, 1
j prompt
inputError: # display input error message

la $a0, errorMessege
li $v0, 4
syscall
li $v0, 5
syscall
j prompt

prompt: # diplay prompt for how many numbers to sort

la $a0, numberOfNumPrompt
li $v0, 4
syscall
li $v0, 5
syscall
move $s0, $v0 # move input before it gets changed
bge $s0, 11, inputError # if number input is > 10 go to inputError procedure
ble $s0, 1, inputError # if number input is < 1 go to inputError procedure

loop: # get numbers and put them into the array

bgt $s2, $s0, sort
la $a0, enterNumPrompt
li $v0, 4
syscall
li $v0, 5
syscall
sw $v0, ($a1)
addi $a1, $a1, 4 # move the array over by 1 element
addi $s2, $s2, 1
j loop

sort:

la $t4, vals #t0 is number up to outter loop
la $t1, vals #t1 is number comparing to inner loop

addi $t1,$t1,4
la $t8,vals
add $t8,$t0,$t8
la $t9,vals
add $t9,$t0,$t9
addi $t9,$t9,-4
loops:
lw $t2,($t4) #get number 1 outter loop
lw $t3,($t1) #get number 2 inner loop
blt $t2,$t3,next #don't need to swap
sw $t3,($t4) #swap
sw $t2,($t1)
next:
addi $t1,$t1,4
blt $t1,$t8,loops #check inner loop
addi $t4,$t4,4 #increment outter loop
move $t1,$t4
addi $t1,$t1,4
blt $t4,$t9,loops #check outter loop
j printArray

printArray:
la $a1,vals
add $t0, $t0, $s0
loop1:
blez $t0, done
li $v0, 1
lw $a0, 0($a1)
syscall
la $a0, commaSeperator
li $v0, 4
syscall
addi $a1, $a1, 4
addi $t0, $t0, -1
j loop1
done:
jr $ra
j done









share|improve this question















I'm having some trouble getting my bubble sort to work correctly in MIPS. It sorts the first two numbers but doesn't touch the rest for example, if I put in the numbers 3,2,1 it will print out 2,3,1. I imagine this is due to some kind of looping mistake and is likely easily fixed but I cannot for the life of me figure out how to fix it. The program asks the user to input numbers then puts them in an array and sorts them.



The problem is in the sort procedure.



.data
vals: .space 40
numberOfNumPrompt: .asciiz "Enter amount of numbers to store (1-10): "
enterNumPrompt: .asciiz "Enter number to store: "
errorMessege: .asciiz "Number must be between 1 and 10. Press Enter to try again."
commaSeperator: .asciiz ", "

.text
.globl main
main:

la $a1, vals # base address of the array
li $a2, 9 # store number 9 in register $a2

li $t0, 0 # set increment variable
addi $s2, $s2, 1
j prompt
inputError: # display input error message

la $a0, errorMessege
li $v0, 4
syscall
li $v0, 5
syscall
j prompt

prompt: # diplay prompt for how many numbers to sort

la $a0, numberOfNumPrompt
li $v0, 4
syscall
li $v0, 5
syscall
move $s0, $v0 # move input before it gets changed
bge $s0, 11, inputError # if number input is > 10 go to inputError procedure
ble $s0, 1, inputError # if number input is < 1 go to inputError procedure

loop: # get numbers and put them into the array

bgt $s2, $s0, sort
la $a0, enterNumPrompt
li $v0, 4
syscall
li $v0, 5
syscall
sw $v0, ($a1)
addi $a1, $a1, 4 # move the array over by 1 element
addi $s2, $s2, 1
j loop

sort:

la $t4, vals #t0 is number up to outter loop
la $t1, vals #t1 is number comparing to inner loop

addi $t1,$t1,4
la $t8,vals
add $t8,$t0,$t8
la $t9,vals
add $t9,$t0,$t9
addi $t9,$t9,-4
loops:
lw $t2,($t4) #get number 1 outter loop
lw $t3,($t1) #get number 2 inner loop
blt $t2,$t3,next #don't need to swap
sw $t3,($t4) #swap
sw $t2,($t1)
next:
addi $t1,$t1,4
blt $t1,$t8,loops #check inner loop
addi $t4,$t4,4 #increment outter loop
move $t1,$t4
addi $t1,$t1,4
blt $t4,$t9,loops #check outter loop
j printArray

printArray:
la $a1,vals
add $t0, $t0, $s0
loop1:
blez $t0, done
li $v0, 1
lw $a0, 0($a1)
syscall
la $a0, commaSeperator
li $v0, 4
syscall
addi $a1, $a1, 4
addi $t0, $t0, -1
j loop1
done:
jr $ra
j done






sorting assembly mips bubble-sort






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 4:44









Cœur

17.4k9103145




17.4k9103145










asked Apr 15 '16 at 4:50









NaterTot

11




11












  • See my answer here: stackoverflow.com/questions/33991923/bubble-sorting-using-mips
    – Craig Estey
    Apr 15 '16 at 4:56










  • Try a debugger, it helps to visualise what your code is doing
    – S.Klumpers
    Apr 16 '16 at 6:24


















  • See my answer here: stackoverflow.com/questions/33991923/bubble-sorting-using-mips
    – Craig Estey
    Apr 15 '16 at 4:56










  • Try a debugger, it helps to visualise what your code is doing
    – S.Klumpers
    Apr 16 '16 at 6:24
















See my answer here: stackoverflow.com/questions/33991923/bubble-sorting-using-mips
– Craig Estey
Apr 15 '16 at 4:56




See my answer here: stackoverflow.com/questions/33991923/bubble-sorting-using-mips
– Craig Estey
Apr 15 '16 at 4:56












Try a debugger, it helps to visualise what your code is doing
– S.Klumpers
Apr 16 '16 at 6:24




Try a debugger, it helps to visualise what your code is doing
– S.Klumpers
Apr 16 '16 at 6:24












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36638492%2ftrouble-looping-through-array-bubble-sort-mips%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36638492%2ftrouble-looping-through-array-bubble-sort-mips%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python