0

For a Little Man Computer (LMC) simulator I have the following assignment:

Your code must accept a number between 1 and 15. It must then add the number 2 (to the submitted number), before displaying the new total. This process must be repeated until the final total reaches either 14 or 15. At this point, the program must end. Note: your final code MUST NOT output any negative numbers.

So it should display the output as sequence 2,4,6,8,10,12,14 or 3,5,7,9,11,13,15.

The problem I am facing is that whatever number I input between 1 and 15 it is just multiplying the number by itself and outputting it.

Here is my code so far:

INP
STA 11
ADD 11
OUT
ADD 11
OUT
ADD 11
OUT
HLT

2

What am I doing wrong?

[![Picture of LMC run][2]][3]

trombonee
  • 31
  • 7
  • 1
    @SamuelLiew Related meta question https://meta.stackoverflow.com/questions/402467/what-does-needs-debugging-details-mean-here-little-man-computer-question – tripleee Oct 29 '20 at 08:02
  • How could the output ever start with 2 when the input is at least 1, and the instructions tell you that *first* 2 must be added to the input *before* outputting the new total? In my understanding that means you can never have an output that starts with 2. – trincot Oct 30 '20 at 11:18

2 Answers2

1

Some remarks about what you wrote:

So it should display the output as sequence 2,4,6,8,10,12,14 or 3,5,7,9,11,13,15.

The instructions make it impossible to get an output that starts with 2, as the minimum input value is said to be 1, and the program should add 2 to it before the first output. So that means the first value that is output will be at least 3.

As the input can be a greater value (up to 15), the output sequence can be shorter than what you indicate. For instance, if the input is 10, the output should be just 12, 14.

This also means that if the user inputs 14 or 15, the program will not produce any output at all.

The problem I am facing is that whatever number I input between 1 and 15 it is just multiplying the number by itself and outputting it.

The line with 2 in your code is doing nothing, as in the LMC language, constant values need to be accompanied with a DAT mnemonic:

DAT 2

If you had done that, the value 2 would have been stored in mailbox 10. But even then, you are never accessing that mailbox in your code, so you are not adding 2 at any stage during the execution.

Instead, you store the input at mailbox 11 (which is OK), and then add mailbox 11 to that, which indeed results in twice that number. And you repeat this exactly 2 more times, so that you output three times that number and four times that number.

You should use labels instead of numeric references to mailboxes (like you did with 11). So you should have something like:

two DAT 2
sum DAT

and then use those labels as follows:

    INP
    STA sum ; instead of 11
    ADD two ; this will add 2

Can someone help me with what I am doing wrong?

Besides the above mentioned points, your attempt lacks a conditional loop. It should verify somewhere that the accumulated sum has not reached the limit (14 or 15), and if so -- and only then -- it should continue to add another 2 to the sum and output it.

You can repeat code conditionally by doing a subtraction and checking the result is not negative with BRP. So depending on the subtraction result, you can let the execution continue with code in a loop, or let it exit that loop. If looping, eventually it will arrive again at this spot where it will repeat the subtraction and the check. At some point the subtraction result will have a different sign, and BRP will react differently. This way you can get out of the loop.

Solution

Here is an implementation which you can run here (run the snippet to assemble the LMC code, and then click the newly displayed Run button to actually run the assembled code).

#input: 1
     INP
     STA sum
loop SUB n14  ; compare sum with 14
     BRP end  ; stop when sum is equal or greater...
     LDA sum
     ADD two
     STA sum
     OUT
     BRA loop ; repeat
end  HLT
two  DAT 2
n14  DAT 14
sum  DAT

<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.78/lmc.js"></script>
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    this worked perfectly. Thank you for the clear explanation for what I was doing wrong and also explaining how it all worked. Thank you – trombonee Oct 30 '20 at 22:29
0

So in simple terms,

  1. Input a number
  2. Abort (branch to end) if it's less than zero
  3. Abort (branch to end) if it's zero
  4. Abort (branch to end) if it's bigger than 14
  5. Add 2
  6. Print
  7. Branch to 4

Your code is reading a number, then adding it to itself three times, printing after each (which should also use a loop if done properly).

Because this is an assignment, I'll leave it to you to figure out how to write this in actual LMC code. The opcode to define a constant like 2 is DAT, and with this primitive instruction set, the way to compare two numbers is to subtract them. Then use the "branch if zero or positive" opcode to decide whether or not to loop back.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • No, forgive my question if it wasn't vague enough but when I tried asking the first time my question was deleted(I think). My task is: "Your code must accept a number between 1 and 15. It must then add the number 2 (to the submitted number), before displaying the new total. This process must be repeated until the final total reaches either 14 or 15. At this point, the program must end. Note: your final code MUST NOT output any negative numbers. " I am doing web dev and mobile degree, and i am in foundation year and lack CS skills. I know React, but lack logic thinking – trombonee Oct 30 '20 at 01:44
  • when I look up on the LMC table it doesn't mention anything about loops, I think there is 10 different instructions/values. So I didn't know loops are possible in LMC or even how to do them – trombonee Oct 30 '20 at 01:49
  • It's called "branch". I'm sure this must have been explained in lectures. – tripleee Oct 30 '20 at 05:14
  • Instead of adding details in comments, you should make sure your question is up to date and includes everything which may be relevant. You have now twice provided more detailed versions of the assignment in comments. – tripleee Oct 30 '20 at 05:15
  • Your question was closed, but then reopened. Deleting happens if it's closed for several days or weeks, or if a moderator removes it (spam, harassment, etc). – tripleee Oct 30 '20 at 05:20
  • The point of a toy computer like LMC is to demonstrate that it can do everything a real computer can do if you write enough code. Apart from better I/O facilities and a *slightly* more versatile instruction set, the CPU in your real computer is quite similar. These simple atomic instructions are the building blocks of higher-order logic like "loop until no longer true" or "compare two strings". – tripleee Oct 30 '20 at 05:36
  • I'm sure in React too there is a marked difference between "add the number to itself" and "add 2 to the number". – tripleee Oct 30 '20 at 05:46
  • The demo when you open https://www.101computing.net/LMC/ contains a program which is roughly similar to this. – tripleee Oct 30 '20 at 06:01