Advertisement
gutierrezjlucas

bubble-sort.mas

Apr 24th, 2023 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// GUTIERREZ JUAN LUCAS 2023
  2. /// MARIE test bubble sort algorithm
  3.  
  4. ORG 000
  5. Jump START
  6. ARRAY,  DEC 1   // SIZE
  7.         DEC 1   // FIRST ELEMENT
  8.                                                            
  9.  
  10. START,  Load    ARRAY
  11.         Store   SIZE        // load SIZE with the size of the array
  12.        
  13. FORj,   Load    START_ADDR
  14.         Store   CURRENT
  15.         Load    ZERO        // clear INDEX_I and IS_VAL
  16.         Store   INDEX_I
  17.         Store   IS_SWAP
  18.         Load    INDEX_J     // INDEX_J++
  19.         Add     ONE    
  20.         Store   INDEX_J
  21.         Subt    SIZE       
  22.         Skipcond 400        // if INDEX_J == SIZE then jump END else jump FORi
  23.         Jump    FORi
  24.         Jump    END
  25.        
  26. FORi,   Load    INDEX_I     // INDEX_I++
  27.         Add     ONE
  28.         Store   INDEX_I    
  29.         Add     INDEX_J     // INDEX_I + INDEX_J
  30.         Subt    SIZE        // if INDEX_I + INDEX_J == SIZE then jump CHECK_PRE_END else jump COMPARE current and next
  31.         Skipcond 400
  32.         Jump    COMPARE
  33.         Jump    CHECK_PRE_END
  34. DO,     Load    CURRENT     // CURRENT++
  35.         Add     ONE
  36.         Store   CURRENT
  37.         Jump    FORi
  38.  
  39. COMPARE,    Load    CURRENT
  40.             Add     ONE
  41.             Store   NEXT
  42.             LoadI   NEXT
  43.             Store   NEXT_VAL
  44.             LoadI   CURRENT
  45.             Subt    NEXT_VAL
  46.             Skipcond 0
  47.             Jump    SWAP
  48.             Jump    DO 
  49.  
  50. SWAP,   Load ONE
  51.         Store IS_SWAP   // IS_SWAP = true
  52.         Loadi CURRENT
  53.         Store AUX
  54.         Load NEXT_VAL
  55.         StoreI CURRENT
  56.         Load AUX
  57.         StoreI NEXT
  58.         Jump DO
  59.        
  60. // OPTIMIZATION
  61. CHECK_PRE_END,  Load    IS_SWAP        
  62.                 Skipcond 400    // if IS_SWAP == 0 then no elements has been swapped and then we are done
  63.                 Jump    FORj
  64.                 Jump    END
  65.  
  66. END, Halt
  67.  
  68. PRINT,  LoadI CURRENT
  69.         Output
  70.         Load CURRENT
  71.         Add ONE
  72.         Store CURRENT
  73.         Load INDEX_I
  74.         Add ONE
  75.         Store INDEX_I
  76.         Load SIZE
  77.         Subt INDEX_I
  78.         Skipcond 400
  79.         Jump PRINT
  80.         Jump END
  81.  
  82.        
  83. SIZE,       DEC 0
  84. ONE,        HEX 001
  85. ZERO,       HEX 000
  86. INDEX_I,    DEC 000
  87. INDEX_J,    DEC 000
  88. START_ADDR, HEX 002
  89. CURRENT,    HEX 001
  90. NEXT,       HEX 000
  91. NEXT_VAL,   DEC 000
  92. AUX,        DEC 000
  93. IS_SWAP,    HEX 000
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement