在LMC / Assembly中合并排序

时间:2015-03-03 19:39:25

标签: assembly mergesort little-man-computer

我想在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)

0 个答案:

没有答案