memorybubble-sortcontinuouslittle-man-computer

e-LMC extended Little Man Computer bubble embedded program continuous input


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

Solution

  • 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.