Advertisement
Guest User

Selection sort -- bugged -- mips assembler

a guest
Apr 12th, 2012
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###############################################################################
  2. ############################## DATA SECTION ###################################
  3. ###############################################################################
  4.     .data
  5. datalen:
  6.   .word 0x0010  # 16
  7. data:
  8.   .word 0xffff7e81
  9.   .word 0x00000001
  10.   .word 0x00000002
  11.   .word 0xffff0001
  12.   .word 0x00000000
  13.   .word 0x00000001
  14.   .word 0xffffffff
  15.   .word 0x00000000
  16.   .word 0xe3456687
  17.   .word 0xa001aa88
  18.   .word 0xf0e159ea
  19.   .word 0x9152137b
  20.   .word 0xaab385a1
  21.   .word 0x31093c54
  22.   .word 0x42102f37
  23.   .word 0x00ee655b
  24.   .word   0,0      # padding.... This shoulrd not be needed...
  25.  
  26.     .data
  27. newline:
  28.   .word   0x0a
  29.  
  30. ###############################################################################
  31. ############################# PROGRAM SECTION #################################
  32. ###############################################################################
  33.     .text
  34. main:
  35.   ##
  36.   # $s1 addres to data arry
  37.   # $s2 data arry length
  38.   #
  39.   # $s3 inner loop itterator -- i
  40.   # $s4 outer loop itterator -- j
  41.   #
  42.   # $s5 indexOfLastLowValue
  43.   # $s6 lastLowValue
  44.   #
  45.   # $t9 innerloop slt -- set to one if innerloop has reached the end.
  46.   #     Do Not Change...
  47.   # $t8 outerloop slt -- set to one if outerlaap has reached the end.
  48.   #     Do Not Change...
  49.  
  50.   la   $s1, data              # addres to data
  51.   lw   $s2, datalen           # datalen
  52.   nop
  53.   addi $s2, $s2, -1           # WHY?!?!?!?
  54.  
  55.   ######## Print the array before sortig
  56.   printa:
  57.     slt  $t9, $s2, $s3
  58.     seq  $t9, $s2, $s3
  59.     nop
  60.     # print number
  61.     sll $t3, $s3, 2
  62.     add $t3, $t3, $s1
  63.     nop
  64.     lw $a0, ($t3)
  65.     li $v0, 1
  66.     nop
  67.     syscall
  68.     nop
  69.     # print newline
  70.     la $a0, newline
  71.     li $v0, 4
  72.     nop
  73.     syscall
  74.     nop
  75.     addi $s3, $s3 ,1
  76.     beqz $t9, printa
  77.     nop
  78.     add $t9, $zero, $zero
  79.     # print newline
  80.     la $a0, newline
  81.     li $v0, 4
  82.     nop
  83.     syscall
  84.     nop
  85.     ############# END Print the array before sortig
  86.  
  87.  
  88.   add  $s3, $zero, $zero      # i
  89.   add  $s4, $zero, $zero      # j
  90.  
  91.   add  $s5, $zero, $zero      # indexOfLastLow
  92.   add  $s6, $zero, $zero      # lastLowValue
  93.  
  94.   add  $t9, $zero, $zero      # set to one if i >= datalen
  95.   add  $t8, $zero, $zero      # set to one if j >= datalen
  96.  
  97.   outerloop:
  98.     seq  $t9, $s2, $s3
  99.     slt  $t9, $s2, $s3       # if datalen < i
  100.     # j = i + 1
  101.     addi $s4, $s3, 1
  102.     # indexOfLastLow = i
  103.     add  $s5, $s3, $zero
  104.     # lastLowValue = data[i]
  105.     sll  $t4, $s3, 2         # t4 = t4 * 4      -- so it becames a word counter
  106.     nop
  107.     add  $t4, $t4, $s1       # t4 = t4 + *data  -- so that t4 poins to the
  108.     nop                      # address of the element
  109.     lw   $s6, ($t4)          # load the element into lastLowValue
  110.    
  111.     innerloop:
  112.       seq  $t8, $s2, $s4
  113.       slt  $t8, $s2, $s4     # if datalen < j
  114.       # if array[j] < lastLowValue
  115.       sll $t5, $s4, 2
  116.       nop
  117.       add $t5, $t5, $s1
  118.       nop
  119.       lw $t5, ($t5)
  120.         nop
  121.       blt $s6, $t5, endinnerloop # if lastLowValue is bigger then data[j]
  122.       nop
  123.       add $s6, $t5, $zero
  124.       add $s5, $s4, $zero
  125.       nop
  126.      
  127.       endinnerloop:
  128.       # jump to innerloop if datalen >= j
  129.       addi $s4, $s4, 1
  130.       beqz  $t8 innerloop
  131.       nop
  132.       # end of innerloop
  133.  
  134.     #### Swap elements
  135.     nop
  136.     # reset j
  137.     add  $s4, $zero, $zero
  138.     nop
  139.     #loads data[i]
  140.     sll $t7, $s3, 2
  141.     add $t7, $s1, $t7
  142.     lw $t5, ($t7)
  143.     nop
  144.     # loads data[inderOfLastLow]
  145.     sll $t3, $s5, 2
  146.     nop
  147.     add $t3, $s1, $t3
  148.     nop
  149.     lw $t6, ($t3)
  150.     nop
  151.     # Does the swap
  152.     sw $t5, ($t3)
  153.     sw $t6, ($t7)
  154.     nop
  155.     ### end swap elements
  156.     # Reset j
  157.     add $s4, $zero, $zero
  158.     addi $s3, $s3, 1
  159.     nop
  160.     # jump back
  161.     beqz $t9, outerloop
  162.     ##### end of outerloop
  163.   nop
  164.   add $s3, $zero, $zero
  165.   add $t9, $zero, $zero
  166.   nop
  167.   #Print the sortetd array
  168.   print:
  169.     seq  $t9, $s2, $s3
  170.     slt  $t9, $s2, $s3
  171.     nop
  172.     # print number
  173.     sll $t3, $s3, 2
  174.     add $t3, $t3, $s1
  175.     nop
  176.     lw $a0, ($t3)
  177.     li $v0, 1
  178.     nop
  179.     syscall
  180.     nop
  181.     # print newline
  182.     la $a0, newline
  183.     li $v0, 4
  184.     nop
  185.     syscall
  186.     nop
  187.     addi $s3, $s3 ,1
  188.     beqz $t9, print
  189.     nop
  190.     exit:
  191.       li $v0, 10
  192.     syscall
  193.     nop
  194.   #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement