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
The code 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 the owner
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
milf43 Dining is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.