Stuck on little man computer language don't get help please

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
cduval04
Messages
1
Reaction score
0

Homework Statement


trying to write a program for school. the program is supposed to do the following:

Write a program that takes an input value (for example, a number 5). The output should be sum of all numbers from 1 to the value input by the user (in this example, the output would be 1+2+3+4+5 i.e. 15)

what I don't get is how if say I enter 20 how to get it to know that I entered 20 and just add all the numbers from 1 to 20 like count. would I use the SUB (subtract) command to aid in this? help-I'm stuck!


Homework Equations





The Attempt at a Solution


INP
STA NUM1
LDA NUM1
INP
STA NUM2
LDA NUM2
ADD NUM2
OUT
HLT
 
Physics news on Phys.org
cduval04 said:
what I don't get is how if say I enter 20 how to get it to know that I entered 20 and just add all the numbers from 1 to 20 like count. would I use the SUB (subtract) command to aid in this? help-I'm stuck!
Hi cduval04! http://img96.imageshack.us/img96/5725/red5e5etimes5e5e45e5e25.gif

I'm not au fait with your language, but they are all much the same. :smile:

Usually you'd do something like loop though a set of instructions repeatedly subtracting 1 each time until the result tests as being equal to 0, at which time you would exit the loop and know that you had executed it N times, where N is the number that was originally input.

So you need to look over your instruction set, and find an instruction that allows you to JUMP or GOTO some line in the code if the accumulator is zero (or maybe non-zero), or something like that.

All clear now? :wink:
 
Last edited by a moderator:
cduval04 said:

Homework Statement


trying to write a program for school. the program is supposed to do the following:

Write a program that takes an input value (for example, a number 5). The output should be sum of all numbers from 1 to the value input by the user (in this example, the output would be 1+2+3+4+5 i.e. 15)

what I don't get is how if say I enter 20 how to get it to know that I entered 20 and just add all the numbers from 1 to 20 like count. would I use the SUB (subtract) command to aid in this? help-I'm stuck!


Homework Equations



The Attempt at a Solution


INP
STA NUM1
LDA NUM1
INP
STA NUM2
LDA NUM2
ADD NUM2
OUT
HLT
It knows you've entered a value after executing the INP command - the value is then in the accumulator. The STA command stores the accumulator value in the stated location (eg, NUM1 and NUM2 in your example).

You need to lookup the use of labels and the branch commands to make loop decisions. Yes, you could use the SUB command, probably the best way as it makes using the BRZ command easier.

Not that I know anything about the LMC, but I just looked it up on Google ... http://en.wikipedia.org/wiki/Little_man_computer - in fact, look at the "count down to zero" example on this page for a starting point for your problem.