I am looking at this assignment:
Write a program that takes three inputs and outputs them in ascending order.
I had used the following LMC simulator: https://www.101computing.net/LMC/#
And this is what I've done so far:
INP 901 Input num1
STA 318 Store num1 in address 18
INP 901 Input num2
STA 319 Store num2 in address 19
INP 901 Input num3
STA 320 Store num3 in address 20
SUB 219 Subtract num2 from num3
BRP 810 Jump to address 10 if it is zero or positive (J1)
LDA 519 Load num2 from address 19
STA 320 Store num3 in address 20
LDA 520 Load num3 from address 20 (J1)
SUB 218 Subtract num1 from num3
BRP 815 Jump to address 18 if it is zero or positive (J2)
LDA 518 Load num1 from address 18
STA 320 Store num3 in address 20
LDA 520 Load num3 from address 20 (J2)
OUT 902 Output result
HLT 000 Stop program
DAT Data num1
DAT Data num2
DAT Data num3
I only achieved to find the largest number, but I do not know how to arrange them in ascending order.
How can I also output the other two values in their right order?
First some comments on the code you provided:
INP
, not INP 901
.STA 318
, which by the previous point really should be STA 18
, but write STA num1
, and define that label at the corresponding DAT
line: num1 DAT
. Note that the simulator you linked to doesn't even accept a DAT
without label.#
or ;
) at the start of the comments. Although some simulators will not mind that you just write comments without such a separator, it is better practice to use it. Also the simulator you linked to does not seem to support comments.Stop program
as a comment next to HLT
. That should be evident. Instead make comments that explain the higher purpose of an instruction in your algorithm. For instance "is num3 greater than num2?" would be good comment at the first BRP
.So applying the above changes to your code, you would get this:
#input: 3 2 1
INP
STA num1
INP
STA num2
INP
STA num3
comp23 LDA num3 # is num2 <= num3?
SUB num2
BRP sorted23 # yes
LDA num2 # no: copy it to num3
STA num3
sorted23 LDA num3 # is num1 <= num3?
SUB num1
BRP output # yes: we're done
LDA num1 # no: copy it to num3
STA num3
output LDA num3 # output the greatest
OUT
HLT
num1 DAT
num2 DAT
num3 DAT
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.815/lmc.js"></script>
This will print the greatest value among the inputs. Now to sort the input, you need to swap pairs of values. For swapping you could use a fourth variable (temp
). For instance, to swap num2
with num3
, you would write:
LDA num3
STA temp # temporary save num3 here
LDA num2
STA num3 # now both are the same value
LDA temp # but we still have a copy...
STA num2
You can sort 3 numbers with maximum three swaps. If there is a third swap involved, it will be the same kind of swap as the first swap.
Here is how that would work:
#input:3 2 1
INP
STA num1
INP
STA num2
INP
STA num3
comp23 LDA num3 # is num2 <= num3?
SUB num2
BRP sorted23 # yes
LDA num3 # no: swap num2 and num3
STA temp
LDA num2
STA num3
LDA temp
STA num2
sorted23 LDA num2 # is num1 <= num2
SUB num1
BRP sorted123 # yes: we're done
LDA num2 # no: swap num1 and num2
STA temp
LDA num1
STA num2
LDA temp
STA num1
BRA comp23 # repeat once
sorted123 LDA num1 # output the sorted list
OUT
LDA num2
OUT
LDA num3
OUT
HLT
num1 DAT
num2 DAT
num3 DAT
temp DAT # used for swapping
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.815/lmc.js"></script>
In my opinion the simulator you pointed to is not the best. The one I use in this answer is available here -- I am its author.