-1

I'm trying to solve this challenge:

create a simple LMC program to determine if a number evenly divides another one:

Input 2 numbers

If they do not evenly divide, then the program outputs 0 and asks to input two new numbers, and If they evenly divide, then the program outputs the quotient and stops execution.

If one of the numbers is negative output -1 first and then output the quotient and stop execution

I found this answer that is almost perfect for my requirements. Here are the issues and what I need:

  1. 10/5 = 2 - correct

  2. 3/5 = 0 - correct, as the input does not divide evenly

  3. 4/-2 = -2 - should output -2, but it currently outputs 0

  4. 6/0 - should output 0 because division by zero is undefined, but it currently goes into a nonstop loop

For numbers 1 and 2, the output is correct: the input is either evenly divided or not, resulting in the expected answers. For number 3, the code should handle the input and output -2, but instead, it loops until it outputs 0. It will still divide each other even there's negative. For number 4, dividing by zero should output 0, but the code goes into a nonstop loop.

What do I need to add to the code or adjust to fix the issues with samples 3 and 4? The code currently can't process these correctly.

Credits to @trincot:

start   LDA zero # reset
        STA ANSWER
        INP
        STA DIVIDEND
        INP
        STA DIVISOR
LOOP    LDA DIVIDEND
        BRZ END
        SUB DIVISOR
        BRP continue # No negative overflow
        LDA zero # Tell user there is a remainder
        OUT
        BRA start # ... and ask for new inputs
continue STA DIVIDEND
        LDA ANSWER
        ADD INC
        STA ANSWER
        BRA LOOP
END     LDA ANSWER
        OUT
        HLT
DIVIDEND DAT
DIVISOR DAT
ANSWER  DAT
INC     DAT 1
zero    DAT 0
5
  • 1
    In point three you say it should output -2, but in the earlier description you write "If one of the numbers is negative output -1 first and then output the quotient". These two are in contradiction. Which is what you need?
    – trincot
    Commented Jul 6 at 15:17
  • Secondly, you describe what you want to behave differently. For instance, you want to output 0 when the divisor is 0. That's easy. Just check that is zero and if so output 0. Have you tried it? What was the problem? Please show us the code you tried with...
    – trincot
    Commented Jul 6 at 15:23
  • 1
    Related to the first comment: in the original specification of LMC there is no negative sign: inputs and outputs consist of numbers with (up to) three digits, and that's it. So a requirement where inputs can be negative, as well as output can be negative means that you have a specific LMC simulator that can input/output negative numbers. Please provide which simulator you are using.
    – trincot
    Commented Jul 6 at 15:26
  • 1
    Stackoverflow is for more for direct Q&A, and less for modifying found code (that you perhaps have yet to understand). Try writing up some pseudo code that completely addresses your problem statement, and then your questions will be about how to do the same in LMC assembly, like how do I do this if-then-else statement in LMC, instead of being about how to solve this whole problem statement directly in assembly.
    – Erik Eidt
    Commented Jul 7 at 19:09
  • @milf43Dining, any reason you're not responding to the comments?
    – trincot
    Commented Jul 9 at 4:35

0