0

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
Clifford
  • 88,407
  • 13
  • 85
  • 165
Anyonomiss
  • 25
  • 5
  • Can you point to a language specification of the "e-LMC"? – trincot Oct 25 '20 at 19:15
  • It has a larger memory address (0-999), general purpose registers (R4 to R7), constant registers (R0=0, R1=1, R2=2, R3=999), Memory-mapped IO memory addresses 990-999 reserved for IO. Instruction set: same as LMC but has MUL for multiplication, DIV-for division, MOV (RN < RM), CPY (copy data block of length from SAddr to destination DAddr). – Anyonomiss Oct 25 '20 at 21:02
  • For example, to load a series of numbers from input you might do the following. 1) Initialize the pertinent "STO" instruction. Set location 100 to the pertinent value such as '3500'. 2) Input a number and execute the STO instruction (along with the other pertinent instructions). 3) Load the contents of memory location 100 (3500) into the accumulator, increment by one, and store it back to its location (100). The STO instruction will now read as '3501'. 4) Go to step 2 and continue until the set count of numbers has been provided. – Anyonomiss Oct 25 '20 at 21:18
  • Where is the documentation of this language? – trincot Oct 25 '20 at 21:20
  • Can you receive a PDF? – Anyonomiss Oct 25 '20 at 21:23
  • It doesn't exist online? – trincot Oct 25 '20 at 21:24
  • It's Instruction Set Architecture and Addressing Modes extended LMC and I only have a pdf of it. – Anyonomiss Oct 25 '20 at 21:28
  • E LMC supports the following addressing modes: Direct addressing mode, Immediate addressing mode, Indirect addressing mode, Register indirect addressing mode, Register index relative address mode, Two operands: one is a base address and another operand is register RN containing an offset value. The sum specifies the address at which the data is found. – Anyonomiss Oct 25 '20 at 21:31
  • Then I fear you're not going to get much attraction for this question... I would have a look if it was plain LMC, but if this is about a language that apparently is not documented on the internet, I hope someone else will find the courage to read into it just for the sake of this one question... to then probably never hear about it again. – trincot Oct 25 '20 at 21:32
  • Yes, that's what I thought. Thank you for your honesty. – Anyonomiss Oct 25 '20 at 23:31
  • Here is the document PDF: https://docs.google.com/document/d/1ZOTXOWzJyr5SSpG11XvGji9PLfF51h4YeRYFwpIBn78/edit – Anyonomiss Oct 27 '20 at 13:09
  • This document will show the differences in regular LMC and e-LMC. This may help with what I am trying to do in this project. I appreciate whatever information you can give me. I am not being supplied with anything for this project except parameters. I was not given the documentation to write the bubble sort in this language so I am trying to find help with it. – Anyonomiss Oct 27 '20 at 14:07

1 Answers1

0

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.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thank you for your code. This code has labels and used DAT. e-LMC does not. Did you see the Instruction set in the PDF for e-LMC? – Anyonomiss Oct 28 '20 at 09:38
  • No I didn't, but to be honest, it should be a document that is publicly available on the internet at a reliable site (not a file sharing site). The policy on Stack Overflow is that questions should have all information needed to answer the question. – trincot Oct 28 '20 at 10:59
  • You are correct. Your code actually is helping me a lot. I am able to modify it to e-lmc by changing the numeric codes for the larger memory of 1000. So 901 becomes 9001 and so on. However, the challenge for me is changing the labels to coincide with the register values for direct addressing so that I do not need the variables. – Anyonomiss Oct 28 '20 at 11:17
  • Is it possible to replace the labels with the actual memory locations? Can you help? – Anyonomiss Oct 28 '20 at 11:58
  • When you run the snippet (click "Run code snippet") you'll see the memory addresses displayed in grey at the left of the code, starting with 0. So for instance, `input` is 2, `setcurr1` is 3, `isempty` is 9, ...etc. In fact, the whole program is assembled into the instruction codes, which you see next to the mailbox numbers (also in grey). – trincot Oct 28 '20 at 12:12
  • So, I can replace all of the labels with the actual memory addresses and they will run correctly? I have not tried but I'm just asking before I attempt. – Anyonomiss Oct 28 '20 at 12:20
  • Yes, at least on an LMC it would work like that. The mnemonics & labels are just human readable representations of those numbers. The emulator (also this one) actually first translates the mnemonics and labels to numbers (that is the assembly phase) and then executes those numbers, not the mnemonics or labels. – trincot Oct 28 '20 at 12:30
  • Greatly appreciated! – Anyonomiss Oct 28 '20 at 12:59
  • When I get rid of the labels, the program doesn't run correctly in the LMC simulator. – Anyonomiss Oct 28 '20 at 15:38
  • Which simulator are you using? – trincot Oct 28 '20 at 15:44
  • I want to take out the branch is zero and just have it run for about 100 times how could I tell it to stop at 100? – Anyonomiss Oct 28 '20 at 15:53
  • That is a different question. If you cannot make that work, you should post it as a new question. What about your previous comment where you said it didn't work... which simulator are you using where it doesn't work? – trincot Oct 28 '20 at 15:58
  • 1
    No worries. I got it to work. You were correct and I removed all labels and the program still runs like it's suppose to. I'll post a new question on the BRZ. Thank you. – Anyonomiss Oct 28 '20 at 16:06
  • In your program what does the LDA setfirst and STA setcurr1 at the beginning do? I thought you need to start with INP first. – Anyonomiss Oct 28 '20 at 17:14
  • It is just a precaution for when you would run the program once and then only reset the program without initialising the variables. This ensures that `setcurr1` is as it should be at the start. If you don't care about that, they could be omitted (but if you do and don't use labels, you'll have to redo the correct addressing in the remaining code). – trincot Oct 28 '20 at 17:46
  • Thank you, I will keep them. On your labels what is cmp stand for? – Anyonomiss Oct 28 '20 at 17:51
  • That is short for compare. The `SUB` that is executed there serves to compare one value in the array with the next. – trincot Oct 28 '20 at 17:53
  • Got it thank you. I'll post my final code here when I finish it. – Anyonomiss Oct 28 '20 at 18:55