Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- prompt: .asciiz "Please input 5 numbers.\n"
- outPrompt: .asciiz "The sorted numbers are:\n"
- newline: .asciiz "\n"
- .align 4
- array: .space 20 #4 bytes per number, 5 numbers
- .text
- #storing the array
- la $s0, array
- #tell user for input
- li $v0, 4
- la $a0, prompt
- syscall
- #jump to collect input
- jal input
- #variables for sorting
- li $t0, -1 #$t0 = i
- li $t1, 25 #number of possible total swaps that can occur, given 5 numbers (n^2)
- #jump to begin sorting (loop1)
- j loop1
- j exit
- input:
- #get input and store it
- li $v0, 5
- syscall
- sw $v0, 0($s0)
- #get input and store it
- li $v0, 5
- syscall
- sw $v0, 4($s0)
- #get input and store it
- li $v0, 5
- syscall
- sw $v0, 8($s0)
- #get input and store it
- li $v0, 5
- syscall
- sw $v0, 12($s0)
- #get input and store it
- li $v0, 5
- syscall
- sw $v0, 16($s0)
- jr $ra
- loop1:
- addi $t0, $t0, 1
- beq $t0, $t1, output
- #code
- jal loop2
- loop2:
- #loading the value we're checking to swap
- lw $t2, 0($s0)
- #loading j + 1, the next value in the array
- lw $t3, 4($s0)
- #if j < j + 1, switch
- blt $t2, $t3, switch2
- #otherwise, return to loop 1
- j loop1
- switch2:
- #switch
- sw $t3, 0($s0)
- sw $t2, 4($s0)
- jal current
- #to the next loop
- j loop3
- loop3:
- #loading the value we're checking to swap
- lw $t2, 4($s0)
- #loading j + 1, the next value in the array
- lw $t3, 8($s0)
- #if j < j + 1, switch
- blt $t2, $t3, switch3
- #otherwise, return to loop 1
- j loop1
- switch3:
- #switch
- sw $t3, 4($s0)
- sw $t2, 8($s0)
- jal current
- #to the next loop
- j loop4
- loop4:
- #loading the value we're checking to swap
- lw $t2, 8($s0)
- #loading j + 1, the next value in the array
- lw $t3, 12($s0)
- #if j < j + 1, switch
- blt $t2, $t3, switch4
- #otherwise, return to loop 1
- j loop1
- switch4:
- #switch
- sw $t3, 8($s0)
- sw $t2, 12($s0)
- jal current
- #to the next loop
- j loop5
- loop5:
- #loading the value we're checking to swap
- lw $t2, 8($s0)
- #loading j + 1, the next value in the array
- lw $t3, 12($s0)
- #if j < j + 1, switch
- blt $t2, $t3, switch4
- #otherwise, return to loop 1
- j loop1
- switch5:
- #switch
- sw $t3, 12($s0)
- sw $t2, 16($s0)
- jal current
- #to the next loop
- j loop5
- current:
- #pull value in array and print it
- lw $t7, 0($s0)
- li $v0, 1
- move $a0, $t7
- syscall
- #pull value in array and print it
- lw $t7, 4($s0)
- li $v0, 1
- move $a0, $t7
- syscall
- #pull value in array and print it
- lw $t7, 8($s0)
- li $v0, 1
- move $a0, $t7
- syscall
- #pull value in array and print it
- lw $t7, 12($s0)
- li $v0, 1
- move $a0, $t7
- syscall
- #pull value in array and print it
- lw $t7, 16($s0)
- li $v0, 1
- move $a0, $t7
- syscall
- #print new line
- li $v0, 4
- la $a0, newline
- syscall
- jr $ra
- output:
- exit:
- li $v0, 10
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement