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)