Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Homework 1
- #======================Description=================================
- #Implementation of a selection sort, used here to sort
- #a list of unsorted integers and print the result
- #to the console. The unsorted input should be stored in the "IntList" memory location,
- #with the program prompting for the length of the list. The program will store the
- #sorted list in the same location.
- #======================Algorithmn==================================
- #Each "iteration" of the algorithmn will define a "sorted" and "unsorted" section of the
- #overall list of numbers. Starting at the zero index position and incrementing upward, the
- #program will determine the lowest # in the unsorted section (eval/switchlow), and proceed to swap (swap)
- #it with the number at the lowest index of the unsorted section. The start of the unsorted section is
- #then added to the sorted section, and the algorithmn continues.
- #==================================================================
- #Register Usage:
- # $t0 - Index of start of unsorted section
- # $t1 - Offset of smallest number
- # $t2 - Loop counter
- # $t3/$t4 - Holding tmp integers for magnitude comparison
- # $t6/$t7 - Temp vars for swapping integers
- # $a1 - Offset of # to swap
- # $a2 - Length of arr to sort
- .data
- Prompt: .asciiz "Enter length of array:"
- Space: .asciiz " "
- .align 2
- IntList: .word 92, 35, 8, 66, 3, 57, 58, 80, 53, 28, 60, 23, 74, 6, 62, 62, 25, 37, 37, 5, 95, 38, 97, 14, 65 #Unsorted array of integers
- .text
- .globl main
- main: li $v0, 4 #Print prompt to console
- la $a0, Prompt
- syscall
- li $v0, 5 #Read in array length, store to $a2
- syscall
- move $a2, $v0
- sll $a2, $a2, 2 #convert to offset
- li $t0, 0 #init index of start to unsorted section
- li $t1, 0 #init small # offset
- li $t2, 4 #init loop counter
- eval: beq $t2, $a2, swap #Sort through the unsorted section and find the lowest #
- lw $t3, IntList($t1) #Compare the current number to the lowest valued number so far
- lw $t4, IntList($t2)
- blt $t4, $t3, switchlow #Swap the two if necessary
- addi $t2, $t2, 4
- j eval
- switchlow:
- add $t1, $0, $t2 #Change current lowest val integer
- addi $t2, $t2, 4 #Inc loop counter
- j eval
- contloop: addi $t0, $t0, 4 #Inc start of unsorted section
- addi $t1, $t0, 0 #Set small # offset
- add $t2, $t1, $0 #Set loop counter to start of unsorted section
- addi $t2, $t2, 4 #Inc loop counter by 1 entry
- beq $t0, $a2, end #end prog if end reached
- j eval
- swap: lw $t6, IntList($t0) #Read in integers to be swapped
- lw $t7, IntList($t1)
- sw $t7, IntList($t0) #Write to mem
- sw $t6, IntList($t1)
- jr contloop #Continue
- end: li $t0, 0
- li $v0, 10 # code for program end
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement