Step Count Algorithm in LMC

steps-count-controlled-loopCount-controlled loops are used in many programs to increment a counter for each iteration of the loop. Per default the increment for the counter is +1.

FOR counter FROM 1 TO 10:
    OUTPUT (COUNTER)

However you can specify a different step when incrementing your counter in a count-controlled loop. For instance to count in 5:

FOR counter FROM 0 TO 100 STEP 5:
    OUTPUT (COUNTER)

You can also use different starting and ending values:

FOR counter FROM 50 TO 250 STEP 5:
    OUTPUT (COUNTER)

A negative step can also be used to decrement the counter:

FOR counter FROM 20 TO 0 STEP -2:
    OUTPUT (COUNTER)

This challenge consists of writing a program using LMC to implement a count-controlled loop using a step defined by the end user. Your program will:

  • INPUT: Retrieve the starting value for the step count algorithm from the end user.
  • INPUT: Retrieve the ending value for the for step count algorithm from the end user.
  • INPUT: Retrieve the step value from the end user.
  • Count and output all the values (from the starting value to the ending value, using the given step).

Note that this algorithm is an implementation of an arithmetic number sequence.

To complete this challenge you will need to use one of the following online LMC Simulators:

The list of LMC instructions is provided at the bottom of this page.

You will be able to test your algorithm using the following input values:

Test # Input Values Expected Output Pass/Fail
#1 Start Value: 10
End Value: 20
Step: 2
10,12,14,16,18,20
#2 Start Value: 82
End Value: 100
Step: 5
82,87,92,97
#3 Start Value: 10
End Value: 1
Step: -1
negative-step
10,9,8,7,6,5,4,3,2,1

LMC Instruction Set


Note that in the following table “xx” refers to a memory address (aka mailbox) in the RAM. The online LMC simulator has 100 different mailboxes in the RAM ranging from 00 to 99.

Mnemonic Name Description Op Code
INP INPUT Retrieve user input and stores it in the accumulator. 901
OUT OUTPUT Output the value stored in the accumulator. 902
LDA LOAD Load the Accumulator with the contents of the memory address given. 5xx
STA STORE Store the value in the Accumulator in the memory address given. 3xx
ADD ADD Add the contents of the memory address to the Accumulator 1xx
SUB SUBTRACT Subtract the contents of the memory address from the Accumulator 2xx
BRP BRANCH IF POSITIVE Branch/Jump to the address given if the Accumulator is zero or positive. 8xx
BRZ BRANCH IF ZERO Branch/Jump to the address given if the Accumulator is zero. 7xx
BRA BRANCH ALWAYS Branch/Jump to the address given. 6xx
HLT HALT Stop the code 000
DAT DATA LOCATION Used to associate a label to a free memory address. An optional value can also be used to be stored at the memory address.

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 6

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: ,