0

I want to implement a merge sort in LMC where I input a pre-sorted array of values and store them then input a second pre-sorted array of values and merge sort them with the first array but I'm having a lot of trouble with my sorting and array shift loops. I'm using label to know where to start shifting down the array to insert smaller values. Entering 1, 2, 0 and then 2, 3, 0 should output (003 002 002 001).

arrayInput (IN) ;; Enter the first array of numbers.
    (BRZ sortLoop)
storeArray (DAT 380) ;; Storing the array of numbers.
    (LDA arrayInput)
    (ADD increment)
    (STO arrayInput)
    (LDA counter)
    (ADD increment)
    (STO counter)
    (BR arrayInput)
sortLoop (IN) ;; Insert the second array of numbers and start sorting here.
    (BRZ outputLoop)
    (STO temp)
    (LDA 80)
    (SUB temp)
    (BRP shiftDownArray)
    (BR sortLoop)
shiftDownArray (BR label) ;; Shifting down the array everytime we find a smaller value.
    (LDA label)
    (ADD increment)
outputLoop (DAT 380) ;; Output the results.
    (OUT)
    (LDA outputLoop)
    (ADD increment)
    (STO outputLoop)
    (LDA counter)
    (SUB increment)
    (STA counter)
    (BRZ end)
    (BR outputLoop)
end (HLT)
label (LDA arrayInput)
    (ADD counter)
    (STO label)
    (BR shiftDownArray)
counter (DAT 000)
increment (DAT 001)
temp (DAT 000)
Welcor
  • 2,431
  • 21
  • 32
  • Single step the code and see where it goes wrong. – Jester Mar 03 '15 at 19:42
  • shiftDownArray doesn't work so I'm looking for suggestions on how to fix, ie shift down an array to insert smaller values and then keep sorting. – Lou E. Slugger Mar 03 '15 at 21:06
  • So you do not only want to merge, you also want the sort order to be the inverse of the original sort order. Nothing in your code deals with that reversal. Besides that, there are so many errors in this code (e.g. `STO arrayInput` will destroy the `INP ` instruction), that it is hard to see what you intended to do. – trincot Oct 19 '20 at 18:46

0 Answers0