0

I am tasked to write a program that takes an integer as input, and then prints all integers between that value and 10 as an increasing sequence. After that output, it should also print the count of iterations made. So for instance, if the input is 7, the output should be:

7
8
9
10
4

This is my program:

ORG 400 / Start at address 400
INPUT / Take a number from the user
STORE X / Store INPUT in memory location X
LOAD ONE / Load the constant value 1
STORE Counter / Store it in the Counter memory location
LOOP, LOAD X / Load the value of X
OUTPUT / Display the current value of X
ADD ONE / Increment X by 1
STORE X / Store the new value of X
LOAD Counter / Load the current counter value
ADD ONE / Increment the counter by 1
STORE Counter / Store the updated counter value
LOAD X / Load the value of X
SKIPCOND 000 / Skip if X == 11
JUMP LOOP / Jump back to the start of the loop
LOAD Counter / Load the final counter value
OUTPUT / Display the counter
HALT / Terminate the program
X, DEC 0 / Memory location for X (initialized to 0)
Counter, DEC 0 / Memory location for Counter (initialized to 0)
ONE, DEC 1 / Constant value 1

I expected the loop to end when the number is 10. Also, it should print the number of iterations (loops) at the end, but it keeps going after 10, as you can see in this screenshot:

enter image description here

trincot
357k38 gold badges282 silver badges340 bronze badges
asked Nov 25, 2024 at 18:53
1
  • Count down from 10 to 0. Alternatively, subtract 10 from your counter and check for zero. PS: edit your question and use the code button on the toolbar to format your program. Commented Nov 25, 2024 at 19:11

1 Answer 1

0

There are a few issues in our program:

  • A comment says "Skip if X == 11", but there is nothing in your code that compares with 11. You're just checking whether the value in the accumulator (which is that of X) is negative. You could fix this by subtracting 11 from X and checking if that is negative.

  • You let the the counter start with 1, but that is not correct. You have not counted any valid X yet, so you should start with 0.

  • If the user inputs a number that is greater than 10, I would think you should not output any number, and output a count that is 0. Yet, your program always prints the initial value of X, no matter whether it is within range or not. You could fix this by first doing the exit-check, and only after that check continue the loop with the display part.

Not a major problem, but:

  • Comments that just say what the code does literally are not useful. So don't comment like this:

    STORE X / Store the new value of X
    

    This is information that is not helpful, as the documentation for the STORE instruction already says as much. Instead, try to comment at a higher level, explaining what the higher goal is of a set of instructions.

  • ORG 400: this is not very useful if you don't store data at the top of your program. Just drop this.

Here is a possible implementation:

 INPUT
 STORE X
 LOAD Zero
 STORE Counter
Loop, LOAD X / First check if X is within range
 SUBT Eleven 
 SKIPCOND 000 / Continue loop while X < 11 
 JUMP Exit / Otherwise we've displayed the whole range
 LOAD X 
 OUTPUT / Display the current value of X
 ADD One / Increment X by 1 
 STORE X
 LOAD Counter
 ADD One / Increment the counter by 1 
 STORE Counter
 Jump Loop / ...and repeat
 
Exit, LOAD Counter
 OUTPUT / Display the counter
 HALT
X, DEC 0
Counter, DEC 0
Zero, DEC 0
One, DEC 1
Eleven, DEC 11
answered Nov 25, 2024 at 21:37
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.