本文介绍了e-LMC扩展的Little Man Computer气泡嵌入式程序连续输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个e-LMC扩展的小矮人计算机程序,该程序将接受不确定的输入并对它们进行冒泡排序.我需要有更多的连续输入,然后进行气泡排序.

I am looking for a e-LMC extended little man computer program that will accept an indefinite inputs and bubble-sort them. I need to have more inputs continuous and then do the bubble sort.

INP //read 1st value
STA 0 // for store
INP // read 2nd value
   STA 1 // store
INP // read 3rd value
STA 2 // store
LDA 1 // LOOP 1, STEP 1:
    SUB 0 //
BRP STEP2 // if R0 and R1 are in order, don't swap them
LDA 1 // Begin swapping registers
STA 3
LDA 0
STA 1 // R1 = R0
LDA 3
STA 0 //R0 = temp
STEP2 LDA 2 // LOOP 1, STEP 2
SUB 1
BRP STEP3 // If R1 and R2 are in order, don't swap them
LDA 2 // Begin swapping registers
STA 3 // temp =R2
LDA 1
STA 2 //R2=R1
   LDA 3
STA 1 // R1 = temp
STEP3 LDA 1 // LOOP 2, STEP 1
SUB 0
BRP STEP4 // if R0 andR1 are in order, don't swap them
LDA 1 // Begin swapping registers
STA 3 // temp = R1
LDA 0
STA 1 //R1=R0
LDA 3
STO 0 // R0 = temp

STEP4 LDA 0
OUT
   LDA 1
   OUT
LDA 2
OUT
   HLT

推荐答案

由于我不熟悉e-LMC,因此在此提供了一个纯LMC实现.不利的一面当然是LMC限制了空间.由于以下代码占用了62个邮箱(不包括输入数组),因此最多可以输入38个值(未经验证).

As I am not familiar with e-LMC, I provide here a pure LMC implementation. The downside is of course that space is limited with an LMC. As the below code occupies 62 mailboxes excluding the input array, a maximum of 38 values can be input (this is not verified).

我选择以0结尾的值来标记输入的结尾,即要排序的值不能包含0.

I opted to mark the end of the input with a terminating 0 value, i.e. the values to be sorted cannot include 0.

正如您已经在注释中指出的那样,此解决方案在很大程度上依赖于自修改代码.标有 get *** set *** cmp *** 的所有指令均会动态更改为指向正确的元素在数组中.

As you already indicated in comments, this solution relies heavily on self-modifying code. All instructions that are labelled with get***, set*** and cmp*** are dynamically changed to point to the right element in the array.

您可以在以下代码段中运行此代码(它会加载模拟器):

You can run this code in this snippet (it loads an emulator):

#input: 5 2 4 1 3 0
         LDA setfirst
         STA setcurr1
input    INP
setcurr1 STA array
         BRZ isempty
         LDA setcurr1
         ADD one
         STA setcurr1
         BRA input

isempty  LDA array
         BRZ zero     ; empty array
sort     LDA getfirst ; init "curr" indices
         STA getcurr1
         STA getcurr2
         LDA setfirst
         STA setcurr2
         LDA cmpfirst
         STA cmpcurr
         STA issorted ; bool: assume sorted

loop     LDA getcurr1 ; set "next" indices
         ADD one
         STA getnext1
         STA getnext2
         LDA setcurr2
         ADD one
         STA setnext

getnext1 LDA array
         BRZ check    ; end of array
cmpcurr  SUB array
         BRP inc      ; no swap needed
getcurr1 LDA array    ; swap
         STA temp
getnext2 LDA array
setcurr2 STA array
         LDA temp
setnext  STA array
         LDA zero
         STA issorted ; was not sorted yet
inc      LDA getnext1  ; increment "cur" indices
         STA getcurr1
         LDA setnext
         STA setcurr2
         LDA cmpcurr
         ADD one
         STA cmpcurr
         BRA loop

check    LDA issorted
         BRZ sort

getcurr2 LDA array
         BRZ zero     ; all done
         OUT
         LDA getcurr2
         ADD one
         STA getcurr2
         BRA getcurr2

; constants:
zero     HLT
one      DAT 1
getfirst LDA array
setfirst STA array
cmpfirst SUB array

; variables:
issorted DAT ; boolean
temp     DAT ; for swapping
array    DAT ; start of array

<script src="https://cdn.jsdelivr.net/gh/trincot/[email protected]/lmc.js"></script>

正如您所写,e-LMC是LMC的扩展,具有更多的寄存器和寻址方法,我想将其更改为可以利用这些扩展的程序并不难.

As you wrote that the e-LMC is an extension to the LMC, having more registers and addressing methods, I suppose it is not so hard to change this into a program that would take benefit of those extensions.

这篇关于e-LMC扩展的Little Man Computer气泡嵌入式程序连续输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 12:14