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 b
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).
I opted to mark the end of the input with a terminating 0 value, i.e. the values to be sorted cannot include 0.
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/lmc@v0.77/lmc.js"></script>
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.