Advertisement
Guest User

Untitled

a guest
Jul 11th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. #Homework 1
  2. #======================Description=================================
  3. #Implementation of a selection sort, used here to sort
  4. #a list of unsorted integers and print the result
  5. #to the console. The unsorted input should be stored in the "IntList" memory location,
  6. #with the program prompting for the length of the list. The program will store the
  7. #sorted list in the same location.
  8. #======================Algorithmn==================================
  9. #Each "iteration" of the algorithmn will define a "sorted" and "unsorted" section of the
  10. #overall list of numbers. Starting at the zero index position and incrementing upward, the
  11. #program will determine the lowest # in the unsorted section (eval/switchlow), and proceed to swap (swap)
  12. #it with the number at the lowest index of the unsorted section. The start of the unsorted section is
  13. #then added to the sorted section, and the algorithmn continues.
  14. #==================================================================
  15.  
  16. #Register Usage:
  17. # $t0 - Index of start of unsorted section
  18. # $t1 - Offset of smallest number
  19. # $t2 - Loop counter
  20. # $t3/$t4 - Holding tmp integers for magnitude comparison
  21. # $t6/$t7 - Temp vars for swapping integers
  22. # $a1 - Offset of # to swap
  23. # $a2 - Length of arr to sort
  24.  
  25. .data
  26. Prompt: .asciiz "Enter length of array:"
  27. Space: .asciiz " "
  28. .align 2
  29. 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
  30.  
  31. .text
  32. .globl main
  33.  
  34. main: li $v0, 4 #Print prompt to console
  35. la $a0, Prompt
  36. syscall
  37.  
  38. li $v0, 5 #Read in array length, store to $a2
  39. syscall
  40. move $a2, $v0
  41. sll $a2, $a2, 2 #convert to offset
  42.  
  43. li $t0, 0 #init index of start to unsorted section
  44. li $t1, 0 #init small # offset
  45. li $t2, 4 #init loop counter
  46.  
  47. eval: beq $t2, $a2, swap #Sort through the unsorted section and find the lowest #
  48. lw $t3, IntList($t1) #Compare the current number to the lowest valued number so far
  49. lw $t4, IntList($t2)
  50. blt $t4, $t3, switchlow #Swap the two if necessary
  51. addi $t2, $t2, 4
  52. j eval
  53.  
  54. switchlow:
  55. add $t1, $0, $t2 #Change current lowest val integer
  56. addi $t2, $t2, 4 #Inc loop counter
  57. j eval
  58.  
  59. contloop: addi $t0, $t0, 4 #Inc start of unsorted section
  60. addi $t1, $t0, 0 #Set small # offset
  61. add $t2, $t1, $0 #Set loop counter to start of unsorted section
  62. addi $t2, $t2, 4 #Inc loop counter by 1 entry
  63. beq $t0, $a2, end #end prog if end reached
  64. j eval
  65.  
  66.  
  67.  
  68. swap: lw $t6, IntList($t0) #Read in integers to be swapped
  69. lw $t7, IntList($t1)
  70. sw $t7, IntList($t0) #Write to mem
  71. sw $t6, IntList($t1)
  72. jr contloop #Continue
  73.  
  74. end: li $t0, 0
  75. li $v0, 10 # code for program end
  76. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement