Advertisement
Guest User

Untitled

a guest
Oct 26th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Lab 5
  3. ////////////////////////////////////////////////////////////////////////////////
  4.  
  5. // Include the checkoff program:
  6. .include "checkoff.uasm"
  7.  
  8. // Leave the following as zero to run ALL the test cases, and get your solution
  9. //   validated if all pass.  If you have trouble with test case N, set it to N
  10. //   to run JUST that test case (for easier debugging):
  11. TestCase:       LONG(0)
  12.  
  13. // Quicksort-in-place code.  We include the C/Python version here as a comment;
  14. // you can use this as a model for your Beta assembly version:
  15.  
  16. //def partition(array,left,right):
  17. //    # choose middle element of array as pivot
  18. //    pivotIndex = (left+right) >> 1;
  19. //    pivotValue = array[pivotIndex]
  20. //
  21. //    # swap array[right] and array[pivotIndex]
  22. //    # note that we already store array[pivotIndex] in pivotValue
  23. //    array[pivotIndex] = array[right]
  24. //
  25. //    # elements <= the pivot are moved to the left (smaller indices)
  26. //    storeIndex = left
  27. //    for i in xrange(left,right):  # don't include array[right]
  28. //        temp = array[i]
  29. //        if temp <= pivotValue:
  30. //            array[i] = array[storeIndex]
  31. //            array[storeIndex] = temp
  32. //            storeIndex += 1
  33. //
  34. //    # move pivot to its final place
  35. //    array[right] = array[storeIndex]
  36. //    array[storeIndex] = pivotValue;
  37. //    return storeIndex;
  38.  
  39. partition:
  40.         PUSH(LP)        // standard entry sequence
  41.        PUSH(BP)
  42.        MOVE(SP, BP)
  43.        
  44.         // save registers we use below
  45.         PUSH(R1)
  46.         PUSH(R2)
  47.         PUSH(R3)
  48.         PUSH(R4)
  49.         PUSH(R5)
  50.         PUSH(R6)
  51.         PUSH(R7)
  52.         PUSH(R8)
  53.         PUSH(R9)
  54.         PUSH(R10)
  55.         PUSH(R11)
  56.         PUSH(R12)
  57.         PUSH(R13)
  58.  
  59.         LD(BP, -12, R3) // R3 = address of array[0]
  60.         LD(BP, -16, R2) // R2 = left
  61.         LD(BP, -20, R1) // R1 = right
  62.  
  63.         // pivotIndex = (left + right) >> 1
  64.         ADD(R1, R2, R0) // R0 = left + right
  65.         SHRC(R0, 1, R4) // R4 = (left + right) >> 1 = pivotIndex
  66.        
  67.         // pivotValue = array[pivotIndex]
  68.         MULC(R4, 4, R0) // R0 = pivotIndex*4
  69.         ADD(R3, R0, R0) // R0 = address of array[0] + 4*pivotIndex
  70.                         //    = address of array[pivotIndex]
  71.         LD(R0, 0, R5)   // R5 = array[pivotIndex] = pivotValue
  72.        
  73.         // array[pivotIndex] = array[right]
  74.         MULC(R1, 4, R6) // R6 = right*4
  75.         ADD(R3, R6, R6) // R6 = address of array[0] + 4*right
  76.                         //    = address of array[right]
  77.         LD(R6, 0, R6)   // R6 = Mem(address of array[right]) = array[right]
  78.         ST(R6, 0, R0)   // Mem[address of array[pivotIndex]] = array[right]
  79.        
  80.         // storeIndex = left
  81.         ADDC(R2, 0, R6) // R6 = left + 0 = left = storeIndex
  82.        
  83.         // for i in xrange(left, right)
  84.         SUB(R1, R2, R7) // R7 = right - left = iterations left
  85.         ADDC(R2, 0, R8) // R8 = left + 0 = i = counter
  86. start_of_loop:
  87.         BEQ(R7, end_of_loop, R31)
  88.        
  89.         // DO SHIT
  90.         // temp = array[i]
  91.         MULC(R8, 4, R0) // R0 = i*4
  92.         ADD(R3, R0, R0) // R0 = address of array[0] + i*4
  93.                         //    = address of array[i]
  94.         LD(R0, 0, R9)   // R9 = Mem[address of array[i] + 0] = temp
  95.        
  96.         // if temp <= pivotValue:
  97.         CMPLE(R9, R5, R10)  // R10 = bool of "temp <= pivotValue"
  98.         BEQ(R10, end_of_if, R31)    // if-condition check
  99.        
  100.         // array[i] = array[storeIndex]
  101.         MULC(R6, 4, R11)    // R11 = storeIndex * 4
  102.         ADD(R3, R11, R11)   // R11 = address of array[0] + storeIndex*4
  103.                             //     = address of array[storeIndex]
  104.         LD(R11, 0, R12) // R12 = Mem[R11 + 0] = array[storeIndex]
  105.         ST(R12, 0, R0)  // Mem(R0 + 0) = R12
  106.                         // Mem(address of array[i]) = array[storeIndex]
  107.                         // array[i] = array[storeIndex]
  108.        
  109.         // array[storeIndex] = temp
  110.         ST(R9, 0, R11)  // Mem(R11 + 0) = R9
  111.                         // Mem(address of array[storeIndex]) = temp
  112.                         // array[storeIndex] = temp
  113.        
  114.         // storeIndex += 1
  115.         ADDC(R6, 1, R6)
  116.        
  117.         // END DO SHIT
  118.        
  119. end_of_if:
  120.         SUBC(R7, 1, R7)
  121.         ADDC(R8, 1, R8)
  122.         BR(start_of_loop, R31)
  123.        
  124. end_of_loop:
  125.        
  126.         // array[right] = array[storeIndex]
  127.         MULC(R6, 4, R0) // R0 = storeIndex*4
  128.         ADD(R3, R0, R0) // R0 = address of array[0] + storeIndex*4
  129.                         //    = address of array[storeIndex]
  130.         LD(R0, 0, R13)  // R13 = Mem(R0 + 0) = Mem(address of array[storeIndex])
  131.                         //     = array[storeIndex]
  132.         MULC(R1, 4, R0) // R0 = right*4
  133.         ADD(R3, R0, R0) // R0 = address of array[0] + right*4
  134.                         //    = address of array[right]
  135.         ST(R13, 0, R0)  // Mem(R0 + 0) = R13
  136.                         // Mem(address of array[right]) = array[storeIndex]
  137.                         // array[right] = array[storeIndex]
  138.        
  139.         // array[storeIndex] = pivotValue
  140.         MULC(R6, 4, R0) // R0 = storeIndex*4
  141.         ADD(R3, R0, R0) // R0 = address of array[0] + storeIndex*4
  142.                         //    = address of array[storeIndex]
  143.         ST(R5, 0, R0)   // Mem(R0 + 0) = R5
  144.                         // Mem(address of array[storeIndex]) = pivotValue
  145.                         // array[storeIndex] = pivotValue
  146.        
  147.         // return storeIndex
  148.         ADDC(R6, 0, R0) // R0 = R6 + 0 = storeIndex
  149.        
  150.        
  151.         // restore saved registers
  152.         POP(R13)
  153.         POP(R12)
  154.         POP(R11)
  155.         POP(R10)
  156.         POP(R9)
  157.         POP(R8)
  158.         POP(R7)
  159.         POP(R6)
  160.         POP(R5)
  161.         POP(R4)
  162.         POP(R3)
  163.         POP(R2)
  164.         POP(R1)
  165.        
  166.        MOVE(BP, SP)
  167.        POP(BP)
  168.        POP(LP)
  169.        JMP(LP)
  170.  
  171.  
  172. //def quicksort(array, left, right):
  173. //    if left < right:
  174. //        pivotIndex = partition(array,left,right)
  175. //        quicksort(array,left,pivotIndex-1)
  176. //        quicksort(array,pivotIndex+1,right)
  177.  
  178. // quicksort(ArrayBase, left, right)
  179. quicksort:
  180.         PUSH(LP)        // standard entry sequence
  181.        PUSH(BP)
  182.        MOVE(SP, BP)
  183.  
  184.         // save registers we use below
  185.         PUSH(R1)
  186.         PUSH(R2)
  187.         PUSH(R3)
  188.         PUSH(R4)
  189.         PUSH(R5)
  190.        
  191.         LD(BP, -12, R3) // R3 = address of array[0]
  192.         LD(BP, -16, R2) // R2 = left
  193.         LD(BP, -20, R1) // R1 = right
  194.        
  195.         // if left < right
  196.         CMPLT(R2, R1, R4) // R4 = bool of if-condition
  197.         BEQ(R4, end_of_quicksort, R31) // branch if if-condition not true
  198.        
  199.         // call partition, according to templete in L10 slide 23
  200.         // return value will be in R0, which is pivotIndex
  201.         PUSH(R1)            // push 'right'
  202.         PUSH(R2)            // push 'left'
  203.         PUSH(R3)            // push 'ArrayBase'
  204.         BR(partition, LP)   // call partition()
  205.         DEALLOCATE(3)       // clean up!
  206.        
  207.         ADDC(R0, 0, R5)     // move pivotIndex to R5
  208.        
  209.         // quicksort(array, left, pivotIndex - 1)
  210.         SUBC(R5, 1, R0)     // R0 = pivotIndex - 1
  211.         PUSH(R0)            // push 'pivotIndex - 1'
  212.         PUSH(R2)            // push 'left'
  213.         PUSH(R3)            // push 'ArrayBase'
  214.         BR(quicksort, LP)   // call quicksort()
  215.         DEALLOCATE(3)       // clean up!
  216.        
  217.         // quicksort(array, pivotIndex+1, right)
  218.         ADDC(R5, 1, R0)     // R0 = pivotIndex + 1
  219.         PUSH(R1)            // push 'right'
  220.         PUSH(R0)            // push 'pivotIndex + 1'
  221.         PUSH(R3)            // push 'ArrayBase'
  222.         BR(quicksort, LP)   // call quicksort()
  223.         DEALLOCATE(3)       // clean up!
  224.        
  225. end_of_quicksort:
  226.         // restore saved registers
  227.         POP(R5)
  228.         POP(R4)
  229.         POP(R3)
  230.         POP(R2)
  231.         POP(R1)
  232.        
  233.         MOVE(BP, SP)    // standard exit sequence
  234.        POP(BP)
  235.        POP(LP)
  236.        JMP(LP)
  237.  
  238. // Allocate a stack: SP is initialized by checkoff code.
  239. StackBasePtr:
  240.        LONG(StackArea)
  241.  
  242. .unprotect
  243.  
  244. StackArea:
  245.        STORAGE(1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement