只需将评论用作标签的灵感。不是任何操作目标的行可以不带标签。例如:
start IN // input count
STO count // store count
LDA stoInstruction // STO
ADD location // Determine first location
STO storeInput // Overwrite STO instruction for list
ADD count
STO i // Store STO + LOC + Count to determine end
loopInput LDA storeInput // Load manipulated instruction (using as counter)
SUB i //
BRZ exitInputLoop // If last count, go to END INPUT LIST
IN
storeInput DAT // manipulated instruction (store input in list)
LDA storeInput
ADD one // increment store instruction (to next list location)
STO storeInput // Update STO instruction
BR loopInput // GOTO INPUT LIST LOOP
exitInputLoop LDA one
SUB count // 1 – count
BRP exitLoopI // GO TO END I LOOP
LDA zero
STO i // set I to zero (0)
loopI LDA count
SUB one // COUNT - 1
SUB i // COUNT -1 – I
BRZ exitLoopI // if(I == count - 1) GOTO END I LOOP
LDA count
SUB one
STO j // J = Count – 1
loopJ LDA i // I
SUB j // I - J
BRP exitLoopJ // If I == j, then GO END J LOOP
LDA ldaInstruction // load LDA instruction numeric code
ADD location // set to LDA 500
ADD j // set to LDA [500 + j] or A[j]
STO loadCurrent // reset instruction
SUB one // set to LDA [500 + j – 1] or A[j-1]
STO loadPrevious // reset instruction
loadPrevious DAT // load A[j-1] (instruction is manipulated)
STO previous
loadCurrent DAT // load A[j] (instruction is manipulated)
STO current
SUB previous // A[j] – A[j-1] (swap if not positive)
BRP decrementJ // GOTO DECREMENT J
LDA stoInstruction // load STO instruction code
ADD location // set to STO 500
ADD j // set to STO [500 + j]
STO storeCurrent // reset instruction
SUB one // set to STO [500 + j – 1]
STO storePrevious // reset instruction
LDA current // load A[j]
storePrevious DAT // Store in A[j-1] (instruction is manipulated)
LDA previous // load A[j-1]
storeCurrent DAT // Store in A[j] (instruction is manipulated)
decrementJ LDA j
SUB one
STO j // J = J – 1
BR loopJ // GOTO START J LOOP
exitLoopJ LDA i
ADD one
STO i // I = I + 1
BR loopI // GOTO START I LOOP
exitLoopI LDA count // Count
OUT
LDA ldaInstruction
ADD location // LDA + LOC
STO instruction // set up instruction
ADD count // LDA + LOC + Count
STO i // store unreachable instruction
loopOutput LDA instruction // load manipulated instruction (used as counter)
SUB i
BRZ exitLoopOutput // GOTO END OUTPUT LOOP
instruction DAT // manipulated output
OUT
LDA instruction
ADD one
STO instruction // increment manipulated instruction
BR loopOutput // GOTO OUTPUT LIST LOOP
exitLoopOutput BR start // Branch to top of loop (embedded)
HLT // (Should never hit this instruction)
previous DAT // A[j-1] value (also used for swapping)
current DAT // A[j] value (also used for swapping)
count DAT // count variable (input and output)
DAT // unused
i DAT // ‘I’ counter
j DAT // ‘j’ counter
DAT // unused
location DAT 500 // initial list location
stoInstruction DAT 3000 // STO instruction
ldaInstruction DAT 5000 // LDA instruction
one DAT 1 // one (constant)
zero DAT 0 // zero (constant)
笔记:
这个 LMC 是原始 LMC 的一种变体,它有 3 位数字,而您似乎正在使用一个使用 4 位数字的 LMC。
代码不是很简洁:它使用了 98 个邮箱,不包括输入数据所需的存储空间。它可以用更少的东西来完成。以这个使用 75 个邮箱的实现为例。
您写道需要行号,但是当您使用标签时,行号(即邮箱号)变得无关紧要:LMC 汇编器可以在汇编期间分配它们。
在标准的 100 邮箱 LMC 上运行
在您发表评论后,我在此处提供了适用于标准 LMC 的代码版本。这意味着实际输入数据没有多少空间:只剩下 11 个邮箱用于数据。
我不得不更换以下部分:
location DAT 500 // initial list location
stoInstruction DAT 3000 // STO instruction
ldaInstruction DAT 5000 // LDA instruction
...有了这个:
location DAT list // initial list location
stoInstruction DAT 300 // STO instruction
ldaInstruction DAT 500 // LDA instruction
list DAT // start of the list
这在标准 LMC 中是必要的:
- 操作码是 3 位数字,而不是 4 位数字;
- 邮箱地址是 2 位,而不是 3 位(所以 500 是不可接受的);
- 最好只使用下一个可用地址作为列表的位置,而不是硬编码的邮箱地址。
我还删除了定义未使用邮箱的两行。
最后,我会BRZ
用指令改变这两条指令BRP
,因为理论上不能保证当前一个SUB
给出否定结果时累加器的值是多少。在这种情况下,不能依赖累加器的值(因为它只能有非负值——参见维基百科)。因此BRZ
,对未定义的值执行 a 是有风险的。BRP
是一个安全指令,因为它检查标志——而不是累加器。
#input: 3 44 22 99
start IN // input count
STO count // store count
LDA stoInstruction // STO
ADD location // Determine first location
STO storeInput // Overwrite STO instruction for list
ADD count
STO i // Store STO + LOC + Count to determine end
loopInput LDA storeInput // Load manipulated instruction (using as counter)
SUB i //
BRP exitInputLoop // If last count, go to END INPUT LIST
IN
storeInput DAT // manipulated instruction (store input in list)
LDA storeInput
ADD one // increment store instruction (to next list location)
STO storeInput // Update STO instruction
BR loopInput // GOTO INPUT LIST LOOP
exitInputLoop LDA one
SUB count // 1 – count
BRP exitLoopI // GO TO END I LOOP
LDA zero
STO i // set I to zero (0)
loopI LDA count
SUB one // COUNT - 1
SUB i // COUNT -1 – I
BRZ exitLoopI // if(I == count - 1) GOTO END I LOOP
LDA count
SUB one
STO j // J = Count – 1
loopJ LDA i // I
SUB j // I - J
BRP exitLoopJ // If I == j, then GO END J LOOP
LDA ldaInstruction // load LDA instruction numeric code
ADD location // set to LDA 500
ADD j // set to LDA [500 + j] or A[j]
STO loadCurrent // reset instruction
SUB one // set to LDA [500 + j – 1] or A[j-1]
STO loadPrevious // reset instruction
loadPrevious DAT // load A[j-1] (instruction is manipulated)
STO previous
loadCurrent DAT // load A[j] (instruction is manipulated)
STO current
SUB previous // A[j] – A[j-1] (swap if not positive)
BRP decrementJ // GOTO DECREMENT J
LDA stoInstruction // load STO instruction code
ADD location // set to STO 500
ADD j // set to STO [500 + j]
STO storeCurrent // reset instruction
SUB one // set to STO [500 + j – 1]
STO storePrevious // reset instruction
LDA current // load A[j]
storePrevious DAT // Store in A[j-1] (instruction is manipulated)
LDA previous // load A[j-1]
storeCurrent DAT // Store in A[j] (instruction is manipulated)
decrementJ LDA j
SUB one
STO j // J = J – 1
BR loopJ // GOTO START J LOOP
exitLoopJ LDA i
ADD one
STO i // I = I + 1
BR loopI // GOTO START I LOOP
exitLoopI LDA count // Count
OUT
LDA ldaInstruction
ADD location // LDA + LOC
STO instruction // set up instruction
ADD count // LDA + LOC + Count
STO i // store unreachable instruction
loopOutput LDA instruction // load manipulated instruction (used as counter)
SUB i
BRP exitLoopOutput // GOTO END OUTPUT LOOP
instruction DAT // manipulated output
OUT
LDA instruction
ADD one
STO instruction // increment manipulated instruction
BR loopOutput // GOTO OUTPUT LIST LOOP
exitLoopOutput BR start // Branch to top of loop (embedded)
HLT // (Should never hit this instruction)
previous DAT // A[j-1] value (also used for swapping)
current DAT // A[j] value (also used for swapping)
count DAT // count variable (input and output)
i DAT // ‘I’ counter
j DAT // ‘j’ counter
location DAT list // initial list location
stoInstruction DAT 300 // STO instruction
ldaInstruction DAT 500 // LDA instruction
one DAT 1 // one (constant)
zero DAT 0 // zero (constant)
list DAT
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.815/lmc.js"></script>