admin管理员组

文章数量:1431145

Example: if the entered value is 5, the values 5,6,7,8,9,10 (one number per line) will be displayed followed by the number of iterations. Note that the number 10 should be displayed.

I tried many things but the loop doesn't stop , it keep going after 10

ORG 400          / Start at adress 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     / If AC<0 (X is less than 10), continue loop
JUMP ENDLOOP     / If X is not less than 10, terminate loop


LOAD Counter     / Load the final counter value
OUTPUT           / Display the counter

ENDLOOP , HALT   / Terminate the program

X, DEC 0         / Memory location for X (initialized to 0)
Counter, DEC 1   / Memory location for Counter (initialized to 1)
ONE, DEC 1       / Constant value 1

Example: if the entered value is 5, the values 5,6,7,8,9,10 (one number per line) will be displayed followed by the number of iterations. Note that the number 10 should be displayed.

I tried many things but the loop doesn't stop , it keep going after 10

ORG 400          / Start at adress 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     / If AC<0 (X is less than 10), continue loop
JUMP ENDLOOP     / If X is not less than 10, terminate loop


LOAD Counter     / Load the final counter value
OUTPUT           / Display the counter

ENDLOOP , HALT   / Terminate the program

X, DEC 0         / Memory location for X (initialized to 0)
Counter, DEC 1   / Memory location for Counter (initialized to 1)
ONE, DEC 1       / Constant value 1
Share Improve this question asked Nov 19, 2024 at 11:09 ZiwaseemiZZiwaseemiZ 1 2
  • The loop does not end? From the looks of it it should not loop at all. You never jump back to LOOP. Also 10 is never mentioned in the code either. Anyway, use your simulator to single step the code and see where it goes wrong. – Jester Commented Nov 19, 2024 at 11:12
  • "If AC<0 (X is less than 10)": how does this comment in your code make sense? You just copied the value of X into AC... so how could AC<0 be equivalent with X<10?? – trincot Commented Nov 19, 2024 at 14:37
Add a comment  | 

1 Answer 1

Reset to default 0

You are incrementing X by one (and another one, and another...) and then comparing it to zero.

So how can you compare it to 10?

You can define 10 (possibly 11) as a constant. Then subtract that constant from X and compare the result with zero. Experiment with less than 000 and greater than 800 tests.

本文标签: