我想在 LMC 中实现合并排序,在其中输入一个预先排序的值数组并存储它们,然后输入第二个预先排序的值数组并将它们与第一个数组合并排序,但我遇到了很多麻烦我的排序和数组移位循环。我正在使用标签知道从哪里开始向下移动数组以插入较小的值。输入 1, 2, 0 然后输入 2, 3, 0 应该输出 (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)