little-man-computer

Output a sequence


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]


Solution

  • 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>