2

LMC is a learning tool over @http://gcsecomputing.org.uk/lmc/index.html and a set of problems are "Write a program to output the numbers 1 to 10 in ascending order.

Write a program to output the numbers 1 to 10 in descending order."

Using the instruction set http://gcsecomputing.org.uk/lmc/instruction_set.html I was able to get this

loop LDA value1
OUT
SUB value2
STA value1
BRP loop

value1 DAT 10
value2 DAT 1

This counts from 10 to 0 but I can't figure out how to count upward to 10 and then stop counting. Any help solving these two problems would be very much appreciated.

alrightthen
  • 21
  • 1
  • 4

1 Answers1

1

Change the direction (SUB becomes ADD), change the start/end values (value1, value2) and perform a comparison with 10 (by doing a SUB and BRP) to detect whether the end value has been reached, and do this before incrementing.

Don't forget to add a HLT to your program, as you don't want the execution to continue in your data section.

I would also suggest using more meaningful variable names (DAT labels).

start   LDA current
loop    OUT
        SUB until
        BRP exit
        LDA current
        ADD one
        STA current
        BRA loop
exit    HLT

current DAT 0
until   DAT 10
one     DAT 1

<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.6/lmc.js"></script>
trincot
  • 317,000
  • 35
  • 244
  • 286